Fix ImportError: no module named django.core.management

One very common error that I get is the ImportError: No module named django.core.management, but fortunately, it's quite easy to fix.

This is the full traceback of the error:

Traceback (most recent call last):
  File "manage.py", line 8, in <module>
    from django.core.management import execute_from_command_line
ImportError: No module named django.core.management

The error is telling us that it can't find of the core modules of Django. This is because it isn't installed in your current environment.

For Django, we generally want to use a virtual environment to install all the python packages in.

How to fix the ImportError

Fixing this error depends a bit on your current set up. If you do not have a virtual environment set up, then you can read on. If you know that there is a virtual environment already, then skip to the second part.

1. Create a virtual environment

If you do not have such an environment for your current project, then you need to do this:

Mac OS/Linux:

virtualenv .env

Windows:

mkvirtualenv .env

That will create a folder called .env that will be the virtual environment for your project.

2. Enable the virtual environment

You either already have an environment or just created it. Then you need to use the virtual environment. You can do that with this:

Mac/Linux:

# Change `.env` in the next steps depending on your environment name/folder. 
source .env/bin/activate

Windows:

# Change `.env` in the next steps depending on your environment name/folder. 
workon .env

3. Install Django in your virtual environment

Check if there is a requirements.txt file in the root of your project. If there is, then it means someone (perhaps you), has created a file which lists all of the packages that have to be installed to get it up and running. If that's the case, then you only need to run this:

pip install -R requirements.txt

If you don't have a requirements.txt files, then just run this:

pip install django

This will install the latest version of Django. If you need a specific version, then you can do that as well with:

pip install django==2.2.4

Now try to run Django again

The error should be gone now. If you still get an error, then comment below and I will try to help you.

To put it short: this error is caused because Django isn't installed in your environment (virtual environment). I would not recommend installing Django without being in a virtual environment since that would be the default for your computer then. It would make it a lot harder to switch between different projects. The virtual environment is there to make sure that you can easily switch between different packages that are needed for different projects.

Django error importerror tips-tricks
Written by Stan Triepels

Stan is professional web developer working mainly with Django and VueJS. With years of experience under the belt, he is comfortable writing about his past mistakes and ongoing learnings.