4

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.

Community
  • 1
  • 1
cjones
  • 8,384
  • 17
  • 81
  • 175

2 Answers2

10

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")
Alasdair
  • 298,606
  • 55
  • 578
  • 516
0

"{{ project_name }}" appears to be a placeholder - manage.py isn't a template, and you are not using template syntax to place a variable in the string for os.environ.setdefault().

You need to use the correct project name/name of the module. In the example repo, the correct project name would be "project_name" (no braces), e.g. "project_name.settings.development" if you were using the development settings as named in that repo.

The error you see indicates the behavior clearly, as it is looking for a module named "{{ project_name }}" as that is the top-level (separated by dot) python module name provided, and it does not exist.

Jmills
  • 2,414
  • 2
  • 19
  • 22