Unresolved Reference Errors for django

Hi there – I've been using PyCharm for a while without any issues, but today I'm running into unresolved reference errors for django in my all my files and I can't figure out why.

More info:
  • I'm using a virtualenv
  • The application launches and runs without any errors
  • PyCharm is popping up the "package requirements are not satisfied" message when I first open the project (and one of the requirements it's saying isn't satisfied is Django), but the requirements are definitely installed, and when I go to the settings screen for the project in PyCharm to install them, it shows them as installed.

I'm starting to think it might be an issue with how I have the project set up, either the directory structure or maybe the fact that I'm using multiple settings files.

As far as the directory structure goes, I have a top-level directory that is my git repo root. Within that I have requirements.txt. Then inside that directory (i.e. a directory at the same level as requirements.txt) I have the root of the Django project – here's the high-level layout:

foo-project
    requirements.txt
    foo
        foo
            settings.py
            settings
base.py
local.py
production.py
staging.py
        manage.py

This is the project layout proposed in Two Scoops of Django if you're familiar with that book.

As far as how I fired this up as a project in PyCharm, I did File -> Open Directory and pointed to the root of the git repo as opposed to the root of the Django app itself.

So my first question is, and this may be at the heart of the unresolved reference error, why is PyCharm insisting that I don't have my package requirements satisfied when I do? Would that cause the unresolved reference errors? Do I need to move my requirements.txt file elsewhere, or is there a way to point PyCharm directly to it?

My second question is if this is related to the fact that I'm using multiple settings files? Each of the environment-specific settings files imports everything from the base file (from .base import *). In the PyCharm Python interpreter settings for this project I added –settings=foo.settings.local in the Additional options box. And when I launch the app from PyCharm, it very clearly says "Django version 1.5.1, using settings 'foo.settings.local'" in the console.

Note that I have not run into this issue in projects with the requirements.txt file in the same directory as manage.py and in which I use a single settings.py file, but before giving up on what I think is a much better project structure I figured I better ask what might be going on here.

Happy to provide more information to help troubleshoot. Thanks in advance for any ideas people might have.
13 comments
Are you sure that your Django requirement is satisfied? Does the requirement string in requirements.txt include versions?

You can check that your Python packacking tools recognize the installation of Django by running this unit test in PyCharm for your virtualenv (you should change REQUIRED_PACKAGE and REQUIREMENTS_FILE accordingly).

What references are unresolved for you?
0
Thanks Andrey – as far as the Django requirement being satisfied, PyCharm seems to think it's not but the application runs fine. The requirements.txt does include versions. The odd thing is I get the message at the top of PyCharm saying requirements are not satisifed, but if I go to File -> Settings and click on the Python interpreter being used, it shows everything as being installed (i.e. it shows the correct versions of everything that I have in my requirements.txt).

The major reference that is showing as unresolved is Django.

I'll check the unit test and see what that yields – thanks for pointing me to that!
0
The unresolved import django means that PyCharm doesn't see your Django installation in the Python path for the current Python interpreter (virtualenv). Try File | Invalidate Caches. Also try installing Django using the Install quick-fix for "Package requirements not satisfied".
1
Thanks – actually tried both of those things yesterday and no luck.

Unit test passes.
0
Here's what File -> Settings -> Python Interpreters looks like in PyCharm for the virtualenv.
requirements.png
0
If you will share your project (or its stripped down version that still has this problem) then I take a closer look at it.
0
Thanks – I'll strip out the sensitive info and attach a zip in a bit.
0
Interesting – did the following:
  • created a copy of the project (just did a cp -R foo foonew) and stripped out the sensitive info
  • nuked .idea directories
  • created new virtualenv
  • didn't run pip install -r requirements.txt from a terminal
  • did File -> Open Directory in PyCharm
  • set up project and virtualenv
  • got message about requirements not being satisfied and installed the packages from PyCharm (which is different from the other project, wherein I installed into the virtualenv via terminal)

The new project created in this way works fine. So what I'll do is wipe out my virtualenv and start over on the other project and see if that resolves it.
0
Is there a way to force PyCharm to read the requirements.txt or tell it where it's located for a project? For some reason in the original project even after deleting and recreating the virtualenv, deleting the .idea directory, and opening the directory again in PyCharm, it's not seeing that I don't have the packages indicated in requirements.txt installed. This is different behavior from the copy I did to do the testing I outlined earlier.

I can just blow this away entirely and start over but thought I'd ask about why PyCharm doesn't seem to be seeing/reading requirements.txt before I do that.
0
You can set up the path to requirements.txt in Preferences | Python Integrated Tools | Package requirements file.
0
This may sound crazy but I think I may have it nailed down to using hyphens in the virtualenv directory name. That turned out to be the only difference between the project that didn't work and the copy of it that did.

With hyphens in the directory name for the virtualenv, even when I did get the "Package requirements ... are not satisfied" message (and whether or not that would ever show up was inconsistent), there was no "Install requirements" link. Only the "Ignore requirements" link showed up, and if clicked on the message itself and chose "fix all" nothing happened. No errors or anything, nothing happened.

When I kept the project the same but created a new virtualenv with no hyphens in the directory name and pointed to that, it behaved as I would expect – got the requirements not satisfied message, the "install requirements" link appeared, and I could successfully install the requirements. Also, with no hyphen in the directory name I could install the requirements via terminal and PyCharm would recognize them.

I realize that for Django projects there can't be hyphens in the project name, but is this maybe a PyCharm issue where it isn't handling the virtualenv properly due to the hyphen?

I'm going to run through all this one more time to make sure that's the reason, but it certainly seems to be the case based on what I've been banging on thus far.
0
Ran through this all again and that's definitely the issue. Not sure if that's expected or a bug, but at a minimum it would have been nice to have PyCharm throw an "Invalid virtualenv path" error or something along those lines as opposed to simply behaving strangely.

Thanks so much for the help as I worked through this. All is well now.
0
I cannot reproduce the problem with virtualenv directories that contain hyphens. Maybe it depends on the operating system or on the location of the virtualenv directory (under the project directory, in the hidden ~/.virtualenvs/ directory, excluded from Project paths or not, etc.).

Could you please file a bug in our issue tracker and describe your configuration in detail? A small sample project attached to the issue would be very welcome.
0

Please sign in to leave a comment.