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

问题描述

我对 PyCharm 如何确定 Python 用来定位模块和包的路径感到困惑.

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

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

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

我明白了

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

似乎 PyCharm 总是 在 PYTHONPATH 的开头(它认为是什么)添加当前项目根目录,并且这个设置只是将它再次添加到结束.

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) 如何配置 PyCharm 使其(真的)不会将项目目录添加到包搜索路径中?

此外,据我所知,对于 PyCharm,PYTHONPATH 根本不是我的系统 PYTHONPATH,而是用户添加"的条目——事实上,令人困惑的是,在 Python 的路径设置的末尾口译员.

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) PyCharm 的 PYTHONPATH 来自哪里?这不是我在系统其他任何地方看到的 PYTHONPATH.

FWIW,PyCharm 的 Sphinx 尊重内容根"设置,仅在构建配置中检查路径时添加内容根.

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

推荐答案

应该是因为 PyCharm 默认将项目根添加到 Path 中.

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

(1) 如何配置 PyCharm 使其(真的)不会将项目目录添加到包搜索路径中?

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

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:

                                               

                                                

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

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:

如上图所示,PyCharm 自动添加您当前的根目录.这应该不足为奇,因为当您 cd 进入一个目录并运行类似 python foo.py 之类的东西时,您隐式添加了当前根到你的路径.为了在 python 控制台中模拟这个,PyCharm 添加了当前的根目录.

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:

                

                

而且由于您的 Source 根不会添加到路径中,您可以高枕无忧:

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

                   

                   

(2) PyCharm 的 PYTHONPATH 来自哪里?这不是我在系统其他任何地方看到的 PYTHONPATH.

它来自您的解释器设置:

It comes from your interpreter settings:

一般来说,PYTHONPATH 本质上是包含您的标准库文件以及您的第三方库文件的所有目录的集合.

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 总是添加“contents root"?到我的 PYTHONPATH,什么 PYTHONPATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:58