Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

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

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.

enter image description here

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.

enter image description here

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...