我的Django技能水平:菜鸟。
我很想正确设置DJANGO_SETTINGS_MODULE
,以便最终将我的模型导入脚本中。我在项目中使用virtualenv
。
这是我当前的错误:
ModuleNotFoundError: No module named 'dashex'
以及相应的feeder.py
脚本:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'dashex.settings'
sys.path.append('os.path.dirname(os.path.dirname(os.path.abspath(__file__)))')
import django
django.setup()
import zmq
import time
from time import sleep
import uuid
from Dashboard_app.models import AccountInformation
[...]
背景:
我首先使用了here发布的settings.configure()
解决方案。但是,此解决方案引发了this错误...因此,我希望DJANGO_SETTINGS_MODULE
能够解决问题。
根据文档(docu),我必须使用Django-admin
来设置DJANGO_SETTINGS_MODULE
。
我尝试过的事情:
在Windows的命令提示符中执行
$ set DJANGO_SETTINGS_MODULE=dashex.settings
,什么也没发生。
已执行
$ py manage.py set DJANGO_SETTINGS_MODULE=dashex.settings
引发错误unknown command set
在我项目的$ django-admin set DJANGO_SETTINGS_MODULE=dashex.settings
根目录中执行
manage.py
会引发错误no module named dashex
。
This答案建议执行上述操作,但是如何处理:
C:\temp\testproject> set DJANGO_SETTINGS_MODULE=testproject.settings
吗?
This解决方案建议将export DJANGO_SETTINGS_MODULE=<django_project>.settings
附加到我的虚拟环境中。这是什么意思,怎么做?
执行了
$ django-admin runserver --settings=dashex.settings
,这也会引发错误no module named dashex
。
已执行
$ py manage.py runserver --settings=dashex.settings
,它将以正确的设置启动服务器。 但是最初提到的错误仍然存在。
我的项目:
Settings.py:
import os.path
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ["127.0.0.1", "locahost"]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
#other Apps
'Wiki_app',
'rest_framework',
'Dashboard_app.apps.DashboardAppConfig'
]
@alasdair解决方案后的最新回溯:
Traceback (most recent call last):
File "C:\Users\Jonas Blickle\Desktop\dashex\Dashboard_app\feeder.py", line 6, in <module>
django.setup()
File "C:\Program Files\lib\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 79, in __getattr__
self._setup(name)
File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "C:\Program Files\lib\site-packages\django\conf\__init__.py", line 157, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Program Files\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'dashex'
[Finished in 0.37s]
工作解决方案:
import sys
sys.path.insert(0, r"C:\Users\Jonas\Desktop\Dashex")
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'Dashex.settings'
import django
django.setup()
from Dashboard.models import AccountInformation
[...]
答案 0 :(得分:2)
django-admin和manage.py不用于设置环境变量,因此问题中的命令,例如django-admin set DJANGO_SETTINGS_MODULE=dashex.settings
毫无意义。
在Windows上,可以在运行脚本之前在命令提示符下运行set DJANGO_SETTINGS_MODULE=dashex.settings
。您说不想使用外壳程序,因此在脚本中设置环境变量可能会更容易。
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'dashex.settings'
# Since feeder.py is in Dashboard_app, you need to add the parent directory
# to the python path so that dashex can be imported
# (without this you'll get the 'no module named dashex' error)
sys.path.append('..')
import django
django.setup()
# Now you can import models