Skip to main content
 首页 » 编程设计

python之Heroku 上的项目中带有 Python 模块的 Node 应用程序未安装

2024年10月24日11lautakyan007

我有一个部署到 Heroku 的 Node (Express 服务器)项目运行良好,但我有一个我编写的小 Python 模块不需要服务器或新的 Heroku 实例,但它需要从安装一些第 3 方包 pip 。

我的问题是,虽然 Python 似乎运行良好(print() 在 .py 文件中完美运行),但它没有安装模块。我运行了 pip freeze 并将 requirements.txt 文件添加到我的项目的根目录中。该文件如下所示:

funcy==1.6 
numpy==1.10.2 
scipy==0.16.1 

但是当我部署 Heroku 时没有检测到任何 python 并且似乎没有安装它们并且我得到:

Traceback (most recent call last): File "src/blm/algo.py", line 4, in <module> from package import mvo File "/app/src/blm/package/mvo.py", line 1, in <module> import numpy as np ImportError: No module named 'numpy'  

Heroku 的输出

-----> Using set buildpack heroku/nodejs 
-----> Node.js app detected 
-----> Creating runtime environment 
 
       NPM_CONFIG_LOGLEVEL=error 
       NPM_CONFIG_PRODUCTION=true 
       NODE_ENV=production 
       NODE_MODULES_CACHE=true 
-----> Installing binaries 
       engines.node (package.json):  unspecified 
       engines.npm (package.json):   unspecified (use default) 
 
       Resolving node version (latest stable) via semver.io... 
       Downloading and installing node 5.8.0... 
       Using default npm version: 3.7.3 
-----> Restoring cache 
       Loading 2 from cacheDirectories (default): 
       - node_modules 
       - bower_components (not cached - skipping) 
-----> Building dependencies 
       Pruning any extraneous modules 
       Installing node modules (package.json) 
 
       > my-project@0.0.1 postinstall /tmp/build_c845357a6a491eb02f8ad4b043020ac6/my-project-7f7e7fe8632473e1f1f88f430b040396fb10671f 
       > webpack --config ./webpack-production.config.js --progress --colors 
 
Hash: 2ceaeac5876ff2e2463e 
       Version: webpack 1.12.14 
       Time: 15178ms 
       Asset    Size  Chunks             Chunk Names 
       bundle.js  455 kB       0  [emitted]  main 
       main.css   15 kB       0  [emitted]  main 
       + 272 hidden modules 
       Child extract-text-webpack-plugin: 
       + 2 hidden modules 
-----> Caching build 
       Clearing previous node cache 
       Saving 2 cacheDirectories (default): 
       - node_modules 
       - bower_components (nothing to cache) 
-----> Build succeeded! 
       ├── babel-core@6.7.2 
       ├── babel-loader@6.2.4 
       ├── babel-preset-es2015@6.6.0 
       ├── babel-preset-react@6.5.0 
       ├── babel-preset-stage-0@6.5.0 
       ├── bcrypt@0.8.5 
       ├── css-loader@0.23.1 
       ├── d3@3.5.16 
       ├── d3pie@0.1.9 
       ├── express@4.13.4 
       ├── extract-text-webpack-plugin@1.0.1 
       ├── jquery@2.2.2 
       ├── node-sass@3.4.2 
       ├── node-uuid@1.4.7 
       ├── react@0.14.7 
       ├── react-dom@0.14.7 
       ├── react-redux@4.4.1 
       ├── react-router@2.0.1 
       ├── redux@3.3.1 
       ├── sass-loader@3.2.0 
       ├── style-loader@0.13.0 
       └── webpack@1.12.14 
 
-----> Discovering process types 
       Procfile declares types -> web 
-----> Compressing... 
       Done: 30M 
-----> Launching... 
       Released v13 
       https://my-project.herokuapp.com/ deployed to Heroku 

===更新===

我还尝试添加一个“pipinstall”worker 来查看它是否会安装,但没有任何反应。

简介:

web: node server.js 
pipinstall: pip install -r requirements.txt 

请您参考如下方法:

您的应用需要多个构建包。

运行以下命令:

heroku 配置:设置 BUILDPACK_URL= https://github.com/ddollar/heroku-buildpack-multi

然后,将以下内容添加到项目根目录中名为 .buildpacks 的文件中:

https://github.com/heroku/heroku-buildpack-python.git 
https://github.com/heroku/heroku-buildpack-nodejs.git 

或者,您可以使用 heroku toolbelt 命令(heroku buildpacks:set 和 heroku buildpacks:add)配置您的多个 buildpack,如所述 here .