简体   繁体   English

Django settings.py错误:不支持按文件名导入

[英]Django settings.py Error: Import by filename is not supported

I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. 我在虚拟环境中运行Django(使用virtualenv),我正在尝试添加自定义开发环境设置文件,以便在我开发时简化应用程序配置。 My plan was to do this with two lines of code 我的计划是使用两行代码执行此操作

if os.environ.get('DEVELOPMENT', None):
    from login import settings_dev

I've also tried import settings_def and from login.settings_dev import * . 我也尝试过import settings_deffrom login.settings_dev import * My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. 我的settings_dev.py文件与settings.py文件位于同一目录中,我的应用程序位于名为login的文件夹中。 When I run python login/manage.py syncdb I get this error: 当我运行python login/manage.py syncdb我收到此错误:

Error: Import by filename is not supported.

My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so: 我的搜索不断提出DJANGO_SETTINGS_MODULE(虽然我不确定它是如何发挥所有这一点 - 第一个Django app:]),所以只是一个FYI它在我的settings.py文件中设置如下:

os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'

I've also tried exporting it in my terminal, but I get the same error. 我也尝试在我的终端中导出它,但我得到了同样的错误。

Does anyone know how I can fix this/what I'm doing wrong here? 有谁知道我怎么能解决这个/我在这里做错了什么?

Make sure while passing relative address of file to use "." 确保在传递文件的相对地址时使用“。” instead of "/". 代替 ”/”。

I faced the same error what I actually did 我遇到了与实际相同的错误

"music/urls" “音乐/网址”

But it should be 但它应该是

"music.urls" “music.urls”

I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. 我在runserver命令执行中遇到了类似的错误,最后我发现由于安装了django版本的python版本不兼容而引发此错误。 There is two versions of python on my system and I had running django server by the wrong one. 我的系统上有两个版本的python,我运行了错误的django服务器。 Hope it could be helpful to someone. 希望它对某人有所帮助。

In the original settings.py , at the very end: 在原始settings.py ,最后:

try:
    from settings_dev import *
except ImportError:
    pass

Create settings_dev.py in the same directory as settings.py , and in it, add these two lines at the very top: 创建settings_dev.py在同一目录settings.py ,并在其中,加在最高层下面两行:

import sys
globals().update(vars(sys.modules['settings']))

Now add whatever development settings you want in this file. 现在在此文件中添加所需的任何开发设置。

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

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