Pycharm: "unresolved reference" error on the IDE when opening a working project

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!


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

I also had the problem, and it took me few hours to find the exact solution.

You need to confirm the following things.

  1. 'django.contrib.staticfiles', is added to INSTALLED_APPS in the settings.py file of your application.

  2. 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.


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.


I faced similar issue with pyspark ( spark 2.1 ) & luigi.

Failed tries:

  1. Setting PYTHONPATH environment variable
  2. Invalidate cache & restart pycharm
  3. 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.


My ten-penneth - If you're working with virtual environments (have a venv directory) make sure it is marked as excluded.

Plagiarised from above:

  • In the Project view, right-click on the venv directory
  • In the dialog menu select Mark Directory As > Excluded

The directory should turn orange...

This sometimes happens if you've done a git clean or for whatever reason had to rebuild your environment and PyCharm hasn't noticed