django - Target WSGI scriptcannot be loaded as Python module
问题描述
I am looking for some help in migrating a web app from local machine to apache server.
-Python 3.7 -Django 2.1 -Apache 2.4
I also have a virtualenvironment created for the FleetManagRApp. My httpd.conf looks like this.
LoadFile "c:/python36/python36.dll"
LoadModule wsgi_module "c:/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/python36"
Listen 9981
<VirtualHost *:9981>
WSGIScriptAlias / "E:/DjangoSite/WebApps/WebApps/wsgi.py"
Alias /static "E:/DjangoSite/WebApps/FCApp/static"
<Directory "E:/DjangoSite/WebApps/FCApp/static">
Require all granted
</Directory>
<Directory "E:/DjangoSite/WebApps/WebApps">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Listen 3456
<VirtualHost *:3456>
path="E:/FleetManagRDjango/FleetManagRApp/"
WSGIScriptAlias / "E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py"
Alias /static "E:/FleetManagRDjango/FleetManagRApp/PMApproval/static"
<Directory "E:/FleetManagRDjango/FleetManagRApp/PMApproval/static">
Require all granted
</Directory>
<Directory "E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
wsgi.py looks like below
import os
import sys
path = 'E:/FleetManagRDjango/FleetManagRApp' # use your own username here
if path not in sys.path:
sys.path.append(path)
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'FleetManagRApp.settings')
application = get_wsgi_application()
Error Log
mod_wsgi (pid=21464): Target WSGI script 'E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=21464): Exception occurred processing WSGI script 'E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py'.
Traceback (most recent call last):\r
File "c:\\python36\\lib\\site-packages\\django\\db\\utils.py", line 115, in load_backend\r
return import_module('%s.base' % backend_name)\r
File "c:\\python36\\lib\\importlib\\__init__.py", line 126, in import_module\r
return _bootstrap._gcd_import(name[level:], package, level)\r
File "<frozen importlib._bootstrap>", line 994, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 971, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked\r
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\r
File "<frozen importlib._bootstrap>", line 994, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 971, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked\r
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\r
File "<frozen importlib._bootstrap>", line 994, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 971, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked\r
ModuleNotFoundError: No module named 'sql_server'\r
\r
During handling of the above exception, another exception occurred:\r
\r
Traceback (most recent call last):\r
File "E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py", line 19, in <module>\r
application = get_wsgi_application()\r
File "c:\\python36\\lib\\site-packages\\django\\core\\wsgi.py", line 13, in get_wsgi_application\r
django.setup(set_prefix=False)\r
File "c:\\python36\\lib\\site-packages\\django\\__init__.py", line 27, in setup\r
apps.populate(settings.INSTALLED_APPS)\r
File "c:\\python36\\lib\\site-packages\\django\\apps\\registry.py", line 108, in populate\r
app_config.import_models()\r
File "c:\\python36\\lib\\site-packages\\django\\apps\\config.py", line 202, in import_models\r
self.models_module = import_module(models_module_name)\r
File "c:\\python36\\lib\\importlib\\__init__.py", line 126, in import_module\r
return _bootstrap._gcd_import(name[level:], package, level)\r
File "<frozen importlib._bootstrap>", line 994, in _gcd_import\r
File "<frozen importlib._bootstrap>", line 971, in _find_and_load\r
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked\r
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked\r
File "<frozen importlib._bootstrap_external>", line 678, in exec_module\r
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed\r
File "E:/FleetManagRDjango/FleetManagRApp\\PMApproval\\models.py", line 6, in <module>\r
class ApprovalLog(models.Model):\r
File "c:\\python36\\lib\\site-packages\\django\\db\\models\\base.py", line 124, in __new__\r
new_class.add_to_class('_meta', Options(meta, app_label))\r
File "c:\\python36\\lib\\site-packages\\django\\db\\models\\base.py", line 330, in add_to_class\r
value.contribute_to_class(cls, name)\r
File "c:\\python36\\lib\\site-packages\\django\\db\\models\\options.py", line 214, in contribute_to_class\r
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())\r
File "c:\\python36\\lib\\site-packages\\django\\db\\__init__.py", line 33, in __getattr__\r
return getattr(connections[DEFAULT_DB_ALIAS], item)\r
File "c:\\python36\\lib\\site-packages\\django\\db\\utils.py", line 211, in __getitem__\r
backend = load_backend(db['ENGINE'])\r
File "c:\\python36\\lib\\site-packages\\django\\db\\utils.py", line 134, in load_backend\r
raise ImproperlyConfigured(error_msg)\r
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend.\r
Try using 'django.db.backends.XXX', where XXX is one of:\r
'mysql', 'oracle', 'postgresql', 'sqlite3'\r
Error was: No module named 'sql_server'\r
mod_wsgi (pid=21464): Target WSGI script 'E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py' cannot be loaded as Python module., referer: http://localhost:3456/approve
mod_wsgi (pid=21464): Exception occurred processing WSGI script 'E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py'., referer: http://localhost:3456/approve
Traceback (most recent call last):\r, referer: http://localhost:3456/approve
File "E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py", line 19, in <module>\r, referer: http://localhost:3456/approve
application = get_wsgi_application()\r, referer: http://localhost:3456/approve
File "c:\\python36\\lib\\site-packages\\django\\core\\wsgi.py", line 13, in get_wsgi_application\r, referer: http://localhost:3456/approve
django.setup(set_prefix=False)\r, referer: http://localhost:3456/approve
File "c:\\python36\\lib\\site-packages\\django\\__init__.py", line 27, in setup\r, referer: http://localhost:3456/approve
apps.populate(settings.INSTALLED_APPS)\r, referer: http://localhost:3456/approve
File "c:\\python36\\lib\\site-packages\\django\\apps\\registry.py", line 78, in populate\r, referer: http://localhost:3456/approve
raise RuntimeError("populate() isn't reentrant")\r, referer: http://localhost:3456/approve
RuntimeError: populate() isn't reentrant\r, referer: http://localhost:3456/approve
The first app is not managed by virtual environment (uses python 3.6 and django 1.7) and I cant control that. I am unable to point my app to the virtual env. I am by no means a web developer. Just one of the enthusiastic tool builders. How can I point my app to the venv without touching the first app ?
解决方案
我能够找到解决方案。
以下是步骤:
- 抛弃了 Python 自带的 venv
- 在服务器中使用 virtualwrapper-win 创建一个新的虚拟环境
- 点安装 mod_wsgi
- 运行 mod_wsgi 以获取 httpd.conf 的路径
LoadModule wsgi_module "c:/users/username/envs/venv/lib/sitepackages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd")
- 修改wsgi.py
- 指向并激活虚拟环境
以下文件:
wsgi.py
activate_this = 'c:/users/username/envs/venv/Scripts/activate_this.py'
# execfile(activate_this, dict(__file__=activate_this))
exec(open(activate_this).read(),dict(__file__=activate_this))
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('c:/users/username/envs/venv/lib/site-packages/')
# Add the app's directory to the PYTHONPATH
sys.path.append('E:/FleetManagRDjango/FleetManagRApp')
sys.path.append('E:/FleetManagRDjango/FleetManagRApp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'FleetManagRApp.settings'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FleetManagRApp.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
httpd.conf 看起来像:
LoadFile "c:/python36/python36.dll"
LoadModule wsgi_module "c:/python36/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "c:/python36"
WSGIScriptAlias / "E:/DjangoSite/WebApps/WebApps/wsgi.py"
WSGIPythonPath "E:/DjangoSite/WebApps/FCApp/"
Listen 9981
<VirtualHost *:9981>
Alias /static "E:/DjangoSite/WebApps/FCApp/static"
<Directory "E:/DjangoSite/WebApps/FCApp/static">
Require all granted
</Directory>
<Directory "E:/DjangoSite/WebApps/WebApps">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Listen 3456
<VirtualHost *:3456>
LoadModule wsgi_module "c:/users/username/envs/venv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIScriptAlias / "E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp/wsgi.py"
Alias /static "E:/FleetManagRDjango/FleetManagRApp/PMApproval/static"
<Directory "E:/FleetManagRDjango/FleetManagRApp/PMApproval/static">
Require all granted
</Directory>
<Directory "E:/FleetManagRDjango/FleetManagRApp/FleetManagRApp">
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
推荐阅读
- python - 获取 nltk k 的惯性意味着使用 cosine_similarity 进行聚类
- django - 如何使用 Django 序列化程序更新自定义用户模型的字段
- google-apps-script - 如何将单元格数据从一个谷歌电子表格发送到另一个,但前提是单元格颜色正确?
- git - 推送到 Github 提示输入错误 url 的密码
- php - php从小时偏移量确定时区名称
- python - 如何使用 discord.py 使用我的不和谐机器人编辑嵌入颜色
- android - RXJAVA ROOM 安卓
- linux - 如何将 patchelf 与 --set-interpreter 一起使用?
- deep-learning - 使用 TorchText (PyTorch) 进行语言翻译
- python - 如何在 Python 中选择独立加权的数字?