33
votes

I'm trying to import:

from django.db import models

PyCharm underlines django.db and complains: Unresolved reference 'django'.

How do I get PyCharm to recognize Django?

12
You might have to set which python environment you're using in pycharm. Are you using virtualenv? - anupsabraham
Normally, selecting your interpreter from File -> Settings -> Project Interpreter should work. If not listed, add it. - anupsabraham

12 Answers

43
votes

I assume you're using a virtualenv which is located in the same directory as other project files. Python should know exactly that path. So, it's possibly that Pycharm is using the wrong Interpreter.

Go to Settings -> Project: -> Project Interpreter -> switch to the right path

In the interpreter packages, there should be Django package installed. If not, do it here/in terminal.

18
votes

Mark root folder of project as 'Sources root', it works for me. Content Root

17
votes

Above answers are answered partially.

Problem Description: I've multiple versions of python installed and Django is installed in one of Python version. There are two issues

  • Issue 1: Pycharm has wrong python interpreter. For this the Project Interpreter has to be changed to Python version where Django is installed. Solution: Follow all steps.

  • Issue 2: Django isn't listed as package. For this we need make available the installed python packages in the Pycharm environment. Solution: Follow all steps except step 3.

Solution: Peform following steps.

  1. In preference/settings go to Project > Project Interpreter
  2. On right hand side click on settings icon > Add Local
  3. Select correct Python version from Base Interpreter
  4. Mark the checkbox against Inherit global site-packages and Make available to all projects
  5. Click ok

Once this is done in Project Intepreter you will be able to see Django in the Package list.

6
votes

If you create the project use python2.7, and in python2.7 path you installed the django, the project works normal. Then you switch the Project Interpreter to the python3.5, but this path did not install the django, then you will get this issue.

enter image description here

5
votes

I got this problem and it stopped my unit tests from running.

I found that PyCharm, during an upgrade, had set my Python Interpreter to one which was in a virtual environment, which I do not use any more for this project. So I set it to my global Python environment in C:\Python and everything works like a charm.

I hope this will help someone.

4
votes

if you have installed Django successfully before, in a different location from project location: in pycharm go to the setting>Project>project interpreter. right side of the Project Interpreter, click on setting icon and choose add local.

then in New Environment check these two checkbox:

  • inherit global site-packages
  • make available to all projects

then press Ok and Apply button and wait installing the interpreter to your project location.

4
votes

You can make pyCharm recognize it by setting it to use your virtualenv setup that I assume you had it already for the project

STEP 1: open preferences PyCharm > Preferences open preferences

STEP 2: Search for interpreter and on the right window, select your virtual environment to be used in Project Interpreter select interpreter

STEP 3 (ADDITIONAL): adding your environment to the list if it didn't show up

  1. select show all interpreter, and then on the pop up window, click + icon on the bottom left show all box

  2. select radio button Existing Environment and select your virtual env, and then check "Make available for all project" if you use the env for other project, and click ok add existing environment

STEP 4: On the preferences window, click apply or straight click the ok button. wait couple second and django variable should be available in your project

2
votes

Add this site-packages works for me. (On 20191218, using the newest version of PyCharm ,and python3.7.5 ) enter image description here

2
votes

Menu -> Invalidate Caches / Restart -> Invalidate and Restrart

1
votes

I had this problem too. In fact, I faced with this problem because django package had not been installed in Pycharm. Therefore, I went to this path and clicked on plus sign;then, I searched django and installed it. It worked well.

file>setting>project>project interpreter

0
votes

I used virtualenv in my project and the following steps is works for me.

Settings -> Project:XXX -> Project Interpreter --> click the right side icon next to project interpreter -> more -> select the virtualenv interpreter

hope it can help someone

-1
votes

I fix this issue by changing "Project Structure". Try to unmark parent folder as "Sources"