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:
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:
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:
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:
If you don’t have a
requirements.txt files, then just run this:
This will install the latest version of Django. If you need a specific version, then you can do that as well with:
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.