简体   繁体   English

使用django-admin.py时出错

[英]Error when using django-admin.py

Please bear with me as I'm new to Python/Django/Unix in general. 请耐心等待我,因为我是Python / Django / Unix的新手。
I'm learning how to use different settings.py files for local and production environments. 我正在学习如何为本地和生产环境使用不同的settings.py文件。 The following is from the section on the --settings option in the official Django docs page on django-admin.py , 以下内容来自django-admin.py上官方Django文档页面中--settings选项部分,

--settings Example usage: - 设置示例用法:

django-admin.py syncdb --settings=mysite.settings django-admin.py syncdb --settings = mysite.settings

My project is structured as following: 我的项目结构如下:

mysite
L manage.py 
L mysite
   L __init__.py  
   L local.py
   L urls.py
   L production.py 
   L wsgi.py

However when I run the following command from the parent mysite directory, 但是,当我从父mysite目录运行以下命令时,

$ django-admin.py runserver --settings=mysite.local $ django-admin.py runserver --settings = mysite.local

I get the following error: 我收到以下错误:

File "/Users/testuser/.virtualenvs/djdev/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'mysite.local' (Is it on sys.path?): No module named mysite.local

From what I gathered on various articles on the web, I think I need to add my project directory path to the PYTHONPATH variable in bash profile. 根据我在网络上的各种文章中收集的内容,我认为我需要将项目目录路径添加到bash配置文件中的PYTHONPATH变量中。 Is this the right way to go? 这是正确的方法吗?

EDIT: changed the slash to dot, but same error persists. 编辑:将斜线更改为点,但同样的错误仍然存​​在。

the --settings flag takes a dotted Python path, not a relative path on your filesystem. --settings标志采用虚线Python路径,而不是文件系统上的相对路径。

Meaning --settings=mysite/local should actually be --settings=mysite.local . 含义--settings=mysite/local应该实际上是--settings=mysite.local If your current working directory is your project root when you run django-admin , then you shouldn't have to touch your PYTHONPATH . 如果您运行django-admin时当前的工作目录是项目根目录,那么您不必触摸PYTHONPATH

You have to replace / with . 你必须替换/ .

$ django-admin.py runserver --settings=mysite.local

You can update PYTHONPATH in the manage.py too. 您也可以在manage.py更新PYTHONPATH。 Inside if __name__ == "__main__": add the following. if __name__ == "__main__":内部if __name__ == "__main__":添加以下内容。

import sys
sys.path.append(additional_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM