【问题标题】:Can't Install Django Packages in Heroku无法在 Heroku 中安装 Django 包
【发布时间】:2018-08-21 12:31:37
【问题描述】:

我刚刚将我的应用程序部署到 Heroku,打开应用程序时出现错误,它显示:“ModuleNotFoundError: No module named 'crispy_forms'”,MultiSelectField 也是如此。

我曾尝试在平台的控制台中安装这些包,但似乎没有效果,每次运行“heroku run pip freeze”时都会得到相同的包。

我尝试在本地控制台中运行以下命令:

heroku run bash
pip install django-crispy-forms
pip install django-multiselectfield
pip freeze # To check if the packages where installed

然后我发现一切似乎都很好,django-crispy-forms 和 django-multiselectfield 出现在“pip freeze”命令的包列表中,但是当我签入平台时,更新不存在。

如下所示,我在根文件夹中添加了“requirements.txt”文件,以及 Pipfile、Procfile。

“requirements.txt”文件添加了代码 pip freeze > requirements.txt:

argon2-cffi==18.1.0
bcrypt==3.1.4
certifi==2018.4.16
cffi==1.11.5
chardet==3.0.4
dj-database-url==0.4.2
Django==2.0
django-crispy-forms==1.7.2
-e git://github.com/AndrewIngram/django-extra- 
views.git@aa58167af84d89feea286f9567a72cc5941360e4#egg=django_extra_views
django-heroku==0.1.0
django-multiselectfield==0.1.8
django-session-timeout==0.0.3
gunicorn==19.7.1
idna==2.7
mailsnake==1.6.4
numpy==1.14.5
pandas==0.23.3
Pillow==5.2.0
pipenv==2018.5.18
psycopg2==2.7.3.2
pycparser==2.18
python-dateutil==2.7.3
pytz==2017.3
requests==2.19.1
six==1.11.0
stripe==2.4.0
urllib3==1.23
virtualenv==16.0.0
virtualenv-clone==0.3.0
whitenoise==3.3.1

我的 Pipfile 如下:

[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true


[packages]

django = "*"
gunicorn = "*"
django-heroku = "*"
django-crispy-forms = "*"
django-multiselectfield = "*"


[requires]

python_version = "3.6"

我的 git push heroku master 的输出如下:

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 711 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote:        Skipping installation, as Pipfile.lock hasn't changed since 
last 
deploy.
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 156.9M
remote: -----> Launching...
remote:        Released v14
remote:        https://blooming-headland-56472.herokuapp.com/ deployed to 
Herok
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/blooming-headland-56472.git
   d4535c9..5966fd9  master -> master

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    https://devcenter.heroku.com/articles/deploying-python

    您通过heroku run bash 所做的更改不会持续。退出 bash 后,dyno 将立即终止。

    我建议你在根文件夹中创建一个像pip freeze > requirements.txt 这样的 requirements.txt,或者在 Pipfile 中提及依赖项。

    在部署应用程序时会自动安装 Pipfile 或 requirements.txt 中指定的依赖项。

    在你的 Pipfile 中添加这个

    [packages]
    django-crispy-forms = "*"
    django = "*"
    django-multiselectfield = "*"
    

    【讨论】:

    • 我试过了,但没有成功,平台的问题还是一样。
    • 你提交并推送了这个新的 Pipfile 吗?
    • 当您像git push heroku master 一样推送部署时,发布输出。可能类似于:Counting objects: 1999, done. Compressing objects: 100% (616/616), done. Writing objects: 100% (1999/1999), 461.28 KiB | 0 bytes/s, done. Total 1999 (delta 1139), reused 1983 (delta 1131) remote: Compressing source files... done. remote: Building source: remote: -----> Python app detected remote: -----> Installing python-2.7.12 remote: $ pip install -r requirements.txt <=========== remote: -----> Launching... remote: Verifying deploy.... done
    • 我在问题描述中添加了输出。
    • 尝试pipenv lock 然后git add Pipfile Pipfile.lock,提交并推送
    【解决方案2】:

    不要尝试手动安装软件包。您应该创建一个 requirements.txt 文件并将其提交到 git,然后 Heroku 将在部署时自动安装包。

    您所做的不起作用的原因是heroku run bash - 或任何命令 - 创建了一个全新的测功机;您在那里安装软件包,但是当您退出时,测功机立即被杀死,其他测功机不受影响。

    【讨论】:

    • 我刚刚制作了 requirements.txt 文件并添加了以下代码:dj-database-url==0.4.2 Django==2.0 django-crispy-forms==1.7.2 django-heroku= =0.1.0 django-multiselectfield==0.1.8 gunicorn==19.7.1 pipenv==2018.5.18 psycopg2==2.7.3.2 pytz==2017.3 virtualenv==16.0.0 virtualenv-clone==0.3.0 whitenoise= =3.3.1 我还是不行。
    • 您是否将此文件添加到存储库的根目录?
    • 是的,与 Procfile、Pipfile 等相同的位置。我也尝试过 pip freeze > requirements.txt 代码,但似乎没有任何效果。
    猜你喜欢
    • 2020-10-28
    • 2022-08-08
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2017-12-05
    • 1970-01-01
    相关资源
    最近更新 更多