我想让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 $
Run Code Online (Sandbox Code Playgroud)
你settings.py必须可以通过python导入.这可以通过以下几种方式实现:
myProject在您的PYTHONPATH环境中包含的文件夹,即sys.path@amyangfei所述的文件夹注意,这PYTHONPATH是python用来构建sys.path它的系统环境变量,它的修改是依赖于os的.对于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
Run Code Online (Sandbox Code Playgroud)
python manage.py从包含的文件夹中运行您的 myProject.要使后者工作,您的项目结构必须如下所示:
manage.py
myProject/
settings.py
Run Code Online (Sandbox Code Playgroud)
DJANGO_SETTINGS_MODULE,settings而不是使用参数myProject.settings运行.python manage.py--settings=settings| 归档时间: |
|
| 查看次数: |
8074 次 |
| 最近记录: |