Controlling PYTHONPATH ordering in PyCharm to make the Source Folders are searched first

喜夏-厌秋 提交于 2021-01-28 01:47:20

问题


I create a projectname package, and use PyCharm to debug the code in it. I also use venv for setting up Python environment for the package. I follow the standard package structure as follows.

.
├── NAME
│   ├── __init__.py
│   ├── arith.py
│   └── arith.py
├── bin
│   └── app.py
├── build
│   ├── bdist.macosx-10.11-intel
│   └── lib
│       └── NAME
│           ├── __init__.py
│           └── arith.py
├── dist
│   └── projectname-0.1-py2.7.egg
├── docs
├── requirements.txt
├── setup.py
└── tests
    ├── __init__.py
    └── arith_tests.py

Then, I imported the project into PyCharm. In Project:sekelton, I marked NAME/tests as source folders, and build/dist as excluded folders.

I also run python setup.py install to build and install the generated egg file into the venv's site-package directory.

The issue is that the egg file installed in site-package is called first as the PYTHONPATH shows from import sys; print sys.path:

['/python/structure/projects/skeleton/bin',
 'python/structure/projects/venv/lib/python2.7/site-packages/projectname-0.1-py2.7.egg',  <== egg file is searched first
 'python/structure/projects/skeleton/NAME',    <==  

This is pretty annoying as I can't debug the code with PyCharm, and when I modified the code, I had to run python setup.py install again to update the egg file. I may be able to circumvent this issue by removing the egg file from the Project Interpreter setup, but I think changing the order should be the better option.

How can I change the PYTHONPATH order in PyCharm so that the local Source Folders are searched first?

EDIT

The PyCharm shows an error message when I tried to remove the package from the Project Interpreter setup, but it's false positives, as PyCharm successfully removes the egg file, and updates the easy-install.pth.


回答1:


Using pip install -e NAME, will make the projectname.egg-link that points to the NAME directory, and update the easy-install.pth. I think this is better approach than python setup.py, and solves the PYTHONPATH problem.

skeleton> pip install -e .
Obtaining file:///python/structure/projects/skeleton
Requirement already satisfied (use --upgrade to upgrade): nose in /python/structure/projects/venv/lib/python2.7/site-packages (from projectname==0.1)
Installing collected packages: projectname
  Running setup.py develop for projectname
Successfully installed projectname

With this approach, the egg is not in PYTHONPATH.

['/python/structure/projects/skeleton/bin',     
'/python/structure/projects/skeleton/NAME',

Even better, we can remove the package with pip uninstall projectname.

pip uninstall projectname
Uninstalling projectname-0.1:

python/structure/projects/venv/lib/python2.7/site-packages/projectname.egg-link
Proceed (y/n)? y
    Successfully uninstalled projectname-0.1    

I got hints from Installing Python packages from local file system folder with pip



来源:https://stackoverflow.com/questions/33788152/controlling-pythonpath-ordering-in-pycharm-to-make-the-source-folders-are-search

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