我正在努力让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 $
答案 0 :(得分:7)
您的settings.py
必须可以通过python导入。这可以通过以下几种方式实现:
myProject
环境中包含PYTHONPATH
的文件夹,即sys.path
,@ amyangfei 请注意,PYTHONPATH
是python用来构建此sys.path
的系统环境变量,其修改依赖于os。对于mac os,请参阅this reference,了解Windows this。请注意,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
DJANGO_SETTINGS_MODULE
更改为settings
而不是myProject.settings
,或者运行python manage.py
--settings=settings
参数。答案 1 :(得分:4)
你尝试过这样的事吗?也许会奏效。
python manage.py startapp polls --settings=myProject.settings
答案 2 :(得分:1)
检查你的python sys.path中是否有'/opt/myenv/myProject'
。
(请参阅python doc: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