简体   繁体   中英

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. 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 * . 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. When I run python login/manage.py syncdb I get this error:

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:

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"

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. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.

In the original settings.py , at the very end:

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:

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

Now add whatever development settings you want in this file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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