1

Tree Directory:

.
├── Pipfile
├── Pipfile.lock
├── README.md
├── Strawberry
│ ├── init.py
│ ├── pycache
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── catalog
│ ├── init.py
│ ├── pycache
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── static
│ ├── templates
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── db.sqlite3
├── foo.py
├── manage.py
└── requirements.txt

7 directories, 19 files

Dudes, it's Ok when I use:

$manage.py check

And I get the result without any problems, Errors and Exceptions.

But when I write the same with django-admin:

$ django-admin check

I get Error:

ModuleNotFoundError: No module named 'Strawberry'

From this result we can now know that Django finds/see environment variable — $DJANGO_SETTINGS_MODULE but don't see directory with settings.


But if I import `models` from `catalog.models` into `foo.py` then on run by python3 pipenv interpretator (Note: virtual environment is activated):
$ python3 foo.py

I get:

ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings

In this case Django not see $DJANGO_SETTINGS_MODULE, but $DJANGO_SETTINGS_MODULE has been created (in virtual environment) and its value is:

$DJANGO_SETTINGS_MODULE=Strawberry.settings

Summing up, I want to know:

This is Ok when I execute a file (foo.py) contains x code/script that uses (imports) Django objects (models) using python interpretator:

$ python3 foo.py

I get an Error, but at the same time, when executing the same code x in django shell:

$ python3 manage.py shell < foo.py

I get the result, without any errors or exceptions.

0 Answers0