问题:PyCharm中未解决的参考问题

我有一个目录结构

├── simulate.py
├── src
   ├── networkAlgorithm.py
   ├── ...

而且我可以使用访问网络模块sys.path.insert()

import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *

但是,pycharm抱怨它无法访问该模块。我如何教pycham解析参考文献?

在此处输入图片说明

I have a directory structure

├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...

And I can access the network module with sys.path.insert().

import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *

However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?

enter image description here


回答 0

手动将其添加为你所做确实这样做的一种方式,但有一个简单的方法,那就是通过简单地告诉pycharm要在添加src文件夹作为源根目录,然后添加源根你的Python路径。

这样,您就不必将代码硬编码到解释器的设置中:

  • src为源内容根:

在此处输入图片说明

  • 然后确保将添加源添加到您的 PYTHONPATH

在此处输入图片说明

  • 现在将解决进口问题:

在此处输入图片说明

这样,您可以将任何所需的内容添加为源根,一切将简单地进行。但是,如果未将其标记为源根,则会出现错误:

在此处输入图片说明

毕竟,别忘了重新启动。在PyCharm菜单中,选择:文件->使缓存无效/重新启动

Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.

This way, you don’t have to hard code things into your interpreter’s settings:

  • Add src as a source content root:

enter image description here

  • Then make sure to add add sources to your PYTHONPATH:

enter image description here

  • Now imports will be resolved:

enter image description here

This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:

enter image description here

After all this don’t forget to restart. In PyCharm menu select: File –> Invalidate Caches / Restart


回答 1

  1. 检查__init__.py文件src夹文件
  2. 添加 src文件夹为源根
  3. 然后确保将添加源添加到您的 PYTHONPATH(见上文)
  4. 在PyCharm菜单中选择:File-> Invalidate Caches / Restart
  1. check for __init__.py file in src folder
  2. add the src folder as a source root
  3. Then make sure to add add sources to your PYTHONPATH (see above)
  4. in PyCharm menu select: File –> Invalidate Caches / Restart

回答 2

如果有人还在看,我尝试使用PyCharm 2016.3时,可接受的答案仍然有效。用户界面可能已更改,但选项仍然相同。

即。右键单击您的根文件夹->“标记目录为”->源根

If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.

ie. Right click on your root folder –> ‘Mark Directory As’ –> Source Root


回答 3

测试完所有变通办法之后,建议您Settings -> Project -> project dependencies重新看看并重新安排它们。

pycharm偏好

After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.

pycharm prefrence


回答 4

通常,$ PYTHONPATH用于教python解释器查找必要的模块。PyCharm需要在“首选项”中添加路径。

在此处输入图片说明


回答 5

通常,这是缺少包装的问题,只需将插入号放在未解决的参考上并按Alt+Enter以显示选项,那么您应该知道如何解决。

Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.


回答 6

遵循接受的答案后,请执行以下操作为我解决此问题:

File→交通Settings→交通Project <your directory/project>→交通Project Dependencies

选择您的未解析导入文件所在的目录/项目,并选中相应的框以告知Pycharm该项目取决于您的其他项目。

我的文件夹层次结构与问题中的文件夹层次结构略有不同。我的就是这样

├── MyDirectory  
     └── simulate.py  
├── src  
     ├── networkAlgorithm.py  
     ├── ...

告诉Pycharm src依赖于MyDirectory我,此问题已解决!

After following the accepted answer, doing the following solved it for me:

FileSettingsProject <your directory/project>Project Dependencies

Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.

My folder hierarcy is slightly different from the one in the question. Mine is like this

├── MyDirectory  
│     └── simulate.py  
├── src  
│     ├── networkAlgorithm.py  
│     ├── ...

Telling Pycharm that src depends on MyDirectory solved the issue for me!


回答 7

