launch Django project from any folder

Question:

In my project I use config file which is one level higher than manage.py.
I use ConfigParser to read it, but for Django to run correctly I need to be in the directory where manage.py is

Here is the part where the magic should happen

import os
import configparser

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

config = configparser.ConfigParser()
config.read('../config.ini', encoding='utf-8')

and if i am in the right directory, everything launches correctly

(.venv) kisha@LAPTOP-LLMM16ID:~/vmlist/vmlist_frontend/it$ ll
total 388
drwxr-xr-x 13 kisha kisha   4096 Aug  5 16:05 ./
drwxr-xr-x  7 kisha kisha   4096 Aug  8 10:43 ../
drwxr-xr-x  6 kisha kisha   4096 Aug  5 16:05 accounts/
drwxr-xr-x  4 kisha kisha   4096 Aug  8 10:43 api/
drwxr-xr-x  7 kisha kisha   4096 Aug  5 16:05 cluster/
drwxr-xr-x  5 kisha kisha   4096 Aug  5 16:05 description/
drwxr-xr-x  6 kisha kisha   4096 Aug  8 10:43 direction/
drwxr-xr-x  5 kisha kisha   4096 Aug  5 16:05 errors/
drwxr-xr-x  5 kisha kisha   4096 Aug  5 16:05 graphs/
drwxr-xr-x  6 kisha kisha   4096 Aug  5 16:05 it/
-rw-r--r--  1 kisha kisha    622 Jun 29 15:07 manage.py
drwxr-xr-x  2 kisha kisha   4096 Aug  2 11:47 migrations/
-rw-r--r--  1 kisha kisha  17976 Aug  8 10:36 request.log
drwxr-xr-x  7 kisha kisha   4096 Jun 29 15:39 settings/
drwxr-xr-x 11 kisha kisha   4096 Aug  5 16:05 vm_mon/
-rw-r--r--  1 kisha kisha 308854 Aug  8 15:13 vmlist.log
(.venv) kisha@LAPTOP-LLMM16ID:~/vmlist/vmlist_frontend/it$ python3.10 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
August 08, 2022 - 15:13:26
Django version 4.0.6, using settings 'it.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

but if i try to launch the project from somewhere else it crashes 🙁

(.venv) kisha@LAPTOP-LLMM16ID:~/vmlist$ ll
total 24
drwxr-xr-x  5 kisha kisha 4096 Jun 29 15:43 ./
drwxr-xr-x 10 kisha kisha 4096 Aug  2 11:40 ../
drwxr-xr-x  5 kisha kisha 4096 Jun 29 15:13 .venv/
-rw-r--r--  1 kisha kisha  353 Jun 29 15:22 Makefile
drwxr-xr-x  2 kisha kisha 4096 Jun 29 15:43 devtools/
drwxr-xr-x  7 kisha kisha 4096 Aug  8 10:43 vmlist_frontend/
(.venv) kisha@LAPTOP-LLMM16ID:~/vmlist$ python3.10 vmlist_frontend/it/manage.py runserver
Traceback (most recent call last):
  File "/home/kisha/vmlist/vmlist_frontend/it/manage.py", line 21, in <module>
    main()
  File "/home/kisha/vmlist/vmlist_frontend/it/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/kisha/vmlist/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/kisha/vmlist/.venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 386, in execute
    settings.INSTALLED_APPS
  File "/home/kisha/vmlist/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 87, in __getattr__
    self._setup(name)
  File "/home/kisha/vmlist/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 74, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/kisha/vmlist/.venv/lib/python3.10/site-packages/django/conf/__init__.py", line 183, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/kisha/vmlist/vmlist_frontend/it/it/settings.py", line 34, in <module>
    PATH_LOG = config['log'].get('path')
  File "/usr/lib/python3.10/configparser.py", line 964, in __getitem__
    raise KeyError(key)
KeyError: 'log'

I think it is possible to use BASE_DIR somehow, but everything I tried didn’t work.

Please help me.

Asked By: muhammed hilal

||

Answers:

this fixed my problem

config = configparser.ConfigParser()
config.read(os.path.join(sys.path[0], "../config.ini"), encoding='utf-8')

hope i helped anybody with my answer

Answered By: muhammed hilal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.