Unresolved reference issue in PyCharm

2019-01-02 16:46发布

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

11条回答
君临天下
2楼-- · 2019-01-02 16:51

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.

查看更多
几人难应
3楼-- · 2019-01-02 16:54

pycharm uses venv ,in the venv u should install the packages explicitly or goto settings ->project interpreter ->add interpreter ->inherit global site-packages

查看更多
后来的你喜欢了谁
4楼-- · 2019-01-02 16:56

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!

查看更多
牵手、夕阳
5楼-- · 2019-01-02 16:57
  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
查看更多
步步皆殇っ
6楼-- · 2019-01-02 17:01

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

pycharm prefrence

查看更多
浅入江南
7楼-- · 2019-01-02 17:01

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

查看更多
登录 后发表回答