0

I'm building an API using Django REST Framework with this tutorial. I'm getting a ModuleNotFoundError: No module named 'django' error when I try to test python3 manage.py. I've been going through the long list of issues it could be (reinstall django, need to add an __init__.py in the root, etc) and someone recommended checking my modules list with pip3 list.

Here's what I see:

Package             Version
------------------- -------
asgiref             3.3.1
dj-database-url     0.5.0
Django              3.1.4
django-filter       2.4.0
django-heroku       0.3.1
djangorestframework 3.12.2
gunicorn            20.0.4

Is there a reason why Django's capitalized? Could that be why it isn't finding it in manage.py? If so, how do I go about fixing it?

Here's the full error message I'm getting, but it's really general. The exception is hardcoded:

Traceback (most recent call last):
  File "manage.py", line 11, 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 22, in <module>
    main()
  File "manage.py", line 17, in main
    ) from exc
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?
  • are you using virtualenv? What is the result of `django-admin --version`? – JPG Dec 29 '20 at 05:32
  • 1
    Have you read this SO post, https://stackoverflow.com/questions/46210934/importerror-couldnt-import-django\ – JPG Dec 29 '20 at 05:32
  • 1
    The name of the distribution package (`Django`) and the name of the import package (`django`) do not necessarily have to match. The distribution package is the thing that you _pip_-install. While the import package is the thing that you import. They are 2 different things. 1 distribution package can contain multiple import packages, and names do not have to match.-- https://sinoroc.github.io/kb/python/packaging.html – sinoroc Dec 29 '20 at 11:09
  • @JPG I'm not using virtualenv, and I'm thinking that's my problem based on the SO you sent. Not sure why but my tutorial never mentioned using a virtual environment. `django-admin --version` gives me `3.1.4`. – mrmicrowaveoven Dec 29 '20 at 21:10
  • @JPG I've never used virtualenv, so it's all brand new to me. Is there a tutorial you'd recommend? I'm already pretty deep in a project so I'm hoping to move it smoothly. – mrmicrowaveoven Dec 29 '20 at 21:12
  • I would keep individual virtual envs for each project, which help us to isolate the dependencies from one-another. Also, I don't remember any *pretty* doc/blog of virtualenv, but you could easily find plenty of the same on the internet – JPG Dec 30 '20 at 02:05

1 Answers1

0

While working with a large framework as Django one must always work in a virtual environment. Please check if you have your virtual environment activated. If not, then follow the steps:

  1. cd your_working_directory
  2. virtualenv environment_name where environment_name can be any name you want.
  3. environment_name/Scripts/activate if on Windows

Or environment_name/bin/activate if on MacOS

Then after activating the environment you can install all the dependencies using pip. i.e pip install django

Dhruv Jadhav
  • 260
  • 1
  • 11
  • This seems like great advice. I've never worked with a `virtual environment` before, so I'm brand new to the steps you gave. Is there a tutorial you recommend? I found this one: https://hostadvice.com/how-to/how-to-create-a-virtual-environment-for-your-django-projects-using-virtualenv/ – mrmicrowaveoven Dec 29 '20 at 21:05
  • 1
    This ended up solving my problem, so I'm Accepting it despite the downvotes. It's possible it was downvoted for the following reasons: 1. It didn't mention installing virtualenv (`pip install virtualenv`) 2. Step 2 didn't explain what `environment_name` was. Turns out it can be anything, just whatever we want the name of our environment to be. 3. Steps 3 and 4 should just be Step 3 with an "or" in it, to show that only one of the two needs to be done. Thank you for your help! – mrmicrowaveoven Dec 30 '20 at 05:06
  • Sorry if I missed mentioning to ```pip install virtualenv```. But I am glad it solved your problems. – Dhruv Jadhav Dec 30 '20 at 06:00