我正在尝试在Windows服务器上的Apache上启动我的Django应用程序。
我的wsgi.py
文件有问题。
我在服务器日志中收到以下错误:
mod_wsgi (pid=1332): Target WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=1332): Exception occurred processing WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py'.
Traceback (most recent call last):\r
File "C:/Apache24/htdocs/OctopusWeb/wsgi.py", line 11, in <module>\r
import OctopusWeb\r
ImportError: No module named 'OctopusWeb'\r
mod_wsgi (pid=1332): Target WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=1332): Exception occurred processing WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py'.
Traceback (most recent call last):\r
File "C:/Apache24/htdocs/OctopusWeb/wsgi.py", line 11, in <module>\r
import OctopusWeb\r
ImportError: No module named 'OctopusWeb'\r
mod_wsgi (pid=1332): Target WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=1332): Exception occurred processing WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py'.
Traceback (most recent call last):\r
File "C:/Apache24/htdocs/OctopusWeb/wsgi.py", line 11, in <module>\r
import OctopusWeb\r
ImportError: No module named 'OctopusWeb'\r
OctopusWeb
是settings.py
和wsgi.py
文件所在的文件夹的名称。
我的wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OctopusWeb.settings")
application = get_wsgi_application()
我使用的所有文件都在C:\Apache24\htdocs
目录中。
你知道我做错了吗?
我编辑wsgi.py
为@JoranBeasley写道
import os
os.environ['PYTHONPATH'] = os.path.abspath(os.path.dirname(__file__))
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OctopusWeb.settings")
application = get_wsgi_application()
现在我收到以下错误
mod_wsgi (pid=1108): Target WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=1108): Exception occurred processing WSGI script 'C:/Apache24/htdocs/OctopusWeb/wsgi.py'.
Traceback (most recent call last):\r
File "C:/Apache24/htdocs/OctopusWeb/wsgi.py", line 16, in <module>\r
application = get_wsgi_application()\r
File "c:\\python34\\lib\\site-packages\\django\\core\\wsgi.py", line 13, in get_wsgi_application\r
django.setup(set_prefix=False)\r
File "c:\\python34\\lib\\site-packages\\django\\__init__.py", line 22, in setup\r
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)\r
File "c:\\python34\\lib\\site-packages\\django\\conf\\__init__.py", line 56, in __getattr__\r
self._setup(name)\r
File "c:\\python34\\lib\\site-packages\\django\\conf\\__init__.py", line 41, in _setup\r
self._wrapped = Settings(settings_module)\r
File "c:\\python34\\lib\\site-packages\\django\\conf\\__init__.py", line 110, in __init__\r
mod = importlib.import_module(self.SETTINGS_MODULE)\r
File "c:\\python34\\lib\\importlib\\__init__.py", line 109, in import_module\r
return _bootstrap._gcd_import(name[level:], package, level)\r
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked\r
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed\r
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked\r
ImportError: No module named 'OctopusWeb'\r
答案 0 :(得分:1)
你不能使用:
import os
os.environ['PYTHONPATH'] = os.path.abspath(os.path.dirname(__file__))
因为在此过程中设置PYTHONPATH
为时已晚。
使用:
import os
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
同时阅读:
解释了如何设置Python虚拟环境和Python模块搜索路径。
答案 1 :(得分:0)
您需要将该目录添加到系统路径或python路径,以便python可以找到您的模块
import os
os.environ['PYTHONPATH'] = os.path.abspath(os.path.join(os.path.dirname(__file__),".."))
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OctopusWeb.settings")
application = get_wsgi_application()