Does `pip install` add current directory to PYTHONPATH?

妖精的绣舞 提交于 2021-02-07 09:13:05

问题


If I have a python project that contains a setup.py file I can run pip install --user -e .. This will install all of the requirements listed in the setup file and add the current project to my pip list:

$ pip show project-name                                                                                
Name: project-name                                                                                    
Version: 1.0.0                                                                             
Summary: None                                                                                                                             
Location: /path-to-project/                    
Requires: matplotlib, numpy, scipy, ...                                                    
Required-by:   

Will this by default add the \path-to-project\ to the users PYTHONPATH, or do you have to do that manually, so you can import the code via import project-name from any location on the system?


回答1:


No, pip does not alter the PYTHONPATH and you don't need to either. pip instead uses standard paths set by Python when you install the project. You don't need to make any changes, you already installed the package in a standard location when you ran pip install --user.

Projects installed with -e use an egg link file and are added to a .pth file in a standard location. The location shown is listed in a project-name.egg-link file (really just a piece of metadata to track packages) and the same path is added to the easy-install.pth file in the same directory. Python automatically adds any paths listed in .pth files to the module search path on startup (this is handled by the site module).

The same set of standard paths is also used to find the project again when you run pip show; that the command works shows that the project is already on the Python module search path, correctly installed.

You can verify the paths by running python -m site. Because you installed the package with the --user switch, the location pip uses is shown there as the USER_SITE path. Check that path to find the *.egg-link and easy-install.pth files.



来源:https://stackoverflow.com/questions/52060533/does-pip-install-add-current-directory-to-pythonpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!