我有一个目录结构
├── simulate.py ├── src │ ├── networkAlgorithm.py │ ├── ...我可以使用 sys.path.insert() 访问网络模块.
And I can access the network module with sys.path.insert().
import sys import os.path sys.path.insert(0, "./src") from networkAlgorithm import *但是,pycharm 抱怨它无法访问该模块.如何教 pycham 解析引用?
However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?
推荐答案手动添加它是确实是这样做的一种方法,但有一个更简单的方法,那就是只需告诉 pycharm 您要将 src 文件夹添加为源根目录,然后将源根目录添加到您的 python 路径.
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:
- 添加 src 作为源内容根目录:
- Add src as a source content root:
然后确保将添加源添加到您的 PYTHONPATH 下:
- 现在将解决导入问题:
这样,您可以添加任何您想要的作为源根目录,并且一切都会正常工作.但是,如果您将其取消标记为源根目录,您将收到错误消息:
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:
毕竟不要忘记重新启动.在 PyCharm 菜单中选择:文件 -->使缓存无效/重新启动
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
更多推荐
PyCharm 中未解决的参考问题
发布评论