I've installed Django multiple ways, via apt-get and pip install. They all say I have the most recent version of Django. Now whenever I run python in Terminal and type in import django, I receive
ImportError: No module named djangoHowever, when I run django-admin --version I receive
1.4.3I know it's installed, but why is python not finding the django module?
26 Answers
python is not finding django because it's not on its path. You can see the list of paths python looks for modules like this:
$ python
>>> import sys
>>> sys.pathYou can import django if you find the location it is installed, and add that location to python's path, for example like this:
$ PYTHONPATH=/path/to/django/parent/dir python
>>> import django # should work nowBut your real problem is that something's wrong with your python installation. If you have installed both python AND django using apt-get, then django should certainly be on python's path without dirty hacks like above.
That said, when working with Django, your best bet is to NOT use apt-get but create a virtual environment using virtualenv (you can install virtualenv itself using apt-get), and install Django and other modules your Django site might need using pip within the virtual environment. That way you can have multiple Django projects side by side, with precisely the Python modules and versions it requires. It's just a few extra steps to do, but definitely worth it and will save you from much frustration in the future.
I had the same problem when I was using PyCharm Community Edition for making Django projects.
For me, the following steps worked:
It turns out that python wants you to create a virtual environment, install django in that and then run the server. To do this,
Create a Virtual Environment
1) Install virtual environment using
pip install virtualenv.2) Navigate to the project folder and type
virtualenv env(Hereenvis the name of the virtual environment.) This will create a new folder namedenvinside the project folder.3) Navigate to
env/Scriptsinside your Project Folder usingcd env/Scripts.4) Type
activateand press Enter. This should start the virtual environment. You can verify this as(env)would be prefixed to your current path.
Install Django
1) Once inside the virtual environment, head back to your project folder using
cd ../..and typepip install django.2) You can verify its installation by typing
django-admin --version. It should display the django version number installed inside the virtual environment.
Now type python manage.py runserver to start the python server.
I faced the same problem. I activated the virtual environment
$ source bin/activate after that it showed me the version of Django
There was no need of installing anything.
mamata@mamta-inspire-all:~/dodjango$ python -m django --version
/usr/bin/python: No module named django
mamata@mamta-inspire-all:~/dodjango$ django-admin --version
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
mamata@mamta-inspire-all:~/dodjango$ source bin/activate
(dodjango) mamata@mamta-inspire-all:~/dodjango$ ls
bin include lib lib64 mamusite pyvenv.cfg share
(dodjango) mamata@mamta-inspire-all:~/dodjango$ django-admin --version
2.2.5
(dodjango) mamata@mamta-inspire-all:~/dodjango$ python -m django --version
2.2.5 1 If you have multiple python version installed into your machine. The different packages you are trying to install gets conflicted.So, the best approach is to use virtualenv..
- Simple install virtual environment and python version
- use virtualenv env to create virtualenvironment.
- activate your virtualenv. using . /env_name/bin/activate
- use pip install django==1.70 (specify version to install django)
- pip list to list all installed packages.
i have this problem and still had it till recently in my project vscode did not recognize my virtual environment , so when ever i try to run the project it gives me an error that vscode can not import django
the fix for this for me that i exported the libraries in my virtual environment to a fill
pip freeze > req.txtthen i created a new virtual environment with the same requirements , and it worked
note: try looking this up python virtual environment
I've got this problem when I renamed the project folder in PyCharm.
- Terminal tab stopped getting into the Virtual Environment (
venvfolder). To fix that, I went to theSettings -> Project -> ProjectInterpreter and changed the folder there. - Django "disappeared".
django-admin --versiondid not respond. To fix, I didpip install djangowithin thevenv.
It helped.