I want to run my stand-alone script csvImp.py
, which interacts with the database used by my Django site BilliClub. I'm running the script from the project root (~/BilliClub
) on my virtual environment django2
.
I've followed the instructions here but for DJANGO_SETTINGS_MODULE
rather than the secret key. The part that trips me up is what value to assign to the environmental variable. Every iteration I've tried has yielded an error like ModuleNotFoundError: No module named 'BilliClub'
after running
(django2) 04:02 ~/BilliClub $ python ./pigs/csvImp.py
.
I am reloading the shell every time I try to change the variable in .env
so the postactivate
script runs each time and I'm making sure to re-enter my virtualenv. The problem just seems to be my inability to figure out how to path to the settings module.
The .env
file:
# /home/username/BilliClub/.env #
DJANGO_SETTINGS_MODULE="[what goes here???]"
Full path of my settings.py
is /home/username/BilliClub/BilliClub/settings.py
.
Abridged results from running tree
:
(django2) 04:33 ~ $ tree
.
├── BilliClub
│ ├── BilliClub
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── manage.py
│ ├── media
│ ├── pigs
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── apps.py
│ │ ├── bc2019.csv
│ │ ├── csvImp.py
│ │ ├── models.py
│ │ ├── models.pyc
│ │ ├── tests.py
│ │ ├── urls.py
│ │ └── views.py
│ └── ...
It looks like you should make csvImp
a custom management command and then
DJANGO_SETTINGS_MODULE
is "BilliClub.settings"
When you write your utility function as Django Management Command you get all the Django configuration for free, and the root directory of your command is the same as the root directory of the web app, and its the directory where manage.py
is.
Take a look at https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/