Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import settings; not on system path

Tags:

python

django

I'm trying to get Django working using virtualenv. I already got the hello world page online. However, there seems to be something wrong now because most of my commands give me the same error about myProject.settings

(myenv)user@mint /opt/myenv/myProject $ python manage.py startapp polls
Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/myenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "/opt/myenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/myenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 263, in fetch_command
    app_name = get_commands()[subcommand]
  File "/opt/myenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
    apps = settings.INSTALLED_APPS
  File "/opt/myenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/opt/myenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/opt/myenv/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myProject.settings' (Is it on sys.path?): No module named myProject.settings
(myenv)user@mint /opt/myenv/myProject $ 
like image 978
onepiece Avatar asked Nov 02 '13 10:11

onepiece


1 Answers

Your settings.py must be importable by python. This can be achieved several ways:

  • You have folder containing myProject in your PYTHONPATH environment, i.e. in sys.path , as stated by @amyangfei

Note that PYTHONPATH is a system environment variable used by python to build this sys.path and its modification is os-dependent. For mac os see this reference , for windows this. Note that PYTHONPATH is used only to construct sys.path, and its change with os.environ['PYTHONPATH']=<some_new_value> does not have significant effect.

However, it is possible to modify sys.path on the fly, to help import module find needed packages, see following example:

$ mkdir /tmp/tst
$ echo "print 'test'" > /tmp/tst/__init__.py
$ python -c "import tst"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named tst
$ python -c "import os; os.environ['PYTHONPATH']+=';/tmp'; import tst;"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named tst
$ python -c "import sys; sys.path.append('/tmp'); import tst;"
test
$ export PYTHONPATH=$PYTHONPATH:/tmp/
$ python -c "import tst"
test
  • You can run your python manage.py from a folder containing myProject.

For the latter to work, your project structure then has to be like follows:

manage.py
myProject/
    settings.py
  • 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.
like image 136
alko Avatar answered Sep 30 '22 14:09

alko