本文介绍了为什么PyCharm总是添加“内容根”到我的PYTHONPATH,什么PYTHONPATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑PyCharm如何确定Python用来定位模块和包的路径。



首先,当我取消选中设置(在Python控制台和我的运行配置),我仍然看到我的项目的目录 sys.path



例如,我有项目在'path问题'和'运行'包含

  import sys 
在sys.path中的p:
print p



我得到

  Users / Rax / Documents / Projects / pathproblem 
...(我的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

,即使我要求content root排除在路径之外:





这可以导致成功导入在典型部署中无法导入的模块(例如



如果我检查设置,我会得到两次:

  / Users / Rax / Documents / Projects / pathproblem 
...(我的PYTHONPATH中的其他内容)
/ Users / Rax / Documents / Projects / pathproblem
...

看来PyCharm总是



(1)如何使用PYTHONPATH我是否配置PyCharm,以便它(真的)不添加项目目录到包搜索路径?



,PYTHONPATH,对于PyCharm,不是我的系统PYTHONPATH,而是用户添加条目 - 事实上,令人困惑的Python解释器的路径设置的结束。



(2)PyCharm的PYTHONPATH从哪里来?






FWIW,PyCharm的Sphinx尊重内容根

解决方案

这应该是因为PyCharm默认将路径添加到路径。

我知道你不能。但是,您可以执行不将您的文件添加到内容路由的操作。但是,在向您展示如何做之前,让我解释一下为什么它将目录的当前根目录添加到其路径。当你打开python控制台:



                                                  



现在,我有一个名为 foo.py ,并且在该文件中有一个名为 bar 的函数,因此我可以轻松地将该函数导入我的控制台:





如上图所示,PyCharm 自动添加您当前的根目录。这应该没有什么惊喜,因为当你 cd 到一个目录,并运行像 python foo.py 隐含地将当前根添加到您的路径。要在python控制台中 ,PyCharm会添加当前的根。



为了避免这种情况, 您当前的根目录并将其标记为源根目录:



                 



因为您的来源根将不会添加到路径,您可以轻松地:



                    

这是来自您的解释器设置:



>



一般来说, 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:

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.

解决方案

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

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:

                                                

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:

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:

                

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

                   

It comes from your interpreter settings:

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

这篇关于为什么PyCharm总是添加“内容根”到我的PYTHONPATH,什么PYTHONPATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 03:11