Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 416
orome Avatar asked Jan 18 '14 02:01

orome


People also ask

What is content root in PyCharm?

A content root is a folder that contains the files that make up your project. Source roots (or source folders; shown as ). These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.

What is default Pythonpath?

/usr/local/bin/python is the default path of the Python directory.

How do I set Pythonpath environment variable in PyCharm?

Press Ctrl+Alt+S to open the IDE settings and select Appearance & Behavior | Path Variables. and enter the name of the new variable (for example, DATA_PATH ) and its value that points to the target directory with the data file on your disk.


2 Answers

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.

like image 72
Games Brainiac Avatar answered Sep 30 '22 05:09

Games Brainiac


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.)

like image 31
yole Avatar answered Sep 30 '22 07:09

yole