96

I've already configured virtualenv in pycharm, when using the python manage.py command, this is error shown:

E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    "Couldn't import Django. Are you sure it's installed and "
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?

How should I fix it, I've installed django.

Astik Anand
  • 12,757
  • 9
  • 41
  • 51
kerberos
  • 1,527
  • 2
  • 10
  • 18

35 Answers35

73

I think the best way to use django is with virtualenv it's safe and you can install many apps in virtualenv which does not affect any outer space of the system vitualenv uses the default version of python which is same as in your system to install virtualenv

sudo pip install virtualenv

or for python3

sudo pip3 install virtualenv

and then in your dir

mkdir ~/newproject

cd ~/newproject

Now, create a virtual environment within the project directory by typing

virtualenv newenv

To install packages into the isolated environment, you must activate it by typing:

source newenv/bin/activate

now install here with

pip install django

You can verify the installation by typing:

django-admin --version

To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:

deactivate
Community
  • 1
  • 1
Kalariya_M
  • 1,357
  • 12
  • 20
  • 11
    Yes, I did as you said, but I still suggest the same mistake – kerberos Sep 14 '17 at 08:02
  • this should work well because i tried in my system and this works like charm – Kalariya_M Sep 14 '17 at 18:04
  • I love you. This worked for me. I tried a lot of other things and always got an error. You are my saviour – Madelyne Velasco Mite Aug 20 '18 at 19:06
  • 2
    For me, it does not generate the file `newenv/bin/active` but `newenv/bin/python`. What a hassle this is. Why don't they just say in their docs which ENV vars they need. Instead they use helper programs that have no transparency at all. – Daniel W. May 09 '19 at 12:19
  • For activating the virtual environment, you can use newenv\Scripts\activate.bat when working on windows. – Adii_Mathur Oct 31 '22 at 16:33
23

When you install Django on your computer, everything works fine but when you install a Virtual environment it gets separated from all things. You will know it's importance when you will make a final project and deploy it to any cloud or hosting.

Just re-install Django in the virtual environment and baam:

pip install Django

and then just run the command for testing:

python manage.py runserver

and you are all done.

Tarctic
  • 13
  • 6
17

You need to install Django, this error is giving because django is not installed.

pip install django
Astik Anand
  • 12,757
  • 9
  • 41
  • 51
  • 8
    I have installed it. I have also PIP list. I used python 3.5. Now I have changed the environment to 2.7, which is wrong. I do not want to write the sys. Path in every django to indicate django – kerberos Sep 14 '17 at 05:15
  • That's what earlier you used `pip3 install django` but now for python2 you need `pip install django`. – Astik Anand Sep 14 '17 at 05:16
9

You need to use both commands: pip install django and pip3 install django that worked for me

Sathiamoorthy
  • 8,831
  • 9
  • 65
  • 77
Artur Boytsov
  • 157
  • 2
  • 3
9

find your django parent dir path and add it to PYTHONPATH

In my case, my django parent dir path is /Library/Python/3.7/site-packages,add this line into ~/.bash_profile

export PYTHONPATH=/Library/Python/3.7/site-packages

else if you have PYTHONPATH already, just append it like this

export PYTHONPATH=${PYTHONPATH}:/Library/Python/3.7/site-packages

then

source ~/.bash_profile
chie
  • 291
  • 3
  • 4
7
  1. Check that you have installed Django; by executing import django in python. you mustn't see ModuleNotFoundError if everything's ok.

  2. Check that you have installed virtualenv; by executing virtualenv --version. you must see the version number if everything's ok.

  3. Check that you have enabled virtualenv; there's got to be the name of your virtualenv in your command prompt starting line. enable it by source bin/activate. also, remember to deactivate it every time your job is finished with the virtualenv.

    my terminal changes after enabling virtualenv

  4. Check that your virtualenv includes django. a virtualenv by default has no modules installed. you either have to install django in your virtualenv (even if you have it in your machine already) or use virtualenv --system-site-packages when creating a virtualenv to include system site packages in the virtualenv.

  5. Add django to your path. open python, import django, then run django to see django's path. then add it to your ~/.bashrc (or ~/.zshrc if you're using zsh). more info in here

  6. Install django-admin by running pip install django-admin

Nitwit
  • 291
  • 3
  • 13
4

I was having great difficulties with this but I have solved my issue. I am on Windows 10 using Vagrant ssh in my virtualenv environment, the box I have installed is ubuntu/xenial64, Django version 2.1, python==3.6.

When I was installing packages I was using pip3 but most importantly I was using sudo and the -H flag to install these packages. When I ran sudo pip3 freeze my packages would come up, but when I ran a plain pip3 freeze there would be no packages.

Then I tried the python3 manage.py startapp <YOUR APP NAME> and it did not work same error as you.

I finally thought to try sudo python3 manage.py startapp <YOUR APP NAME> it finally worked!

