5

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 $ 
onepiece
  • 3,279
  • 8
  • 44
  • 63
  • Share your project's structure. And also, have you synced your db? – Alex Parakhnevich Nov 02 '13 at 10:36
  • The project is very basic right now. There is only the MyProject folder in /opt/myenv with __init__, manage, settings, urls, and wsgi. I have synced my db (postgreSQL) and set up the superuser. Out of curiousity, I ran 'python manage.py syncdb' again and it gave me the same error as above. I'm not sure what I changed in my setup but I did get the Django webpage up on my browser so I assumed I did everything right, but apparently not. – onepiece Nov 02 '13 at 10:43

3 Answers3

7

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.
Community
  • 1
  • 1
alko
  • 46,136
  • 12
  • 94
  • 102
  • I tried your second option, there was no error anymore so it's fine. I don't understand the first solution. I came across it when trying to solve the error, but I don't understand what/how to access PYTHONPATH and its relationship with sys.path. I saw this sys.path.append('/path/to/MyProject') so I assume that adds paths to the PYTHONPATH so python knows where to link. Could you explain the first solution some more, and give more specific steps so it can work the same as the second solution? Thanks – onepiece Nov 02 '13 at 13:15
  • So am I supposed to add "sys.path.append('/folder/containing/settings.py');" to some file? I'm not sure which file though – onepiece Nov 02 '13 at 13:44
  • to `manage.py` you are running, of course. btw, this is true only if your settings_module=settings. for your initial post possible action was to add `sys.path.append('/folder/containing/myProject')`. – alko Nov 02 '13 at 13:51
  • So what is the permanent fix? Not interested in knowing what ineffective. – User Jan 31 '15 at 20:43
4

Have you tried something like this? Maybe will work.

python manage.py startapp polls --settings=myProject.settings
Marc Tudurí
  • 1,869
  • 3
  • 18
  • 22
1

check if '/opt/myenv/myProject' is in your python sys.path. (refer to python doc: sys.path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string) (http://docs.python.org/2/library/sys.html#sys.path)

➜  mysite  python
Python 2.6.8 (unknown, Aug 25 2013, 00:04:29) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/Users/xxx/xxx/', ... ]

similar questions here: ImportError: Could not import settings, Could not import settings 'myproject.settings' (Is it on sys.path?): No module named pinax

Community
  • 1
  • 1
amyangfei
  • 96
  • 1
  • 6