Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm confused about how PyCharm determines the path Python uses to locate modules and packages.

First of all, when I uncheck the settings (in both the "Python Console" and my Run Configuration), I still see the directory for my project at the start of sys.path.

For example, I have project in 'path problem' and 'Run" a file there containing

import sys
for p in sys.path:
    print p

I get

/Users/Rax/Documents/Projects/pathproblem
... (other things in my PYTHONPATH)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages

even when I've asked for the "contents root" to be excluded from the path:

enter image description here

This can result in successful imports of modules that will fail to import in typical deployments (e.g. when building a package).

If I check the setting I get it twice:

/Users/Rax/Documents/Projects/pathproblem
... (other things in my PYTHONPATH)
/Users/Rax/Documents/Projects/pathproblem
...

It seems that PyCharm always adds the current project root at the start of (what it considers to be) the PYTHONPATH and that this setting just adds it again at the end.

(1) How do I configure PyCharm so that it (really) doesn't add the project directory to the package search path?

Also, as near as I can tell, PYTHONPATH, for PyCharm, is not my system PYTHONPATH at all, but the "user added" entries in — in fact, confusingly, at the end of — the Path settings for the Python Interpreter.

(2) Where does PyCharm's PYTHONPATH come from? It's not the PYTHONPATH I see anywhere else on my system.


FWIW, PyCharm's Sphinx respects "contents root" settings, adding the content root only when path when checked in the build configuration.

See Question&Answers more detail:os

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

1 Answer

First of all, when I uncheck the settings (in both the "Python Console" and my Run Configuration), I still see the directory for my project at the start of sys.path.

It should because PyCharm adds the project root to the Path by default.

(1) How do I configure PyCharm so that it (really) doesn't add the project directory to the package search path?

To the best of my knowledge you cannot. However, you can do something that does not add your files to the content route. However, before showing you how to do that, let me explain as to why it adds the current root of the directory to its path. When you open up the python console:

                                                enter image description here

Now, I have a file called foo.py, and in that file I have a function called bar, so I can easily import the function into my console:

enter image description here

As you can see in the image above, PyCharm automatically add your current root. This should come as no surprise since when you cd into a directory, and run something like python foo.py, you are implicitly adding the current root to your path. To emulate this in the python console, PyCharm adds the current root.

To avoid this, you can simple create a folder inside your current root and mark it as a sources root:

                 enter image description here

And since your Source roots will not be added to the path, you can rest easy:

                    enter image description here

(2) Where does PyCharm's PYTHONPATH come from? It's not the PYTHONPATH I see anywhere else on my system.

It comes from your interpreter settings:

enter image description here

In general, PYTHONPATH is essentially a collection of all the directories that house your standard library files, and also your third party library files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...