Reputation: 190659
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?
Upvotes: 563
Views: 665030
Reputation: 170
In my case, with Pycharm 2019.3, the problem was that I forgot to add the extension '.py' to the file I wanted to import. Once added, the error went away without needing to invalides caches or any other step.
Upvotes: 0
Reputation: 1
I encountered an import problem when installing aiogram. At the same time, the bot worked, but pyCharm highlighted the import in red and did not give hints. I've tried all of the above many times.As a result, the following helped me: I found the aiogram folder at the following path c\Users...\AppData\Roaming\Python\Python310\site-packages and copied it to the folder C:\Program Files\Python310\Lib\site-packages After that, I reset pyCharm and that's it!
Upvotes: 0
Reputation: 974
This problem also appears if you use a dash within the Python filename, which therefore is strongly discouraged.
Upvotes: 0
Reputation: 2704
__init__.py
file in src
foldersrc
folder as a source rootPYTHONPATH
(see above)Upvotes: 82
Reputation: 1
just note if u have a problem with python interpreter not installing packages, just change the permission for folder PycharmProjects C:\Users'username'\PycharmProjects to every one
Upvotes: 0
Reputation: 620
The project I cloned had a directory called modules
and was successfully using files from there in the code with import this as that
, but Pycharm was unable to jump to those code fragments because it did not recognise the imports.
Marking the module folder in the following settings section as source
solved the issue.
Upvotes: 7
Reputation: 11
I had the same problem and also try so many suggestions but none of them worked, until I find this post (https://stackoverflow.com/a/62632870/16863617). Regardless his solution didn't work for me, it helped me to came up with the idea to add _init.py_ into the --> Settings | Editor | File Types | Python | Registered patterns
And the unresolved reference error is now solved.
Upvotes: 0
Reputation: 3855
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea
issue causing the IDE to show error while the code still runs correctly. Solution:
.idea
folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Editor not marking init.py as Python but as Text
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py
files not to be python files, and thus ignores them during code analysis. To fix this:
__init__.py
to the list of python filesor
__init__.py
from the list of text filesUpvotes: 9
Reputation: 1112
For my case :
Directory0
├── Directory1
│ └── file1.py
├── Directory2
│ ├── file2.py
Into file1, I have :
from Directory2 import file2
which trows an "unresolved reference Directory2".
I resolved it by:
AND
It is silly but if I don't do the second action, the error still appears and can make you think that you didn't resolve the issue by marking the parent directory as Source Root.
Upvotes: 1
Reputation: 82440
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
as a source content root:
Then make sure to add add sources to your PYTHONPATH
under:
Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console
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:
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
Upvotes: 1172
Reputation: 21
I was also using a virtual environment like Dan above, however I was able to add an interpreter in the existing environment, therefore not needing to inherit global site packages and therefore undo what a virtual environment is trying to achieve.
Upvotes: 2
Reputation:
your problem will be solved
Upvotes: 2
Reputation: 2233
I tried everything here twice and even more. I finally solved it doing something I hadn't seen anywhere online. If you go to Settings>Editor>File Types
there is an 'Ignore Files and folders
' line at the bottom. In my case, I was ignoring 'venv'
, which is what I always name my virtual environments. So I removed venv;
from the list of directories to ignore and VOILA!! I was FINALLY able to fix this problem. Literally all of my import problems were fixed for the project.
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.
Upvotes: 1
Reputation: 6455
For me, adding virtualenv (venv
)'s site-packages path to the paths of the interpreter works.
Finally!
Upvotes: 0
Reputation: 11
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.
Upvotes: 1
Reputation: 151
Although all the answers are really helpful, there's one tiny piece of information that should be explained explicitly:
.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!
Upvotes: 6
Reputation: 9
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 !!!
Upvotes: -1
Reputation: 261
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'
Upvotes: -2
Reputation: 3054
This worked for me: Top Menu -> File -> Invalidate Caches/Restart
Upvotes: 3
Reputation: 493
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.
Upvotes: 1
Reputation: 2545
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
.
Upvotes: -1
Reputation: 483
After following the accepted answer, doing the following solved it for me:
File
→ Settings
→ Project <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!
Upvotes: 3
Reputation: 56936
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.
Upvotes: 3
Reputation: 975
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
Upvotes: 35
Reputation: 364
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.
Upvotes: 4
Reputation: 227
After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies
and re-arrange them.
Upvotes: 21
Reputation: 1059
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).
Upvotes: 1
Reputation: 403
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
Upvotes: 2
Reputation: 190659
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
Upvotes: 15