我有一个目录结构

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

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

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

但是,pycharm抱怨它无法访问该模块。我如何教pycham解决引用?


当前回答

这里所有的东西我都试过两次,甚至更多。我最终解决了这个问题,做了一些我在网上从未见过的事情。如果你去>编辑器>文件类型的设置,在底部有一个“忽略文件和文件夹”行。在我的例子中,我忽略了“venv”,这是我的虚拟环境的名字。所以我去掉了venv;从目录列表中忽略,瞧!!我终于解决了这个问题。实际上,我在这个项目中遇到的所有导入问题都得到了解决。

BTW, I had installed each and every package using PyCharm, and not through a terminal. (Meaning, by going to Settings>Interpreter...). I had invalidated cache, changed 'Source Root', restarted PyCharm, refreshed my interpreters paths, changed interpreters, deleted my venv... I tried everything. This finally worked. Obviously there are multiple problems going on here with different people, so this may not work for you, but it's definitely worth a shot if nothing else has worked, and easy to reverse if it doesn't.

其他回答

在新版本的pycharm中,只需右键单击想要导入文件的目录或python包,然后单击“标记目录为”->“源根”

这里所有的东西我都试过两次,甚至更多。我最终解决了这个问题,做了一些我在网上从未见过的事情。如果你去>编辑器>文件类型的设置,在底部有一个“忽略文件和文件夹”行。在我的例子中,我忽略了“venv”,这是我的虚拟环境的名字。所以我去掉了venv;从目录列表中忽略,瞧!!我终于解决了这个问题。实际上,我在这个项目中遇到的所有导入问题都得到了解决。

BTW, I had installed each and every package using PyCharm, and not through a terminal. (Meaning, by going to Settings>Interpreter...). I had invalidated cache, changed 'Source Root', restarted PyCharm, refreshed my interpreters paths, changed interpreters, deleted my venv... I tried everything. This finally worked. Obviously there are multiple problems going on here with different people, so this may not work for you, but it's definitely worth a shot if nothing else has worked, and easy to reverse if it doesn't.

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

如果你正在开发一个django项目,并且pyCharm中没有安装django插件,它会显示错误“unresolved reference”。 参考: https://www.jetbrains.com/pycharm/help/resolving-references.html

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

点击:File > Settings > (Project: your Project name) > Project Interpreter >

然后点击右边的“+”图标,搜索你想要的软件包并安装它。

享受编码吧!!

一般来说,这是一个丢失包的问题,只需在未解决的引用处放置插入符号,并按Alt+Enter显示选项,然后您应该知道如何解决它。