Hope this was help :)

  • 1
    Hey Braiden, using sudo means you used root to run your app! That should usually not be required (it's even dangerous for security reasons). It shows that you had or have a problem with the permissions of your files or folders. – cslotty Apr 11 '19 at 10:30
  • Yes! Thanks for writing in. The project was abandoned(no git files commited) I have had difficulties figuring out Vagrant. I have been using just anaconda to learn for now going to school in September. I will get to learn all about this stuff soon. –  Apr 11 '19 at 11:57
4

I faced the same issue, and in my case it was because I had multiple python versions on my machine, in addition to the Anaconda ones. In my case django didn't worked well with my anaconda python. I knew that when I run import django on each python terminal for all versions I have.

As a summary here are the steps I made to get this solved:

  1. Run the CMD as Admin

  2. Create a project folder.

  3. Create a new ENV for this new project INSIDE THE PROJECT Folder...

    pip install virtualenv >> virtualenv new_env`
    
  4. Activate it:

    .\new_env\Scripts\activate`
    
  5. After the env activation ⇒ Install Django:

    python -m pip install Django
    

The python version you used here in step 5 will determine which python will to work with this installed Django.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
HassanSh__3571619
  • 1,859
  • 1
  • 19
  • 18
4

Make sure you have Django installed by writing this command :

python -m django --version

if it's not installed you can install it by writing this command :

pip install django      
Anurag Sharma
  • 4,276
  • 2
  • 28
  • 44
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '22 at 15:30
3

You can use python3 to run file, if you don't want to use virtualenv.python3 manage.py runserver

To install python3 look at this page

3

If you are working on a machine where it doesn't have permissions to all the files and moreover you have two versions such as default 2.7 & latest 3.6 then while running the command use the python version with the command. If the latest python is installed with sudo then run the command with sudo.

exp:

sudo python3.6 manage.py runserver
abhisek
  • 337
  • 1
  • 3
  • 11
3

after activating virtual env that error raises up on ubuntu. and I solve this issue just by typing again :

pip3 install Django

inside the directory which is I want to create a new app.

Alireza Atashnejad
  • 612
  • 1
  • 6
  • 22
1

I solved this problem in a completely different way.

Package installer = Conda (Miniconda)
List of available envs = base, djenv(Django environment created for keeping project related modules).

When I was using the command line to activate the djenv using conda activate djenv, the base environment was already activated. I did not notice that and when djenv was activated, (djenv) was being displayed at the beginning of the prompt on the command line. When i tired executing , python manage.py migrate, this happened.
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?

I deactivated the current environment, i.e conda deactivate. This deactivated djenv. Then, i deactivated the base environment.
After that, I again activated djenv. And the command worked like a charm!!

If someone is facing a similar issue, I hope you should consider trying this as well. Maybe it helps.

taurus05
  • 2,491
  • 15
  • 28
1

Instead of creating a new virtual environment, you just have to access to your initially created virtual environment when you started the project.

You just have to do the following in your command line:

1)pipenv shell to access the backend virtual environment that you have initially created.

2) Then, python manage.py runserver

Let me know if it works for you or not.

BC. Lam
  • 19
  • 1
1

To create a virtual environment for your project, open a new command prompt, navigate to the folder where you want to create your project and then enter the following:

py -m venv project-name This will create a folder called ‘project-name’ if it does not already exist and setup the virtual environment. To activate the environment, run: project-name\Scripts\activate.bat**

The virtual environment will be activated and you’ll see “(project-name)” next to the command prompt to designate that. Each time you start a new command prompt, you’ll need to activate the environment again.

Install Django

Django can be installed easily using pip within your virtual environment.

In the command prompt, ensure your virtual environment is active, and execute the following command:

py -m pip install Django

0

In case you have virtual env activated, django installed, django-admin --version prints the valid version - check if there is no circular import in the file you are executing.

fanny
  • 1,373
  • 12
  • 25
0

I faced the same problem when I was doing it on windows 10. The problem could be that the path is not defined for manage.py in the environment variables. I did the following steps and it worked out for me!

  1. Go to Start menu and search for manage.py.
  2. Right click on it and select "copy full path".
  3. Go to your "My Computer" or "This PC".
  4. Right click and select "Properties".
  5. Select Advanced settings.
  6. Select "Environment Variables."
  7. In the lower window, find "Path", click on it and click edit.
  8. Finally, click on "Add New".
  9. Paste the copied path with CTRL-V.
  10. Click OK and then restart you CMD with Administrator privileges.

I really hope it works!

0

Looks like you have not activated your virtualenv when using the runserver command.

Windows: <virtualenv dir>\Scripts\activate.bat

Linux: source <virtualenv dir>\bin\activate

You should see (name of virtualenv) as a prefix to your current directory:

