我有一个目录结构
├── simulate.py
├── src
│ ├── networkAlgorithm.py
│ ├── ...
我可以使用sys.path.insert()访问网络模块。
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
但是,pycharm抱怨它无法访问该模块。我如何教pycham解决引用?
我有一个目录结构
├── simulate.py
├── src
│ ├── networkAlgorithm.py
│ ├── ...
我可以使用sys.path.insert()访问网络模块。
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
但是,pycharm抱怨它无法访问该模块。我如何教pycham解决引用?
当前回答
对我来说,将virtualenv (venv)的site-packages路径添加到解释器的路径中是可行的。 终于!
其他回答
在我的例子中,使用Pycharm 2019.3,问题是我忘记向我想导入的文件添加扩展名“.py”。一旦添加,错误就会消失,而不需要使缓存无效或任何其他步骤。
我在安装aigram时遇到了一个重要问题。与此同时,机器人工作了,但pyCharm用红色突出显示导入,并且没有给出提示。以上这些方法我都试过很多次了。结果,以下操作帮助了我:我在以下路径找到了aiogram文件夹 Python c…\ AppData \ \用户漫游\ \ Python310 \网站 然后复制到文件夹里 C:\Program Files\Python310\Lib\网站 在那之后,我重置pyCharm,就是这样!
在PyCharm中右键单击文件所在的目录 转到——>标记目录为 选择——>源根
你的问题会解决的
如果你有python解释器不安装的问题,请注意 包,只需更改文件夹PycharmProjects的权限 C: \用户'username \ PycharmProjects 致每一个人
这里所有的东西我都试过两次,甚至更多。我最终解决了这个问题,做了一些我在网上从未见过的事情。如果你去>编辑器>文件类型的设置,在底部有一个“忽略文件和文件夹”行。在我的例子中,我忽略了“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.