首页 > 解决方案 > ImportError:无法导入 Django。将 Django 应用程序部署到 Heroku [Windows] 时出现问题

问题描述

我正在尝试将我的 Django 应用程序部署到 Heroku,但在成功构建应用程序后我一直遇到此错误。构建日志:

-----> Building on the Heroku-20 stack
-----> Using buildpack: https://github.com/heroku/heroku-geo-buildpack.git
-----> Geo Packages (GDAL/GEOS/PROJ) app detected
-----> Installing GDAL-2.4.0
-----> Installing GEOS-3.7.2
-----> Installing PROJ-5.2.0
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 50.9M
-----> Launching...
       Released v5
       https://metamedapp.herokuapp.com/ deployed to Heroku

这是构建成功后的日志:

2021-03-16T15:21:32.000000+00:00 app[api]: Build succeeded
2021-03-16T15:21:32.647557+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-16T15:21:36.820238+00:00 heroku[web.1]: Starting process with command `python manage.py runserver 0.0.0.0:49625`
2021-03-16T15:21:39.146005+00:00 app[web.1]: Traceback (most recent call last):
2021-03-16T15:21:39.146029+00:00 app[web.1]: File "manage.py", line 11, in main
2021-03-16T15:21:39.146029+00:00 app[web.1]: from django.core.management import execute_from_command_line
2021-03-16T15:21:39.146030+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'
2021-03-16T15:21:39.146030+00:00 app[web.1]:
2021-03-16T15:21:39.146031+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2021-03-16T15:21:39.146031+00:00 app[web.1]:
2021-03-16T15:21:39.146031+00:00 app[web.1]: Traceback (most recent call last):
2021-03-16T15:21:39.146032+00:00 app[web.1]: File "manage.py", line 22, in <module>
2021-03-16T15:21:39.146032+00:00 app[web.1]: main()
2021-03-16T15:21:39.146032+00:00 app[web.1]: File "manage.py", line 13, in main
2021-03-16T15:21:39.146033+00:00 app[web.1]: raise ImportError(
2021-03-16T15:21:39.146046+00:00 app[web.1]: ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
2021-03-16T15:21:39.193415+00:00 heroku[web.1]: Process exited with status 1
2021-03-16T15:21:39.280185+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-16T15:22:38.622544+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=metamedapp.herokuapp.com request_id=1fccc50d-d117-4cab-862c-91d05b765c4d fwd="86.7.108.29" dyno= connect= service= status=503 bytes= protocol=https
2021-03-16T15:22:39.701300+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=metamedapp.herokuapp.com request_id=723d001f-0cf6-4291-b884-e5d7772d9d3c fwd="86.7.108.29" dyno= connect= service= status=503 bytes= protocol=https

我肯定安装了 Django: django 版本

在我的应用程序开发过程中,我没有使用 virtualenv。此外,我不确定这是否重要,但我没有使用 python buildpack,因为它不断引发错误,说我的 gdal.dll 找不到。

这是我的 requirements.txt 文件,其中 django 作为依赖项之一:

asgiref==3.3.1
attrs==20.3.0
branca==0.4.2
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
click-plugins==1.1.1
cligj==0.7.1
dj-database-url==0.5.0
dj-static==0.0.6
django==3.1.5
django-heroku==0.3.1
django-toolbelt==0.0.1
folium==0.12.1
geographiclib==1.50
geojson==2.5.0
geopandas==0.9.0
geopy==2.1.0
googlemaps==4.4.2
gunicorn==20.0.4
idna==2.10
Jinja2==2.11.3
leaflet==0.0.3
MarkupSafe==1.1.1
munch==2.5.0
numpy==1.20.1
pandas==1.2.2
prettyprint==0.1.5
psycopg2==2.8.6
psycopg2-binary==2.8.6
pyproj==3.0.0.post1
python-dateutil==2.8.1
pytz==2020.5
requests==2.25.1
Shapely==1.7.1
six==1.15.0
sqlparse==0.4.1
static3==0.7.0
urllib3==1.26.3
waitress==2.0.0
whitenoise==5.2.0

这是我的Procfile:

web: python manage.py runserver 0.0.0.0:$PORT

该应用程序在本地运行良好,但只有当我将它推送到 heroku 时,它才会开始给我这个错误。我已经在这几个小时了,似乎无法解决它。

标签: pythondjangoheroku

解决方案


推荐阅读