(virtualenv) E:\video course\Python\code\web_worker\MxOnline>python manage.py runserver
Steve771
  • 31
  • 8
0

windows :

  1. (virtualenv dir)\Scripts\activate # this step to activate virtualenv

  2. you should be in the dir of (project name)

  3. python manage.py runserver

prosoitos
  • 6,679
  • 5
  • 27
  • 41
0

you need to go to the root directory and run the below command

source bin/activate

Once the above command is executed, you will be able to create custom apps

0

I also face the same problem in windows 10 with anaconda For me anaconda3\Scripts>activate

it's working good. What you have to do you just need to go to anaconda home

AppData\Local\Continuum\anaconda3\Scripts

and you need to open a cmd prompt and type activate.

It will activate the venv for you.

Anupam Choudhary
  • 39
  • 1
  • 1
  • 9
0

if you don't want to deactivate or activate the already installed venv just ensure you have set the pythonpath set

set pythonpath=C:\software\venv\include;C:\software\venv\lib;C:\software\venv\scripts;C:\software\venv\tcl;C:\software\venv\Lib\site-packages;

and then execute

"%pythonpath%" %venvpath%Scripts\mytestsite\manage.py runserver "%ipaddress%":8000

Gopal
  • 757
  • 3
  • 13
  • 30
0

The problem is related to this error: Execution Policy Change

Start virtualenv by running the following command:

Command Line C: \ Users \ Name \ yourdjangofilesname > myvenv \ Scripts \ activate

NOTE: On Windows 10, you may receive an error by Windows PowerShell that the implementation of these scenarios is disabled on this system. In this case, open another Windows PowerShell with the "Run as Administrator" option. After that, try typing the following commands before starting your virtual environment:

C:\WINDOWS\system32> set-executionpolicy remotesigned

Execution Policy Change: The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170.

Do you want to change the execution policy? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): A

After selection Y(es), close the Powershell admin window, and then go back to the Powershell Window(where you got the error) and run the command again.

> myenv\Scripts\activate and then python manage.py runserver 8085 ,

(8085 or any number if you want to change its default port to work on otherwise you dont need to point out anything. )

Voontent
  • 719
  • 5
  • 8
0

I had same problem, I installed all dependencies with root access :

In your case:

sudo pip install django

In my case, I had all dependencies in requirements.txt, So:

sudo pip3 install -r requirements.txt

L Lawliet
  • 419
  • 1
  • 7
  • 20
0

Just sync your pipenv environment with:

pipenv sync
kingmilo
  • 147
  • 2
  • 11
0

I had this problem with Django 3.

On manage.py detail the execute_from_command_line import.

You should have:

from django.core.management import execute_from_command_line

Instead of

from django import execute_from_command_line
 
Miguel Carvalhais Matos
  • 1,113
  • 2
  • 13
  • 21
0

I had the same problem and my solution was not posted here:

How I got the error

My error came whenever I was installation the requirements.txt file with pip (let's say from cloning a git repository).

Solution

I manually installed each of the modules in the requirements.txt + any other module needed for the installation of those modules (e.g: I got errors and some modules where missing to install other modules so I had to add them too).

Frederic Perron
  • 777
  • 4
  • 19
0

If there is anyone who faced with the same problem when using virtual environment and running on MacOS, just try

sudo python manage.py startapp <project_name>

instead of

python manage.py startapp <project_name>

It will solve the problem suprisingly!

Oguzhan Bolukbas
  • 504
  • 6
  • 13
0

I had to install django using the virtual environment pip3 executable directly:

cd [virtual environment folder]/bin
sudo ./pip3 install django
Nir
  • 1,836
  • 23
  • 26
0

If you already installed Django / configured virtualenv and you still having the error:

ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?

Try to run the command pipenv shell before start the server with py manage.py runserver

0

Just had this issue in VScode. I had a virtual environment set up and Django was installed. My server ran fine but whenever I tried running the debugger, VScode would throw the ImportError and deactivate my virtual environment.

Fixed it by restarting VScode.

Try the other answers first, make sure your virtual environment is set up and you can run your server. Then if it still doesn't work, restart your IDE.

alcampk
  • 71
  • 1
  • 4
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32091969) – Ivan Starostin Jun 29 '22 at 18:58
0

I had the same error, might have been for different reasons but just in case others find their way to this page who had my problem, this page solved it.
https://iancarpenter.dev/2021/08/19/exception-has-occurred-importerror-couldnt-import-django-when-debugging-django-with-vs-code/#resolution

0

If you have many python version installed for example python 3.6 & python 3.7 Interpreters . Django may be installed on python 3.7 version not in 3.6 and you choose the Interpreter with 3.6 the problem arise, you need to change your interpreter to solve that .

0

first check django version

django-admin --version

next

pip install django

-2

If you are using python 3 use py in front of cmd code, like this

py manage.py runserver
Santhuy
  • 11
  • 1