Cyclic.sh部署Django应用出现django模块找不到错误求助
在Cyclic.sh部署Django应用时出现ModuleNotFoundError: No module named 'django'
我在cyclic.sh上部署Django应用,requirements.txt放在server目录根路径下,部署日志显示依赖已成功下载,但访问页面时却抛出ModuleNotFoundError: No module named 'django'错误。
部署日志
2024-01-07T23:37:49.675Z: Setting environment variables: CYCLIC_URL="http****************" CYCLIC_DB="lazy****************" CYCLIC_BUCKET_NAME="cycl****************" DJANGO_SETTINGS_MODULE="serv****************" REACT_APP_API_URL="********************" CYCLIC_APP_ID="lazy****************" 2024-01-07T23:37:49.051Z: [CYCLIC] cloning... 2024-01-07T23:37:50.334Z: From https://github.com/AnshUjlayan/CourseOverflow * branch 7fd1626cd9c8a1595531ba8851d0b94a56977fee -> FETCH_HEAD 2024-01-07T23:37:50.394Z: HEAD is now at 7fd1626 idek 2024-01-07T23:37:50.399Z: [CYCLIC] Building... 2024-01-07T23:37:50.601Z: Build Configuration: Root Path: /server Output Path: /server Main file: server/wsgi.py Static Site: false Runtime: python3.11 Branch: deployment Ref: 7fd1626cd9c8a1595531ba8851d0b94a56977fee 2024-01-07T23:37:50.604Z: [CYCLIC] verifying... 2024-01-07T23:37:51.346Z: [CYCLIC] using: python:3.11.2 pip:22.3.1 runtime:python3.11 [CYCLIC] building from: /server 2024-01-07T23:37:51.347Z: [CYCLIC] installing dependencies from /server/requirements.txt into /server 2024-01-07T23:38:02.767Z: [CYCLIC] bundling /server... 2024-01-07T23:38:07.459Z: [CYCLIC] packaging 166.65 MB... 2024-01-07T23:38:07.459Z: [CYCLIC] done packaging 2024-01-07T23:38:07.459Z: [CYCLIC] Deploying... 2024-01-07T23:38:21.846Z: deployed eu-west-1 - 14.337s 2024-01-07T23:38:21.847Z: SUCCESS took 34.5 seconds api deployed at: https://lazy-gray-lizard-belt.cyclic.app
访问页面时的错误日志
05:13:04.828 Traceback (most recent call last): File "/var/task/server/wsgi.py", line 11, in <module> from django.core.wsgi import get_wsgi_application ModuleNotFoundError: No module named 'django' [CYCLIC] ERROR: No response from server on port 3000
项目文件结构
- server - api - build_files.sh - manage.py - requirements.txt - staticfiles - utils - server - __init__.py - __pycache__ - asgi.py - settings.py - urls.py - wsgi.py
解决方法
- 手动添加依赖路径到Python环境:在
server/server/wsgi.py开头加入以下代码,确保Python能找到安装的依赖:
import sys import os # 将外层server目录添加到系统路径 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # 如果依赖安装在server下的site-packages目录,补充添加该路径 sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'lib', 'python3.11', 'site-packages'))
确认requirements.txt内容:检查
requirements.txt是否包含Django条目,例如Django==4.2.8,避免拼写错误或版本遗漏。调整Cyclic构建配置:在Cyclic项目设置中,将Root Path改为项目根目录,然后自定义构建命令为:
cd server && pip install -r requirements.txt
- 使用虚拟环境隔离依赖:修改
build_files.sh,添加虚拟环境创建与激活步骤,确保运行时使用正确的依赖环境:
python -m venv venv source venv/bin/activate pip install --upgrade pip pip install -r requirements.txt
内容的提问来源于stack exchange,提问作者AnshUjlayan
相关产品推荐
相关产品推荐

