5

我正在尝试让 Django 使用 virtualenv 工作。我已经在线获得了 hello world 页面。但是,现在似乎出了点问题,因为我的大多数命令都给了我关于 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 $ 
4

3 回答 3

7

settings.py必须可以通过 python 导入。这可以通过几种方式实现:

  • 如@amyangfei 所述,myProject您的环境中包含文件夹PYTHONPATH,即 insys.path

请注意,这PYTHONPATH是 python 用来构建它的系统环境变量sys.path,它的修改是依赖于操作系统的。对于 mac os,请参阅此参考资料,对于 windows,请参阅参考资料。注意PYTHONPATH仅用于构造sys.path,其更改与os.environ['PYTHONPATH']=<some_new_value>没有显着影响。

但是,可以sys.path即时修改,以帮助import模块找到所需的包,请参见以下示例:

$ 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
  • 您可以python manage.py从包含 myProject.

要使后者工作,您的项目结构必须如下所示:

manage.py
myProject/
    settings.py
  • 最后一个选项,如果您的 manage.py 和 settings.py 位于同一位置,请将环境变量更改DJANGO_SETTINGS_MODULEsettings而不是myProject.settings,或python manage.py使用--settings=settings参数运行。
于 2013-11-02T12:41:05.540 回答
4

你有没有尝试过这样的事情?也许会奏效。

python manage.py startapp polls --settings=myProject.settings
于 2013-11-02T11:27:10.720 回答
1

检查是否'/opt/myenv/myProject'在您的 python sys.path 中。(参考 python 文档:sys.path[0],是包含用于调用 Python 解释器的脚本的目录。如果脚本目录不可用(例如,如果以交互方式调用解释器或从标准输入),path[0] 是空字符串)(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/', ... ]

类似的问题在这里:ImportError: Could not import settings , Could not import settings 'myproject.settings' (Is it on sys.path?): No module named pinax

于 2013-11-02T11:03:20.840 回答