PythonAnywhere上Django应用加载报错:No module named 'simple_history'
解决PythonAnywhere上Django应用加载时
simple_history模块找不到的问题 问题重现
已在PythonAnywhere部署Django应用,安装了requirements.txt依赖,python manage.py migrate执行成功,但重载应用时出现以下错误:
2023-09-29 00:42:43,481: Error running WSGI application 2023-09-29 00:42:43,514: ModuleNotFoundError: No module named 'simple_history' 2023-09-29 00:42:43,514: File "/var/www/app_umrahpro_me_wsgi.py", line 16, in <module> 2023-09-29 00:42:43,514: application = get_wsgi_application() 2023-09-29 00:42:43,514: 2023-09-29 00:42:43,514: File "/home/HBNmughal/.local/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application 2023-09-29 00:42:43,515: django.setup(set_prefix=False) 2023-09-29 00:42:43,515: 2023-09-29 00:42:43,515: File "/home/HBNmughal/.local/lib/python3.10/site-packages/django/__init__.py", line 24, in setup 2023-09-29 00:42:43,515: apps.populate(settings.INSTALLED_APPS) 2023-09-29 00:42:43,515: 2023-09-29 00:42:43,515: File "/home/HBNmughal/.local/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate 2023-09-29 00:42:43,516: app_config = AppConfig.create(entry) 2023-09-29 00:42:43,516: 2023-09-29 00:42:43,516: File "/home/HBNmughal/.local/lib/python3.10/site-packages/django/apps/config.py", line 193, in create 2023-09-29 00:42:43,516: import_module(entry)
解决方案
该问题核心原因是WSGI进程使用的Python环境与依赖安装环境不一致,按以下步骤修复:
- 确认依赖安装的Python版本:在终端执行
pip --version,查看输出的Python版本(如python 3.10),确保和WSGI配置中指定的版本匹配。 - 定向安装依赖:如果版本不匹配或环境仍有问题,直接指定Python版本安装模块:
替换python3.10 -m pip install django-simple-historypython3.10为你实际使用的Python版本。 - 检查WSGI配置文件:打开
/var/www/app_umrahpro_me_wsgi.py,确认项目路径正确;如果使用了虚拟环境,确保激活虚拟环境的代码未被注释且路径准确:# 示例虚拟环境激活代码(按需启用) activate_this = '/home/HBNmughal/venv/bin/activate_this.py' with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this)) - 重载应用:完成上述操作后,回到PythonAnywhere的Web控制台点击"Reload"按钮重新加载应用。
内容的提问来源于stack exchange,提问作者HBNmughal
相关产品推荐
相关产品推荐

