ImportError: Couldn't import Django ... Did you forget to activate a virtual environment?
Solution 1
On the Windows machine, you should activate venv
by this command .\venv\Scripts\activate
(please note, you should be in the folder where this venv is)
Then inside activated venv install Django pip install django
and in the same terminal run the server python manage.py runserver
Solution 2
I had similar issue in my windows 10 system and solved using pipenv. Steps with commands given below.
- cd/Go to the project folder
- Setup the virtual environment
pipenv install
- Activate the virtual environment
pipenv shell
- Install django
pip3 install django
- Run the project
pipenv run python manage.py runserver
Solution 3
I had similar issue with:
- django installed globally
- virtual environment setup and activated
- django project setup under venv activated
When trying to run the project, it could not import django anyway.
Adding path to manage.py in variables did not help but as mentioned above, installation of django under venv activated actually resolved the issue.
Your case may be different but try this scenario:
- Setup venv
- Activate venv
- Install django
- Create django proj
- Try to run django proj
Kampet
Updated on May 23, 2021Comments
-
Kampet almost 3 years
I know there are several questions/answers about this, but I can;t figure out what I should do. I wanted to get started with Django and installed it with
pip install
and added Python37 and Python37-32 to my environmental variables, and I guess it worked, because I can run several Python commands in my shell. But every time I try topython manage.py runserver
it gives me an error.
I also set up my virtual environment and activated it, but I think there's a problem with Django. But because I installed it with
pip install django
I know it's there and I can use commands likedjango-admin startapp
... So I guess Django is working. I don't really know what PYTHONPATH means and where to find it. It would be pretty nice if anybody could take a look at my error.Here you can see that Django is installed : #
**C:\Users\Kampet\Desktop\Python-Django\mysite>pip install django Requirement already satisfied: django in c:\users\kampet\appdata\local\programs\ python\python37-32\lib\site-packages (2.2.4) Requirement already satisfied: pytz in c:\users\kampet\appdata\local\programs\py thon\python37-32\lib\site-packages (from django) (2019.2) Requirement already satisfied: sqlparse in c:\users\kampet\appdata\local\program s\python\python37-32\lib\site-packages (from django) (0.3.0)** # And thats my error **C:\Users\Kampet\Desktop\Python-Django\mysite>python manage.py runserver Traceback (most recent call last): File "manage.py", line 10, in main from django.core.management import execute_from_command_line ModuleNotFoundError: No module named 'django' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 16, in main ) from exc ImportError: Couldn't import Django. Are you sure it's installed and available o n your PYTHONPATH environment variable? Did you forget to activate a virtual env ironment?** ###################
Here is where my virtual environment is located.
Python-Django
-----------------mysite
-------------------------main
-------------------------mysite
-------------------------manage.py
-----------------venv
-------------------------Include
-------------------------Lib
-------------------------Scripts
-------------------------pyvenv.cfg
This is my manage.py:
#!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys def main(): os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': main()
#
I don't know why it can't find the module "django" / django.core.management I also can't find django.core.management anywhere in my files, but I reinstalled and upgraded django several times. I don't know if this helps you.
Thank you for your time.
-
Gathide over 3 yearsWhy?
Anaconda
installs other packages.