通过Heroku上的节点端点运行Python脚本时未找到模块解耦

2024-03-28 12:20:32 发布

您现在位置:Python中文网/ 问答频道 /正文

当人们通过我的提交路线提交图像时,我的项目运行图像转录脚本

使用以下开发脚本和命令pipenv run yarn dev,它在本地工作得很好:

"scripts": {
        "build": "tsc",
        "start": "pipenv run node build/index.js",
        "dev": "ts-node-dev src/index.ts",
        "test": "jest --watch",
        "typeorm": "ts-node ./node_modules/typeorm/cli.js -f src/ormconfig.ts",
        "typeorm-dev": "ts-node ./node_modules/typeorm/cli.js -c development -f src/ormconfig.ts",
        "connection": "ts-node src/util/get-connection.ts"
    },

尽管如此,开始脚本似乎并没有在Heroku上正确构建virtualenv

已安装构建包。首先是Python,然后是Node

在日志中,它看起来构建得很好

-----> Python app detected
-----> Need to update SQLite3, clearing cache
-----> Installing python-3.7.6
-----> Installing pip
-----> Installing dependencies with Pipenv 2018.5.18…
       Installing dependencies from Pipfile.lock (8b8885)…
-----> Installing SQLite3
Sqlite3 successfully installed.
-----> Node.js app detected

-----> Creating runtime environment

       NPM_CONFIG_LOGLEVEL=error
       NODE_ENV=production
       NODE_MODULES_CACHE=true
       NODE_VERBOSE=false

-----> Installing binaries
       engines.node (package.json):  unspecified
       engines.npm (package.json):   unspecified (use default)
       engines.yarn (package.json):  unspecified (use default)

       Resolving node version 12.x...
       Downloading and installing node 12.16.1...
       Using default npm version: 6.13.4
       Resolving yarn version 1.x...
       Downloading and installing yarn (1.22.0)...
       Installed yarn 1.22.0

-----> Restoring cache
       - node_modules

-----> Installing dependencies
       Installing node modules (yarn.lock)
       yarn install v1.22.0
       [1/4] Resolving packages...
       success Already up-to-date.
       Done in 0.72s.

-----> Build
       Running build (yarn)
       yarn run v1.22.0
       $ tsc
       Done in 14.12s.

-----> Caching build
       - node_modules

-----> Pruning devDependencies
       yarn install v1.22.0
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       info fsevents@2.1.2: The platform "linux" is incompatible with this module.
       info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       warning Ignored scripts due to flag.
       Done in 19.59s.

-----> Build succeeded!
-----> Discovering process types
       Procfile declares types     -> (none)
       Default types for buildpack -> web
-----> Compressing...
       Done: 113.9M
-----> Launching...
       Released v28
       https://***.herokuapp.com/ deployed to Heroku

出于某些原因,它还会安装SQLite:耸耸肩:

这是我的档案

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
google-cloud-datastore = "*"
google-cloud-vision = "*"
python-decouple = "*"
python-dotenv = "*"
pyinstaller = "*"
python-datauri = "*"

[requires]
python_version = "3.7"

下面是如何在我的脚本中导入解耦

from sys import stdin, stdout
from json import loads, dumps
from decouple import config
from datauri import DataURI
from google.cloud import vision
from google.cloud.vision import types
# from google.oauth2 import service_account
from google.auth import compute_engine
import os

这是我试图通过节点端点提交图像时的heroku日志

