0

I'm working with Django 1.4 and Python 2.7.

I've encountered this issue before, but was able to work around it by adding the parent directory to sys.path using this code:

import sys

def rtrim(s, sep):
    return sep.join(s.split(sep)[0:-1])

sys.path.insert(0, rtrim(sys.path[0], '/'))

from django.core.management import setup_environ
import settings

setup_environ(settings)

Today, seemingly out of nowhere, it reared its ugly head once more. The directory structure is as follows:

Ingest/
  settings.py
  ingest/ (current working directory)
    test.py

enter image description here

Here are sys.path and part of the Traceback:

['/mnt/c/Users/Travis/Documents/GitHub/{redacted}/Ingest', '/mnt/c/Users/Travis/Documents/GitHub/{redacted}/Ingest/ingest', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'Ingest.settings' (Is it on sys.path?): No module named settings

As you can see, the parent directory is in sys.path.

Travis
  • 358
  • 3
  • 13
  • What are you trying to accomplish? Can you verify if `from django.conf import settings` works? – Selcuk May 21 '18 at 23:51

1 Answers1

0

I'm still not certain as to why this worked initially, for over a month, and then broke, seemingly for no reason, but after a couple hours of Googling I finally found the answer here: https://stackoverflow.com/a/19741994/9343059

Last option, if your manage.py and settings.py reside in same location, change environment variable DJANGO_SETTINGS_MODULE to settings instead of myProject.settings, or run python manage.py with --settings=settings arguments

I was able to fix the issue by replacing

from django.core.management import setup_environ
import settings

setup_environ(settings)

with

import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
Travis
  • 358
  • 3
  • 13