Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django relative import of project settings causing errors in moved manage.py file

Tags:

python

django

Following the directory structure in the answer here:

Best practice for Django project working directory structure

Reviewed the code on GitHub here to troubleshoot:

django-start-template

I moved the manage.py into a scripts directory. I modified the manage.py and wsgi.py to have these lines:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")

Instead of the default:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj_dir.settings")

Which I think is basically equivalent.

Anyway, this is the error I get when I run python manage.py runserver:

  File "C:\Users\ISH~1\DJANGO~1\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named '{{ project_name }}'

I have tried using relative imports, but really not getting anywhere to resolve the issue. Being a newb, I am sure the answer is pretty obvious, but I have spent a few hours reading about the problem and not really getting anywhere.

One suggested adding from . import * but this just resulted in:

Traceback (most recent call last):
  File "manage.py", line 4, in <module>
    from . import *
SystemError: Parent module '' not loaded, cannot perform relative import

virtualenv is active and I running manage.py from the correct directory. Works fine when it is not in /scripts/.

EDIT:

I didn't mentioned this, but regardless of whether in manage.py it is:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj_dir.settings")

Or:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name )).settings")

The result is always ImportError: No Module named 'xyz'. Fill in xyz with anything before settings.

Also, the above indicates, my project isn't 100% implemented like what is discussed in the first link. My settings.py is still in the default location. It looks like this basically:

~/projects/django-project

/proj_dir
    settings.py
    urls.py
    wsgi.py
/scripts
    manage.py

Also, I tried this because I thought well it might be looking for proj_dir in scripts and I actually want it to go up a level to look for proj_dir.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", ".proj_dir.settings")

This just brings up the following error.

TypeError: the 'package' argument is required to perform a relative import for '.proj_dir.settings'

That is when I started messing around with import and from . import because documentation was saying the error was related to that. Still couldn't fix the issue.

like image 242
cjones Avatar asked Oct 28 '25 05:10

cjones


1 Answers

If you have moved manage.py to the scripts directory, you will have to explicitly add the parent directory to the python path.

sys.path.append('..')

The line that sets the DJANGO_SETTINGS_MODULE environment variable should not be changed. Keep it as

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "proj_dir.settings")
like image 132
Alasdair Avatar answered Oct 30 '25 23:10

Alasdair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!