Fun with ROS2's pit-filling road-Defaulting to user installation because normal site-packages is not writeable

Table of contents

1. Ask the question - python package cannot be installed to the regular path

2. Analyze the problem - the regular site-packages folder is not writable

2.1 Permission to modify the installation path of third-party software packages (effective, but accurate operation)

2.2 Restrict the package to be valid only for the current user (invalid)

2.3 Build a virtual environment and install software packages in the virtual environment

3. Solution - check the python version first, and then modify the permissions accurately

3.1 Query python version

3.2 Modify folder permissions

later words


1. Ask the question - python package cannot be installed to the regular path

After the last time, the compilation warning of setuptools was solved.

Fun with ROS2's pit-filling road-SetuptoolsDeprecationWarning: setup.py install is deprecated https://blog.csdn.net/slampai/article/details/128104802?spm=1001.2014.3001.5501

I ran into the problem again that the package could not be installed to the regular path.

The exception prompt occurs when using pip install to install the python package, and the content is as follows:

Defaulting to user installation because normal site-packages is not writeable.

Although it is just an error message, the software can still be installed normally, but the files are placed in a temporary path, which may affect the calling of the software package by ROS2.

2. Analyze the problem - the regular site-packages folder is not writable

2.1 Permission to modify the installation path of third-party software packages (effective, but accurate operation)

Some articles suggest: Find the current python environment directory, usually in /usr/local/lib. Find the corresponding python version in it, and open the permissions of the dist-packages folder inside.

So, how to determine the python version and directory currently in use? Let's talk about this later.

First assume that we already know that python3.10 is currently used by default, so execute the following instructions.

sudo chmod 777 /usr/local/lib/python3.10/dist-packages

So far, if the problem is solved, it means that the current python version is indeed 3.10.

If you fail, please read on.

The reason for the failure is actually that python's package management path has multiple locations, and we need to accurately release the installation path we need.

 Tips: There are two installation paths for Python to install third-party packages:

If it is the python that comes with the system, the dist-packages directory will be used.

Usually /usr/local/lib/<python version number>/dist-packages/;

If you install python manually, it will directly use the directory site-packages,

Usually /usr/local/lib/<python version number>/site-packages/;

Also, there is a temporary path. (Note: That is the temporary path mentioned at the beginning of this article)

 ~/.local/lib/ <python version number> /site-packages

For some situations where it is necessary to check the path of the third-party library, there may be a pitfall here. Even if a certain library exists in the normal path, the import still cannot be found, so we can only find a way to point to the temporary library here.

2.2 Restrict the package to be valid only for the current user (invalid)

Sorry, it didn't work. Including using sudo, also didn't work.

pip install --user <package-name>

2.3 Build a virtual environment and install software packages in the virtual environment

This idea is very good, but the premise is that you need to install a virtual environment tool, such as virtualenv.

virtualenv is used to create an independent Python virtual environment, which can separate each project from other projects without affecting each other, and solves the problem of version conflicts of dependent packages.

Then when installing the virtual environment tool, it will still prompt Defaulting to user installation because normal site-packages is not writeable. If you can live with this warning, you can use this method to proceed happily.

# 安装virtualenv
sudo pip install virtualenv

# 以默认的python创建虚拟环境,名为new_env
virtualenv new_env
# 当系统中存在多个python版本时,选择指定版本的python创建虚拟环境
virtualenv --python /usr/lib/bin/python3.10 new_env

# 默认情况下虚拟环境不会依赖系统环境的site-packages,
# 如果想依赖系统环境的site-packages,可以使用--system-site-packages来设置。
virtualenv --system-site-packages cms

 With it, a relatively independent python environment can be created, but manual activation is required before each use.

# 启动虚拟环境:activate,其中new_env为上一步创建的虚拟环境名称
[...]$ source new_env/bin/activate
(new_env)[...]$

# 退出虚拟环境:deactivate
(new_env)[...]$ deactivate
[...]$

In addition, it also has a virtualenv extension tool called virtualenvwrapper, which can easily create, delete, copy, and switch between different virtual environments. The installation method is as follows:

pip install virtualenvwrapper

The function of virtualenvwrapper is also more powerful. Based on virtualenv, it provides a wealth of commands to operate the virtual environment:

  • Create a virtual environment: mkvirtualenv [virtual environment name]
  • List virtual environments: lsvirtualenv
  • Switch virtual environment: workon [virtual environment name]
  • Check which packages are installed in the current environment: lssitepackages
  • Enter the directory of the current environment: cdvirtualenv [subdirectory name]
  • Enter the site-packages directory of the current environment: cdsitepackages [subdirectory name]
  • Control whether the current environment uses global site-packages: toggleglobalsitepackages
  • Copy virtual environment: cpvirtualenv [source] [dest]
  • Exit the virtual environment: deactivate
  • Remove virtual environment: rmvirtualenv [virtual environment name]

3. Solution - check the python version first, and then modify the permissions accurately

Comparing the three schemes, in fact, if the method is proper, the first one is more convenient.

There are two steps in total, and the pro-test is effective. The recommended steps are as follows:

3.1 Query python version

Bash enters python interactive mode, enter the following:

import sys
print(sys.path)

Unsurprisingly, a list of python paths is returned.

Find the path starting with /usr/local/lib/python , which corresponds to the third-party software package installation path of the current python environment.

In the example of the screenshot above, the corresponding path is  /usr/local/lib/python3.10/dist-packages .

3.2 Modify folder permissions

# 请用上一步查询得到的python软件包安装路径
sudo chmod 777 /usr/local/lib/python3.10/dist-packages

So far, problem solved, move on.

later words

This method is pro-test effective.

In different environments, the installation paths of third-party software packages may be different.

If the method is invalid, please reconfirm the installation path of the third-party software in the current python environment.

Hope it helps you.

Guess you like

Origin blog.csdn.net/slampai/article/details/128123964