0
votes

I installed django framework in virtual env and now it is given me an import error.

(WebEnvironment) D:\conda project\web\mysite>python manage.py runserver Traceback (most recent call last): File "manage.py", line 8, in
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 14, in ) 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?

2
Are you using the virtual environment? You need to do something like this my_env\Scripts\activate.bat on windows. If you use some IDE, then you need to set your IDE to use that virtual environment. - ipinak
Which version of django and python is installed on WebEnvironment ? - Shubham Paliwal
You need to install django from virtual environment. Can you check if it is installed in correct environment? - Mahesh Karia

2 Answers

0
votes

Based on the traceback you are getting,

I'd make sure that the virtual environment is activated, and also that the Django module was installed by running these command.

1. Activate the virtual environment in case it's not activated yet

$ source path-to-venv/bin/activate

2. See all the installed modules in the virtual environment and confirm the Django module exists.

$ pip freeze

3. In case Django is missing from the virtual environment's installed modules, then run this command to install it

$ pip install django==version# 

or

$ pip install django   # to install the latest version
0
votes

If you want to create a new virtual environment, you can follow the steps below.

    mkdir djangoproject
    
    cd djangoproject/
    
    pip install virtualenv
    
    virtualenv myvenv

Thus, you create a new virtual environment.

    pip freeze
    
    which python
    
    source myvenv/bin/activate

We activated the virtual environment. (To deactivate -> myvenv deactivate)

    pip install django
    
    pip freeze
    
    django-admin startproject myproject
    
    cd myproject/
    
    python manage.py runserver