ChatGPT解决这个技术问题 Extra ChatGPT

Pycharm error Django is not importable in this environment

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.

I just started using pycharm, but did you change settings -> project interpreter -> Project Interpreter, to reflect your new interpreter? (/Users/plorenzo/../gestorSchools/venv)
Yes, but it's not recognising the installed packages in the virtualenv.
what happens when you set the interpreter path to the directory Above "bin" (the actual virtualenv)?
the interpreter path is already in /Users/plorenzo/dev/trabajo/telefonica/gestorSchools/venv The problem is that nothing appears in the External libraries directory where django and all the packages should be.

A
Arnaud P

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.


Thank you! This is what was causing the issue for me, too
I had to remove the .idea folder, but didn't see the same virtualenv path, so not sure why i kept getting this.
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
Saved me a lot of head scratching just now, thank you!
Thanks! Exact my issue after I recreated the virtual env for the new version of python but with the same name as was before.
J
James Bradbury

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.

https://i.stack.imgur.com/r5eWX.png


b
benzkji

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). Messing with the .idea folder also is not recommended (dont copy it to your new computer ;-).


Solved it for me, PyCharm 2019.3.1 (Professional Edition) Build #PY-193.5662.61, built on December 18, 2019
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
Work for me while using a docker image. PyCharm 2020.3.3 (Professional Edition) Build #PY-203.7148.72
looks like this issue is still experienced in...2021...
M
Machavity

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 :)


in setting->Project->project interpreter I saw the Django package, but installing it again solved the problem for me
P
Pablo Lorenzo

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

fun fact: you get all the upvotes, for a problem that was not yours, but that many others have!
This problem is really hard to spot if spanish is your native tongue.
j
jasonaibrahim

I had the same problem and it was due to having more than one project interpreter defined in the project. In my case I had a python 2.7 and a python 3.7 interpreter. Fixed this issue by removing the python 2.7 interpreter.

View the current interpreter by navigating to Preferences > Project > Project Interpreter Open the dropdown and select Show All... Remove any interpreters that aren't your primary


g
g.carey

It might seem obvious, but try quitting and then re-starting the PyCharm application. When I had this issue, I continued to get the "Django is not importable in this environment" error even though the project interpreter path was correct. Nothing worked until I closed and re-opened the application. Hope this saves someone some time.


Restarting Pycharm makes sense. What just now worked for me was uninstalling and reinstalling Django.
c
cbueche

I got the same issue on OS X, and to fix it, I had to use an absolute path for the project interpreter

/Users/myself/Projects/... is ok ~/Projects/... was not working


f
filias

I had the same problem and I think the solution is not stated here yet. I had to go to Settings > Project Interpreter, click the gears icon on the right and chose the interpreter (which was already correct). Then click on the last icon to the right, the one with some folders structure icon. There I had to add the path to the site-packages of my venv (where Django is). Finally my tests run with no issues.


Wow, I tried all 15 suggestions above here with no success, but this one finally worked!
t
tanaydin

I had similar problem...

I go PyCharm -> Preferences and tried to readd Django to project interpreter packages. PyCharm suddenly added everything in requirements.txt file. And now I can run project without any problem.


D
Dakusan

In my case, I was running the Cygwin version of Python when this error came up. Fixing it involved just adding some of cygwin's bin paths to Window's Path variable.

Run "control sysdm.cpl" Go to the "Advanced" tab Click the "Environmental Variables" button at the bottom of this tab Find "Path" under "System Variables" Add to it ;c:/cygwin/bin;c:/cygwin/sbin;c:/cygwin/usr/local/bin

I did not include /usr/bin, as for some reason, it is combined in to the /bin directory when viewing in windows.


R
Rob Osborne

Ok, in my case the solution was:

Install django Use @cbueche suggestion to remove the ~ Switch to a another project's venv and switch back to my venv for this project

I exited and reloaded the project a few times in there so that might also be necessary. I love PyCharm but starting a new project is always painful.


Thank you. Just restarting PyCharm solved the issue for me.
T
Trinadh Koya

As everybody said ,Check for your interpreter Was it properly configured it or not.If you created your project in virtual environment ,make sure you configured the correct python interpreter or not .The python interpreter(whole) and python interpreter(for only single project using virtualenv) are different .

https://i.stack.imgur.com/ilCgV.png


N
Nids Barthwal

you have to edit your project configuration for that. Environemt->Environment variables set varable DJANGO_SETTINGS_MODULE=project_name.settings


V
Vivek Singh

I kept getting this error even after following suggestions made in other answers, like deleting .idea folder, reinstalling packages from settings->project interpreter.

Finally, I took the venv out of the project folder and created a venev beside the project folder and deleted .idea folder from project folder.

Then I opened the project and chose the interpreter as the newly created venv


A
Abishek G.C.

steps :

 1. Choose File Option
 2. Setting
 3. Scroll to Project -> Select Python Interpreter
 4. Project Interpreter
 5. Click on Setting icon and press Add environment
 6. Create a Virtual Environment

D
DIlyaY

I had the same error. Try to name path (and project) without "_":

../my_project/venv -> ../my-project/venv

v
valentin

1 Everybody recommend you to check your Interpreter PATH to your venv-folder (virtual environment) in PyCharm: Settings (Ctrl+Alt+S) --> Project: project_name --> Python Interpreter --> gear_icon (settings) --> Show All... --> press '+' to add interpreter with correct path to venv --> Apply --> Ok

2 I also recommend you to try my kung-foo AFTER you done previous: same steps... --> Show All... --> press 'folder-tree icon' (Show paths for the selected interpreter) --> press '+' to add new path --> select path to this folder '../venv/Lib/site-packages'

Do that and you'll be extra-happy!


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.
E
Ehsan Barkhordar

I had the same problem in Pycharm.

I fixed it by removing the old Run/Debug Configurations and then create another one.

Before that make sure to install Django if you haven't already.

It is better to clear caches by going File > Invalidate Caches / Restart ... to be more sure.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now