Pycharm: "unresolved reference" error on the IDE when opening a working project
Solution 1
The key is to mark your source directory as a source root. Try the following:
- In the Project view, right-click on the Python source directory
- In the dialog menu select Mark Directory As > Source Root
The folder should now appear blue instead of beige, to indicate it is a Python source folder.
You can also configure this in PyCharm preferences by doing the following for a project that is already in PyCharm:
- In the Mac toolbar, select PyCharm > Preferences
- In the window that opens, select Project Structure from the menu pane on the left
- Select your project in the middle pane, if necessary
- Right-click on your Python source in the right pane and select Sources from the menu dialog
Solution 2
I also had the problem, and it took me few hours to find the exact solution.
You need to confirm the following things.
'django.contrib.staticfiles'
, is added toINSTALLED_APPS
in thesettings.py
file of your application.The directory with the static contents (for example, images), named
static
, resides under the application root.
Now Do the following
PyCharm > Preferences > Project Settings > Django
Make sure your Django Project root
, Settings.py
and manage.py
script are well defined in the dialog box.
You are good to go. Hope this helps.
Solution 3
I did all the stuff above from einnocent and myildirim but still had to do the following:
close pycharm and manually delete the .idea folder, this deletes everything pycharm knows about the code.
open pycharm, reimport the project
the combination of setting the correct source root, restarting python with invaliding cache and deleting the .idea folder / reimporting the pycharm project fixed it for me.
Solution 4
PyCharm > select "File" menu > select "Invalidate Caches / Restart" menu option
Solution 5
I faced similar issue with pyspark ( spark 2.1 ) & luigi.
Failed tries:
- Setting PYTHONPATH environment variable
- Invalidate cache & restart pycharm
- Mark directory as Source Root
Unresolved reference pyspark
could be fixed by adding the spark's python directory as Content Root in the project, but running the project as luigi task gave the same error.
Successfull steps
- Create an empty
__init__.py
file in the project Include following lines of code at the top of script
... import sys sys.path.append("/path/to/spark/python") sys.path.append("/path/to/spark/python/lib") ... // import pyspark files after above mentioned lines
and the Unresolved reference error issue was fixed both in pycharm & luigi task.
Sergio Ayestarán
Distributed systems Cassandra/Redis/PostgreSQL/Custom NoSQL databases Custom map/reduce algorithms Web crawlers Design and setup of fault tolerant platforms for distributed systems
Updated on July 09, 2022Comments
-
Sergio Ayestarán almost 2 years
Intro
I have a Python project on a git repository. Everything works ok for most of the team members, we can sync the code and edit it without any problem with Pycharm on different platforms (Windows, Linux)
The problem
On one of the computers we are getting "Unresolved reference" all over the code on almost every import with the exception of Python's built in libraries (i.e. import datetime is working). This computer is running the Mac version of Pycharm.
The question
Anyone knows how to solve this?, since most of the imports are not recognized code completion and navigation trough goto->declaration and so on is not working. Is there any known issue with the Mac version?
Thanks in advance!
-
Mr_and_Mrs_D over 10 yearsThen restart the IDE... - oops not even this worked. Why can't people write an IDE that works, why ?
-
myildirim about 10 yearsYou should; " File -> Invalidate Caches / Restart... -> Invalidate and Restart " it worked for me. @Mr_and_Mrs_D
-
Mr_and_Mrs_D about 10 years@myildirim: thanks - will check this if I ever go back
-
Sergio Ayestarán almost 10 yearsThanks for the help, unfortunately in my case it wasn't a django project so this method doesn't apply. Probably it will help others as an alternative tho.
-
A.J. almost 10 yearsCool, Thanks, Rock and Roll!!
-
psychok7 almost 10 yearsi still have the same problem... it happens mostly with third party apps
-
J4cK almost 10 years@Clayton How does entire settings.py file look like ? What exactly you changed ?
-
snowbound almost 9 yearsInvalidate Caches / Restart fixed it for me. Thanks!
-
Simon about 8 yearsi wanted to add that to the answer of einnocent since it would have fit there really well. without the reputation to comment, i posted it as a seperate answer.
-
user2682863 almost 8 yearsThis answer worked for me. Seems like a perfectly reasonable answer.
-
edilio almost 6 yearsJust be aware that you can only add one
Content Root
but you can add severalSource Folders
. -
user238607 over 5 yearsI tried to do that. But it says that content roots shouldn't intersect. Doesn't allow it to add the sources.
-
J-Schaefer over 2 yearsUnfortunately I still have the same issue 8 years after the original question. And manually deleting the
.idea/
folder and remarking the source folders works. But there is still no better solution to this?