2020-03-03T14:00:23.367559+00:00 heroku[router]: at=info method=OPTIONS path="/submissions" host=***.herokuapp.com request_id=4cc77332-3f0f-4208-bc48-804644b7bf64 fwd="***" dyno=web.1 connect=0ms service=2ms status=204 bytes=315 protocol=https
2020-03-03T14:00:23.460776+00:00 app[web.1]: events.js:288
2020-03-03T14:00:23.460788+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2020-03-03T14:00:23.460788+00:00 app[web.1]:       ^
2020-03-03T14:00:23.460789+00:00 app[web.1]: 
2020-03-03T14:00:23.460789+00:00 app[web.1]: PythonShellError: ModuleNotFoundError: No module named 'decouple'
2020-03-03T14:00:23.460790+00:00 app[web.1]:     at PythonShell.parseError (/app/node_modules/python-shell/index.js:260:21)
2020-03-03T14:00:23.460790+00:00 app[web.1]:     at terminateIfNeeded (/app/node_modules/python-shell/index.js:139:32)
2020-03-03T14:00:23.460791+00:00 app[web.1]:     at ChildProcess.<anonymous> (/app/node_modules/python-shell/index.js:131:13)
2020-03-03T14:00:23.460791+00:00 app[web.1]:     at ChildProcess.emit (events.js:311:20)
2020-03-03T14:00:23.460791+00:00 app[web.1]:     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
2020-03-03T14:00:23.460792+00:00 app[web.1]:     ----- Python Traceback -----
2020-03-03T14:00:23.460792+00:00 app[web.1]:     File "src/util/scripts/transcription.py", line 3, in <module>
2020-03-03T14:00:23.460792+00:00 app[web.1]:       from decouple import config
2020-03-03T14:00:23.460793+00:00 app[web.1]: Emitted 'error' event on PythonShell instance at:
2020-03-03T14:00:23.460794+00:00 app[web.1]:     at terminateIfNeeded (/app/node_modules/python-shell/index.js:153:26)
2020-03-03T14:00:23.460794+00:00 app[web.1]:     at ChildProcess.<anonymous> (/app/node_modules/python-shell/index.js:131:13)
2020-03-03T14:00:23.460795+00:00 app[web.1]:     at ChildProcess.emit (events.js:311:20)
2020-03-03T14:00:23.460795+00:00 app[web.1]:     at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12) {
2020-03-03T14:00:23.460795+00:00 app[web.1]:   traceback: 'Traceback (most recent call last):\n' +
2020-03-03T14:00:23.460796+00:00 app[web.1]:     '  File "src/util/scripts/transcription.py", line 3, in <module>\n' +
2020-03-03T14:00:23.460796+00:00 app[web.1]:     '    from decouple import config\n' +
2020-03-03T14:00:23.460797+00:00 app[web.1]:     "ModuleNotFoundError: No module named 'decouple'\n",
2020-03-03T14:00:23.460797+00:00 app[web.1]:   executable: 'python3',
2020-03-03T14:00:23.460798+00:00 app[web.1]:   options: null,
2020-03-03T14:00:23.460798+00:00 app[web.1]:   script: 'src/util/scripts/transcription.py',
2020-03-03T14:00:23.460798+00:00 app[web.1]:   args: null,
2020-03-03T14:00:23.460799+00:00 app[web.1]:   exitCode: 1
2020-03-03T14:00:23.460799+00:00 app[web.1]: }
2020-03-03T14:00:23.470279+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-03-03T14:00:23.470482+00:00 app[web.1]: npm ERR! errno 1
2020-03-03T14:00:23.471369+00:00 app[web.1]: npm ERR! ***@0.0.1 start: `pipenv run node build/index.js`
2020-03-03T14:00:23.471528+00:00 app[web.1]: npm ERR! Exit status 1
2020-03-03T14:00:23.471653+00:00 app[web.1]: npm ERR! 
2020-03-03T14:00:23.471736+00:00 app[web.1]: npm ERR! Failed at the ***e@0.0.1 start script.
2020-03-03T14:00:23.471813+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-03-03T14:00:23.476689+00:00 app[web.1]: 
2020-03-03T14:00:23.477045+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-03-03T14:00:23.477046+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-03-03T14_00_23_472Z-debug.log
2020-03-03T14:00:23.578883+00:00 heroku[web.1]: State changed from up to crashed
2020-03-03T14:00:23.583792+00:00 heroku[web.1]: State changed from crashed to starting

Tags: tofromimportsrcmodulesnodeappnpm
1条回答
网友
1楼 · 发布于 2024-03-28 12:20:32

看起来解决方案是添加一个带有以下行的Procfileweb: yarn start

然后更改my package.json启动脚本:"start": "node build/index.js",

然后,我创建了一个requirements.txt文件并添加了所有依赖项:

altgraph>=0.17
cachetools>=4.0.0
certifi>=2019.11.28
chardet>=3.0.4
future>=0.18.2
google-api-core>=1.16.0
google-auth>=1.11.2
google-cloud-core>=1.3.0
google-cloud-datastore>=1.11.0
google-cloud-vision>=1.0.0
googleapis-common-protos>=1.51.0
grpcio>=1.27.2
idna>=2.9
joblib>=0.14.1
numpy>=1.18.1
pandas>=1.0.1
pefile>=2019.4.18
protobuf>=3.11.3
pyasn1>=0.4.8
pyasn1-modules>=0.2.8
PyInstaller>=3.6
Pyphen>=0.9.5
python-datauri>=0.2.8
python-dateutil>=2.8.1
python-decouple>=3.3
python-dotenv>=0.12.0
pytz>=2019.3
pywin32-ctypes>=0.2.0
requests>=2.23.0
rsa>=4.0
scikit-learn>=0.22.1
scipy>=1.4.1
six>=1.14.0
textstat>=0.6.0
urllib3>=1.25.8

相关问题 更多 >