Improper configuration settings on fresh Django install.

MacOS Catalina
Python: 3.7.5
Django 3.0.2

Everything was working fine 3 weeks ago, and on the same computer with the same setup, as well as the list of core commands, I am now getting this message when running django-admin:

Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).

‘django-admin startproject myProj’ does create the project, however running ‘python3 manage.py runserver’ throws the following error:

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?

Have searched online for solutions but there is very little about this issue.

Anyone with the same issue or know how to fix the problem?

1 Like

Hi!

I’m only a beginner and so forgive me if I’ll be confusing you more (or sound condescending since I might not understand your workflow), but:

Your first error message is default behavior as far as I know - I get exactly the same warning message if I simply run django-admin. According to the official docs for django-admin/manage.py,

[manage.py] does the same thing as `django-admin` but also sets the [ `DJANGO_SETTINGS_MODULE` ] environment variable so that it points to your project’s `settings.py` file.
You are trying to use the django-admin command to do something that Django kind of expects you to do using python manage.py (because it expects the DJANGO_SETTINGS_MODULE variable to be set), and so it warns you that not all django-admin-related commands are listed. It might sound very contradictory when you're typing in django-admin, that Django would think that's odd, but I can't really explain it in a better way. Try running python manage.py once you've got a working project to see that this returns the same list of commands, but with some extra (like auth, sessions- and staticfiles-related) commands added.

I see that you use the command ‘python3 manage.py runserver’. I’m not used to seeing that since the ‘python3’ part isn’t necessary when I set up a virtual environment in pipenv and enter the pipenv shell, all ‘python […]’ are executed using the python version specified for the virtual env. Are you not using a virtual environment? If you are using e. g. pipenv, you might try the command pipenv shell before running django-admin startproject foo and then python manage.py runserver.

In case you’re not using virtual environments, you can have a look at instructions for setting pipenv up here: Chapter 1: Initial Set Up | Django For Beginners It’s import to follow the step-by-step instructions exactly before you try making variations yourself.

If you are using a virtual environment for your project and it seems to be working as it should otherwise, could you try other commands using python manage.py? E. g. python manage.py check, to see if that provides any helpful information. If python manage.py shell works, you can then try
>>> import os
>>> os.sys.path
.
That will give you a list of directories that Python searches through for relevant packages. One of the directories should be named something like ‘site-packages’. Try going to that folder and see if that folder in turn contains a folder named ‘django’. If it doesn’t, that probably means your virtual environment handler isn’t working as it should. Then you can search for more specific help for e.g. venv or pipenv.

Again, apologies if I completely missed the mark.

:star:Good luck!:star:

2 Likes

Thank you @arboc for that, you are certainly a lot less of a newbie than I am!

I didn’t know running django-admin on its own gives that message as standard, and you pointing it out got me thinking…

I was creating the venv on my Google Drive from where I was running django. I changed to local drive and hey presto it worked again!

I don’t know enough to reason why it doesn’t work on Google Drive, but at least it is okay now and i continue enjoying learning. Thanks again.

1 Like