Pycharm is not recognizing django, even though it is installed correctly. Why?
Asked Answered
H

14

12

I've got my project set up under a native virtual environment in 3.4 and have had it that way for a while and had it working fine. But for some reason, I am now having issues with running my project through Pycharm. Just to check that everything was set up properly, I did the following:

>>> import sys
>>> sys.path
>>> import django

I can confirm that PROJECT/lib/python3.4/site-packages is included in the path and that Django is installed in this path.

However, when I attempt to run the project through Pycharm I get ImportError: No module named 'django'. I added the following to the Django's manage.py to troubleshoot:

sys.stdout.write(', '.join(sys.path))

The site-packages directory is nowhere to be found. Next, I went to "Settings > Project Interpreter" to make sure I had the correct interpreter chosen. It lists Python 3.4 under the virtual environment and includes Django as the first of the installed packages.

My question, what the heck, Pycharm? The virtual environment is clearly set up correctly. The path is correct from the python terminal and Pycharm itself recognizes all the packages from the virtual environment under it's project interpreter section. Why then, when I actually run the project, does Pycharm stupidly decide to forget to run the project using the virtual environment and not use the correct python path? Little frustrated here. Would appreciate any suggestions.

Hernardo answered 1/7, 2015 at 13:36 Comment(0)
F
6

If you are using virtual environment, go

File > Settings > Project:PycharmProjects > Project Interpreter

and for the current project select the Python path inside your virtualenv at the select box. The path for your python will look similar to this:

~/PycharmProjects/your-project-path/venv/bin/python
Forfeit answered 14/6, 2017 at 10:22 Comment(0)
R
4

I was facing similar issue. Pycharm was not showing and django options. To configure I did the following things:

  • go to edit configuration(run/debug menu)
  • In script path provide your manage.py location
  • in parameters provide 'runserver'
  • in python interpreter provide python location inside your virtual env

And now you can run and debug the django project using pycharm.

Readytowear answered 1/4, 2018 at 6:43 Comment(1)
This is what worked for me - I was not able to make the server work with the "Django" configuration, I had to use a "Python" configuration.Esbenshade
M
3

The django version in local might not be the same as to which is saved into virtual environment. In order to resolve this, it is necessary that interpretor in intellij should point to python file saved into virtual environment. In order to do this performa following actions:

  1. Move into project interpretor settings in Pycharm. (command + ',')
  2. In Project Interpretor option, give the path of python3 which is present inside bin directory inside your virtual env where project is located.
  3. Applying these changes should remove the errorenter image description here

  4. In the above pic, highlighted text shows where python binary file is present.

Macromolecule answered 26/2, 2019 at 7:48 Comment(0)
H
2

Only thing that I could thing to do to solve this, at least for now, was to go into Run/Debug Configurations and set the PYTHONPATH environment variable manually to include the site-packages directory where everything is installed. Once this was done, everything worked fine. Still no clue why this would be there in the terminal but not be set in Pycharm when I'm using the same venv for both. Nor does it make any sense to me why this seemed to happen all of a sudden. Very odd.

Hernardo answered 1/7, 2015 at 14:10 Comment(0)
A
1

Just had the same problem, I went to External libraries -> interpreter ->site packages and "resyncronized" the forlder. django apperared. It seems to me it pycharm did notu update the project structure after I installed some packages.

Asuncionasunder answered 4/11, 2015 at 12:52 Comment(0)
W
0

Make sure your run configuration is correctly pointing to the proper virtual environment. Go to Run menu -> Edit Configurations... Then select the Django Server configuration you are using, and edit it. Make sure its Python Interpreter is set correctly.

Womanhood answered 1/7, 2015 at 14:4 Comment(2)
As I mentioned in the description, I did this, and it is.Hernardo
I dont mean Settings-->Project Interpreter, but Run-->Edit configurationsWomanhood
P
0

PyCharm controls PYTHONPATH itself, it ignores whatever it gets from parent shell (I guess to keep a project's behaviour deterministic).

Check out the documentation for configuring the project's interpreter path: https://www.jetbrains.com/pycharm/help/installing-uninstalling-and-reloading-interpreter-paths.html

Priggish answered 7/7, 2015 at 2:50 Comment(0)
A
0

If you install Django in your system without activating virtual environment then only pycharm will recognize imports. So, if you already installed Django inside some virtual environment then remove all those environments or Django from that environment and then without activating venv run command "pip install Django==version".

Why you need to remove all venv where you already installed Django?

If you don't and then run "pip install Django" then it will show that it's already installed. That's why you have to remove all venv.

After installing Django without activating virtual environment, you can create new virtual environment and do your job.

This is how I resolved this issue!

Ashlar answered 18/1, 2018 at 18:56 Comment(0)
S
0

Go to Preferences -> Libraries and Frameworks -> Django and add PYTHONPATH to Environment variables with value: $PYTHONPATH:/< your virtualenv site-packages directory >

Works for me.

Swash answered 4/10, 2018 at 6:22 Comment(0)
O
0

For me the problem was a bit different. Interpreter was well configured, but my Pycharm root directory wasn't correct.

I work with the following Pycharm project structure :

/.idea/
/env/
/django-project-name/app1/
/django-project-name/app2/
/django-project-name/django-project-name/settings.py
/django-project-name/manage.py

You have two possibilities:

1 - You create a new django project directly in /django-project-name/ (easiest - /.idea/ is moved from / to /django-project-name/)

2 - Define a Sources forder

You have to go to the Pycharm Setting > Project: > Project Structure ; Then mark your project directory as "Sources" (just click on the django project folder and this click on your "Sources at the top").

Finally, restart Pycharm.

Obese answered 14/10, 2018 at 16:8 Comment(0)
T
0

After many trials using different answers, this is how I solved the problem.

Click terminal tab in your pycharm

pip install django 

Do the same thing for all other modules that pycharm is showing error message that it can't discover.

Tarpon answered 5/4, 2022 at 5:57 Comment(1)
Make sure you install the same version as you have in you requirements.txt If you don't have requirements.txt in your main project folder, go into the project folder in your terminal and run pip freeze>requirements.txtTarpon
S
0

Go to pyvenv.sfg file and then include system variable to true. It worked for me.

Simson answered 7/2, 2023 at 14:41 Comment(0)
I
0

Just had the same problem myself (cannot import python default libraries). Instead of filling in paths as suggested by others, I unchecked "Add content root to PYTHONPATH" and "Add source roots to PYTHONPATH" (under Run, Edit configurations, specific config). That solved it for me.

Ingleside answered 23/3, 2024 at 12:53 Comment(0)
H
-1

For me it seems that line 5 in a project_creator file for PyCharm is looking for "django" but the virtual env is installing "Django" (capital D).

I've not tried to find the file and adjust it (if it's at all possible) but I thought I'd put my 2 cents here for now :)

Herophilus answered 4/11, 2018 at 20:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.