PyCharm 和 PYTHONPATH
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28326362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PyCharm and PYTHONPATH
提问by Trying_hard
I am new to PyCharm. I have a directory that I use for my PYTHONPATH: c:\test\my\scripts\
. In this directory I have some modules I import. It works fine in my Python shell.
我是 PyCharm 的新手。我有一个用于 PYTHONPATH 的目录:c:\test\my\scripts\
. 在这个目录中,我导入了一些模块。它在我的 Python shell 中运行良好。
How do I add this directory path to PyCharm so I can import what is in that directory?
如何将此目录路径添加到 PyCharm 以便我可以导入该目录中的内容?
回答by Hendrik F
Out of data, see Duane's answer below.
在数据之外,请参阅下面 Duane 的回答。
You need to go to the Main PyCharmPreferences, which will open up a separate window. In the left pane, choose Project:...> Project Interpreter. Now, in the main pane on the right, click the settings symbol (gear symbol) next to the field for "Project Interpreter". Choose Moreor Show Allin the menu that pops up. Now in the final step, pick the interpreter you are using for this project and click on the tree symbol at the bottom of the window (hovering over the symbol reveals it as "Show paths for the selected interpreter"). Add your path by click in the "plus" symbol.
您需要转到Main PyCharm Preferences,这将打开一个单独的窗口。在左窗格中,选择Project:...> Project Interpreter。现在,在右侧的主窗格中,单击“项目解释器”字段旁边的设置符号(齿轮符号)。在弹出的菜单中选择更多或全部显示。现在在最后一步中,选择您正在用于该项目的解释器并单击窗口底部的树符号(将鼠标悬停在该符号上会显示为“显示所选解释器的路径”)。通过单击“加号”符号添加您的路径。
It took me ages to find, so I hope the detailed instructions will help. Further details are available in the PyCharm docs.
我花了很长时间才找到,所以我希望详细的说明会有所帮助。PyCharm 文档中提供了更多详细信息。
It is good practice to have __init__.py
in each subfolder of the module you are looking to add, as well as making your project folder a 'Source Root'. Simply right-click on the folder in the path bar and choose 'Mark Directory as ...'
__init__.py
在您要添加的模块的每个子文件夹中都有一个很好的做法,并将您的项目文件夹设为“源根目录”。只需右键单击路径栏中的文件夹,然后选择“将目录标记为...”
回答by Duane
回答by alkalinity
In PyCharm Community 2019.2/2019.3 (and probably other versions), you can simply:
在 PyCharm 社区 2019.2/2019.3(可能还有其他版本)中,您可以简单地:
- right-click any folder in your project
- select "Mark Directory As"
- select "Sources Root"
- 右键单击项目中的任何文件夹
- 选择“将目录标记为”
- 选择“源根”
Modules within that folder will now be available for import. Any number of folders can be so marked.
该文件夹中的模块现在可用于导入。可以标记任意数量的文件夹。