Python 导入错误:没有名为 mysite.settings 的模块(Django)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36210686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImportError: No module named mysite.settings (Django)
提问by u123
I have installed Djangoand mod_wsgi-expresson an ubuntu 15.10. Basically (notice I did notdo this as root):
我已经在 ubuntu 15.10 上安装了Django和mod_wsgi-express。基本上(请注意,我不是以 root 身份执行此操作的):
pip install Django
pip install mod_wsgi
Next I did:
接下来我做了:
~/.local/bin $ ./mod_wsgi-express start-server ~/mysite/mysite/wsgi.py
where: ~/mysite/mysite/wsgi.py
comes from the sample projectthat I uploaded to the above destination on the server. But I get an error when I try to access the website (Internal Server Error). When I look in the log I see:
其中:~/mysite/mysite/wsgi.py
来自我上传到服务器上上述目标的示例项目。但是当我尝试访问网站时出现错误(内部服务器错误)。当我查看日志时,我看到:
[Thu Mar 24 22:26:24.638043 2016] [wsgi:error] [pid 19469:tid 139785018738560] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 24 22:26:24.638070 2016] [wsgi:error] [pid 19469:tid 139785018738560] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 24 22:26:24.781030 2016] [wsgi:error] [pid 19469:tid 139785018738560] __import__(name)
[Thu Mar 24 22:26:24.781148 2016] [wsgi:error] [pid 19469:tid 139785018738560] ImportError: No module named mysite.settings
[Thu Mar 24 22:26:27.590300 2016] [wsgi:error] [pid 19469:tid 139784895194880] [remote 92.243.236.53:24636] mod_wsgi (pid=19469): Target WSGI script '/tmp/mod_wsgi-localhost:8000:1000/handler.wsgi' cannot be loaded as Python module.
So it seems that mysite.settingscannot be found/is not on the Python PATH (the file ~/mysite/mysite/settings.pydoes exist).
因此,似乎无法找到mysite.settings/不在 Python PATH 中(文件~/mysite/mysite/settings.py确实存在)。
Based on: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
基于:https: //docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
I tried to add:
我尝试添加:
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
But it did not help. I also tried to add the above path to the sample project to the python path based on: https://code.djangoproject.com/wiki/PythonPath
但它没有帮助。我还尝试将示例项目的上述路径添加到基于:https: //code.djangoproject.com/wiki/PythonPath的python路径
But same error. What am I missing?
但同样的错误。我错过了什么?
EDIT/Solution:
编辑/解决方案:
The problem was in the include path:
问题出在包含路径中:
import sys
#Wrong!
#sys.path.append("/home/user/mysite/mysite")
#Correct
sys.path.append("/home/user/mysite")
采纳答案by u123
The problem was in the include path:
问题出在包含路径中:
import sys
#Wrong!
#sys.path.append("/home/user/mysite/mysite")
#Correct
sys.path.append("/home/user/mysite")
回答by northsideknight
add this to your wsgi.pyfile
将此添加到您的wsgi.py文件中
path = '/home/path/to/project'
if path not in sys.path:
sys.path.append(path)
before setting
设置前
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
回答by porto
Please try:
请尝试:
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.environ['DJANGO_SETTINGS_MODULE'] = 'YOURAPP.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "YOURAPP.settings")
This is working for scripts in main project directory, as well ..
这适用于主项目目录中的脚本,以及..
回答by YaGuang Han
I had this error too, when I was trying to move a windows project to Debian.
当我尝试将 Windows 项目移动到 Debian 时,我也遇到了这个错误。
import sys
sys.path.append("your project path")
回答by Dima
Also, if you are using Visual Studio, check that your app properties for Django match the settings module that you are expecting. By default, it is set to $(MSBuildProjectName).settings, which was a problem for me since my project name and app name were not the same.
此外,如果您使用的是 Visual Studio,请检查您的 Django 应用程序属性是否与您期望的设置模块相匹配。默认情况下,它设置为 $(MSBuildProjectName).settings,这对我来说是个问题,因为我的项目名称和应用程序名称不同。
You can find this setting by right clicking on your app in the Solution Explorer and clicking on "Properties" and then the Django tab on the left.
您可以通过在解决方案资源管理器中右键单击您的应用程序并单击“属性”,然后单击左侧的 Django 选项卡来找到此设置。