如何将Python依赖项添加到Heroku节点服务器?

9

我有一个在Node上运行的Heroku应用程序,但我需要能够在此服务器上运行Python脚本。我正在尝试安装我的Python依赖项,但无法使其正常工作。

我已经向我的项目添加了python和node buildpacks,并创建了一个虚拟环境并成功安装了requirements.txt,但我仍然遇到模块未找到的错误。

如何正确地在Heroku Node服务器上安装Python软件包?

Heroku显示我已经正确设置了两个buildpacks:

heroku buildpacks --app <my app>

输出:

1. heroku/nodejs
2. heroku/python 

如果我尝试安装 requirements.txt 文件:
$ pip install -r requirements.txt

它说要求已经满足了,我猜这是指我的本地环境。

Requirement already satisfied (use --upgrade to upgrade): requests==2.7.0 in /Library/Python/2.7/site-packages (from -r requirements.txt (line 1))
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4==4.5.3 in /Library/Python/2.7/site-packages (from -r requirements.txt (line 2))

但是如果我尝试运行我的Python脚本,会出现一个找不到模块的错误:

Traceback (most recent call last): File "Webcrawler.py", line 3, in from urllib.request import urlopen ImportError: 找不到名为request的模块


你试过这个解决方案吗 - https://dev59.com/YZXfa4cB1Zd3GeqPdVa2? - hashcode55
@hashcode55 是的,我尝试过了,但我认为那是一个过时的答案,因为运行它不会生成 .buildpacks 文件。我尝试手动创建它,但没有成功。 - 123
@123 你能展示一下从Heroku获取的构建日志吗? - Michał Młoźniak
2个回答

9
我这样解决了它:
  1. I created (If there is not) the file "requirements.txt" directly into the root of the entrypoint (index.js, in my case)
  2. I manually added the libraries with the specific versions into the file, in my case like this:

    requests==2.7.0
    beautifulsoup4==4.5.3
    
  3. I created (If there is not) the file "runtime.txt" with with just one line: the python version i need, in my case:

    python-2.7.14
    
  4. I created the multibuildpacks with the two commands:

    heroku buildpacks:set heroku/python
    heroku buildpacks:add --index 1 heroku/nodejs
    
  5. Finally, I continued with the git commands:

    git add requirements.txt runtime.txt
    git commit requirements.txt runtime.txt -m "requirements"
    git push heroku master
    

0

这正是我所做的。如我在问题中所述,我已经为我的项目添加了适当的构建包,但除非我使用 virtualenv,否则 pip 仍然会尝试安装在我的本地计算机上。我可以成功地使用 virtualenv 安装依赖项,但出于某些原因它无法解决模块错误。 - 123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接