简体   繁体   English

PyCharm 和 PYTHONPATH

[英]PyCharm and PYTHONPATH

I have a directory that I use for my PYTHONPATH : c:\test\my\scripts\ .我有一个用于PYTHONPATH的目录: c:\test\my\scripts\ In this directory, I have some modules I import.在这个目录中,我有一些我导入的模块。 It works well in my Python shell.它在我的 Python shell 中运行良好。

How do I add this directory path to PyCharm, so I can import what is in that directory?如何将此目录路径添加到 PyCharm,以便我可以导入该目录中的内容?

Out of data, see Duane's answer below.数据不足,请参阅下面 Duane 的回答。

You need to go to the Main PyCharm Preferences , which will open up a separate window.您需要转到Main PyCharm Preferences ,它将打开一个单独的窗口。 In the left pane, choose Project:... > Project Interpreter .在左窗格中,选择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 More or Show All in 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.通过单击“加号”符号添加您的路径。

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'.最好在您要添加的模块的每个子文件夹中包含__init__.py ,并将项目文件夹设为“Source Root”。 Simply right-click on the folder in the path bar and choose 'Mark Directory as ...'只需右键单击路径栏中的文件夹,然后选择“将目录标记为...”

For Pycharm Community 2019.3对于 Pycharm 社区 2019.3

  • File文件
  • Settings设置
  • Project项目
  • Project Interpreter项目口译员
  • Cog in top right => Show all右上角的齿轮 => 显示全部
  • Select the interpreter选择口译员
  • On the right toolbar, there are 5 icons, bottom one looks like a folder tree,在右侧工具栏上,有 5 个图标,底部一个看起来像一个文件夹树, 文件夹树 click it点击它
  • Press plus +按加号 +
  • Select directory, press OK选择目录,按确定

In PyCharm Community 2019.2/2019.3 (and probably other versions), you can simply:在 PyCharm Community 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.可以如此标记任意数量的文件夹。

enter image description here在此处输入图像描述

you can go to setting in pycharm and set interpreter python你可以去设置pycharm并设置解释器python

The following answer is for PyCharm 2022.1.3+ (Professional Edition).以下答案适用于 PyCharm 2022.1.3+(专业版)。

  1. Open up Preferences (mac: + , )打开首选项(mac: + , ) 在此处输入图像描述

  2. Click on Project and then Project Interpreter单击项目,然后单击项目解释器在此处输入图像描述

  3. Click on the three vertical dots and the on Show All...单击三个垂直点,然后单击显示全部...

在此处输入图像描述 在此处输入图像描述

  1. Choose your interpreter and then click on the directory structure icon选择您的解释器,然后单击目录结构图标在此处输入图像描述

  2. Click on the + icon and add the path you need to add to PYTHONPATH .单击+图标并将您需要添加的路径添加到PYTHONPATH 在此处输入图像描述

That's it!而已!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM