36

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?

alex
  • 6,818
  • 9
  • 52
  • 103
m4jesticsun
  • 1,799
  • 3
  • 12
  • 12

12 Answers12

48

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.

30

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

Igor Z
  • 601
  • 6
  • 7
  • 2
    I had the proper virtualenv selected with django installed, and still my applications were showing as 'unresolved references'. From `File>Settings>Project Structure` setting the project root folder as a "Source" resolved the issue. (As mentioned here) – monkut Feb 27 '19 at 15:00
  • 1
    Works for me... with variation for PyCharm 2019.1 ... PyCharm / Preferences / Project : / Project Structure ... then mark your folder as Sources – user1546081 Jul 02 '19 at 05:46
  • i can confirm this solved my problem on pycharm 2019.1 – Fathy Feb 26 '20 at 15:24
  • 1
    This solved my issue as well, thanks! I navigated by going to `Pycham > Preferences > My Project > Project Structure` on mac – Ezra_Bender Dec 30 '21 at 15:33
18

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.

Ayush Vatsyayan
  • 2,498
  • 21
  • 27
6

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.

LuFFy
  • 8,799
  • 10
  • 41
  • 59
GrahamJ
  • 528
  • 4
  • 15
6

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

aircraft
  • 25,146
  • 28
  • 91
  • 166
5

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.

LuFFy
  • 8,799
  • 10
  • 41
  • 59
Milad
  • 83
  • 2
  • 8
4

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

Windo
  • 1,526
  • 1
  • 12
  • 8
  • That's right. You should locate the python file in the `virutanenv` directory. Not the system python file. – Alston Jan 21 '19 at 10:42
4

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

Leonid Dashko
  • 3,657
  • 1
  • 18
  • 26
3

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

MarsYoung
  • 471
  • 1
  • 5
  • 13
1

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

LuFFy
  • 8,799
  • 10
  • 41
  • 59
M_Gh
  • 1,046
  • 4
  • 17
  • 43
0

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

core114
  • 5,155
  • 16
  • 92
  • 189
-1

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

Melistraza
  • 47
  • 3