尽管所有答案都确实有帮助,但是应该明确解释其中一小部分信息:

  • 本质上,具有多个分层目录的项目将作为具有某些属性的包来工作。
  • 要导入本地创建的自定义类,我们需要导航到包含.py文件的目录并创建一个__init__.py(空)文件。

之所以有帮助,是因为该文件是使Python将目录视为包含包所必需的。干杯!

Although all the answers are really helpful, there’s one tiny piece of information that should be explained explicitly:

  • Essentially, a project with multiple hierarchical directories work as a package with some attributes.
  • To import custom local created Classes, we need to navigate to the directory containing .py file and create an __init__.py (empty) file there.

Why this helps is because this file is required to make Python treat the directory as containing packages. Cheers!


回答 8

通过PyCharm安装(与Community Edition一起使用)。打开,Settings > Project > Project Interpreter然后单击下面的屏幕快照中的绿色+图标。在打开的第二个对话框中,输入软件包名称,然后单击“安装软件包”按钮。

在此处输入图片说明

Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the ‘Install Package’ button.

enter image description here


回答 9

很多时候发生的事情是没有安装插件。例如

如果您正在开发django项目,并且在pyCharm中未安装django插件,则会显示错误“未解决的引用”。请参阅:https : //www.jetbrains.com/pycharm/help/resolving-references.html

Many a times what happens is that the plugin is not installed. e.g.

If you are developing a django project and you do not have django plugin installed in pyCharm, it says error ‘unresolved reference’. Refer: https://www.jetbrains.com/pycharm/help/resolving-references.html


回答 10

请检查您是否使用了正确的解释器。我遇到错误“无法解析的参考’django’”以解决此问题,我从项目设置中更改了项目解释器(将Python 3更改为Python 2.7):选择项目,转到文件->设置->项目:->项目解释器->浏览并选择正确的版本或解释器(例如/usr/bin/python2.7)。

Please check if you are using the right interpreter that you are supposed to. I was getting error “unresolved reference ‘django’ ” to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings: Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).


回答 11

就我而言,问题是我正在使用 Virtual environment是无法访问全局站点程序包的。因此,解释器不知道新安装的软件包。

要解决此问题,只需编辑或重新创建您的虚拟解释器,然后勾选该Inherit global site-packages选项。

在此处输入图片说明

In my case the problem was I was using Virtual environment which didn’t have access to global site-packages. Thus, the interpreter was not aware of the newly installed packages.

To resolve the issue, just edit or recreate your virtual interpreter and tick the Inherit global site-packages option.

enter image description here


回答 12

这为我工作: Top Menu -> File -> Invalidate Caches/Restart

This worked for me: Top Menu -> File -> Invalidate Caches/Restart


回答 13

在PyCharm 2019.3.1中完成。 右键单击src文件夹->“标记目录为”->单击“排除”,您的src文件夹应为蓝色。

Done in PyCharm 2019.3.1 Right-click on your src folder -> “Mark Directory as” -> Click-on “Excluded” and your src folder should be blue.


回答 14

Pycharm使用venv。在venv的控制台中,您应该显式安装软件包或进入settings -> project interpreter -> add interpreter -> inherit global site-packages

Pycharm uses venv. In the venv’s console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.


回答 15

在较新版本的pycharm中,您可以简单地执行以下操作:右键单击要从中导入文件的目录或python包,然后单击“将目录标记为”->“源根”

In newer versions of pycharm u can do simply by right clicking on the directory or python package from which you want to import a file, then click on ‘Mark Directory As’ -> ‘Sources Root’


回答 16

解决此问题的最简单方法是在pyCharm软件中执行以下操作:

单击:文件>设置>(项目:您的项目名称)>项目解释器>

然后单击右侧的“ +”图标以搜索所需的软件包并安装。

享受编码!

The easiest way to fix it is by doing the following in your pyCharm software:

Click on: File > Settings > (Project: your project name) > Project Interpreter >

then click on the “+” icon on the right side to search for the package you want and install it.

Enjoy coding !!!


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。