Why does PyCharm always add "contents root" to my PYTHONPATH, and what PYTHONPATH?
Asked Answered
D

2

26

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.

Deon answered 18/1, 2014 at 2:45 Comment(0)
H
21

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.

Hydrography answered 18/1, 2014 at 10:59 Comment(1)
"As you can see in the image above, PyCharm automatically add your current root." Actually, it appears to come from Python itself. And the question remains about (2): is it just the user added paths?Deon
G
5

In this case, "projects/pathproblem" is added to sys.path by Python itself, not by PyCharm. See http://docs.python.org/2/library/sys.html#sys.path : "As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter."

To answer your second question, PyCharm builds the PYTHONPATH by running the selected Python interpreter with a script to print the contents of sys.path, recording its result, and then adding necessary changes based on the project settings (Django, App Engine etc.)

Goldenseal answered 18/1, 2014 at 11:28 Comment(1)
So PyCharm doesn't use my "system" PYTHONPATH and instead uses the "user added" entries ant the end of PyCharm's Interpreter Paths settings, right? If that's so then I understand where that part of the path comes from. What I don't understand is where the other entires are coming from.Deon

© 2022 - 2024 — McMap. All rights reserved.