The path to the package installed by python pip

Take ubuntu as an example

In an ubuntu environment I got from a store, python3.6 and python2.7 were installed at the same time, and ros was installed. Finally, the location of the pip installation package was very confusing, and I didn’t know where the installed package was installed. If you need code prompts when using vscode, you have to find the path of the package, and then add the path to the following two fields of settings.json in the .vscode folder.

1. A python module has been installed, but vscode prompts that it cannot be found.

The following is an example of the settings.json file (separate different paths with English commas)

{
    "python.autoComplete.extraPaths": [
        "/opt/ros/melodic/lib/python2.7/dist-packages",
        "/usr/local/lib/python2.7/dist-packages/"
    ],
    "python.analysis.extraPaths": [
        "/opt/ros/melodic/lib/python2.7/dist-packages",
        "/usr/local/lib/python2.7/dist-packages/"
    ]
}
The library installation directory of pip that comes with the system is dist-packages. The
library installation directory of pip installed by yourself is site-packages.

2. Find the path of the pip installation package

If the pip list command cannot see the target package (easiest, recommended)

Print it directly

You can open python from the command line

>>> import cv2
>>> print(cv2)

<module 'cv2' from '/usr/lib/python2.7/dist-packages/cv2.x86_64-linux-gnu.so'>

/usr/lib/python2.7/dist-packages is the required path

Or you can create a .py file

import cv2
print(cv2)

If the pip list command can see the package you are looking for

This package can be found in the pip package installation path.

View pip version (capital V)

$ pip -V
pip 20.3.4 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

You can see that the current default pip version and the path to the download package will be output, as well as the corresponding python version.

Add this path (remove the pip (python 2.7)): /usr/local/lib/python2.7/dist-packages/ to settings.json

1 Check the pip library installation paths corresponding to different python versions

Corresponding to python3 and python2 respectively

python3 -m pip -V

python2 -m pip -V

2 Modify the default python version and pip version (according to your own needs)

Even if multiple versions of python are installed on a system, typing python in the terminal will always determine the python version that opens. Only when typing python3 or python2 will the corresponding python be opened.

This is because there are soft links created in the system that bind python to a specific python version. To modify the default python version, you need

2.1 Delete existing soft links first

sudo rm /usr/bin/python

2.2 Create a new soft link (note that the python3.6 path in the middle is modified according to your own needs and the actual system)

sudo ln -s /usr/bin/python3.6 /usr/bin/python

For example, check the installation path of your python3.6:

which python3.6

Guess you like

Origin blog.csdn.net/qq_35858902/article/details/128998525
Recommended