Pycharm error Django is not importable in this environment

62,194

Solution 1

I finally found the error. The problem was that one folder name in the virtualenv path had an accent.

This is the old path:

/Users/plorenzo/../telefónica/gestorSchools/venv

This is the new one that works:

/Users/plorenzo/../telefonica/gestorSchools/venv

Solution 2

Yet another case, where my virtualenv did have Django installed, but Pycharm would still display this error.

I eventually found out that the same virtualenv path had been duplicated in the list of available interpreters. Removing all of them and re-adding fixed it.

The duplication happened after I copied the .idea from another machine, and re-created the virtualenv from Pycharm although it was already listed as a (broken) interpreter.

Solution 3

In my case I had to go to Settings (Alt+F7, or the Spanner/Cog icon) and select a Python interpreter which has Django installed alongside it.

If Django isn't already installed, you should be able to install it via the + (plus) icon (not shown below) once an interpreter has been selected.

enter image description here

Solution 4

For people having selected a virtualenv/interpreter that HAS Django installed (but it might have changed python version since first selection...), and even re-installed Django, and still get this message, and slowly getting crazy:

File > Invalidate Caches / Restart ...

Also, keeping the list of available interpreters clean and correct always helps (on my linux, old interpreters of previous linux installations kept popping up...whaat), as well as not using special characters in virtualenv names (as was the OPs problem...see his answer).

Solution 5

Project interpreter, which is being used in the pycharm setting , doesn't have the django package installed. So go to

setting->Project->project interpreter

Then install django package

it will work :)

Share:
62,194
Pablo L
Author by

Pablo L

Updated on July 05, 2022

Comments

  • Pablo L
    Pablo L almost 2 years

    I'm getting the following errors in Pycharm.

    Error: Django is not importable in this environment

    It used to work well but I moved the project and virtualenv to other directory and the error appeared. It's weird because if I run the project ignoring the configuration settings warning that I get, works fine.

    The problem is that now, Pycharm is not finding the project interperter.

    The command that Pycharm is running is:

    /Users/plorenzo/../gestorSchools/venv/bin/python3.4 /Users/plorenzo/../gestorSchools/schoolsManager/manage.py runserver 8000
    

    The virtualenv is activated.

    My $PATH is:

    /Users/plorenzo/../gestorSchools/venv/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
    

    and my $PYTHONPATH:

    /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4
    

    I spent a few days trying to find the solution but I couldn't find it.

  • SaintWacko
    SaintWacko almost 6 years
    Thank you! This is what was causing the issue for me, too
  • cwhisperer
    cwhisperer over 5 years
    in setting->Project->project interpreter I saw the Django package, but installing it again solved the problem for me
  • Nic Scozzaro
    Nic Scozzaro about 5 years
    Thank you. Just restarting PyCharm solved the issue for me.
  • stephendwolff
    stephendwolff about 5 years
    I had to remove the .idea folder, but didn't see the same virtualenv path, so not sure why i kept getting this.
  • Arnaud P
    Arnaud P about 5 years
    After a quick experiment: when opening the interpreter settings from inside a freshly created project, Pycharm still suggests the virtualenvs from my other projects. So I am tempted to conclude that the 'interpreter' data is stored somewhere more central. Eg. on linux I've got stuff in ~/.PyCharm2019.1. Although this somewhat contradicts my own answer :p
  • geekandglitter
    geekandglitter about 5 years
    Restarting Pycharm makes sense. What just now worked for me was uninstalling and reinstalling Django.
  • benzkji
    benzkji almost 5 years
    fun fact: you get all the upvotes, for a problem that was not yours, but that many others have!
  • maciek
    maciek over 4 years
    Solved it for me, PyCharm 2019.3.1 (Professional Edition) Build #PY-193.5662.61, built on December 18, 2019
  • Troy
    Troy over 4 years
    Wow, I tried all 15 suggestions above here with no success, but this one finally worked!
  • Shane
    Shane about 4 years
    Saved me a lot of head scratching just now, thank you!
  • CoderGuy123
    CoderGuy123 about 4 years
    Worked for me too, was driving me nuts with a false positive error. PyCharm 2016.3.2 Build #PY-163.10154.50, built on December 28, 2016
  • Iñigo González
    Iñigo González almost 4 years
    This problem is really hard to spot if spanish is your native tongue.
  • Ivan Rostovsky
    Ivan Rostovsky over 3 years
    Thanks! Exact my issue after I recreated the virtual env for the new version of python but with the same name as was before.
  • MikeSchem
    MikeSchem almost 3 years
    Work for me while using a docker image. PyCharm 2020.3.3 (Professional Edition) Build #PY-203.7148.72
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • benzkji
    benzkji over 2 years
    looks like this issue is still experienced in...2021...