LoginSignup
1
0

More than 1 year has passed since last update.

【有識者求む】pm2でPoetryを使ってみよう

Last updated at Posted at 2023-04-12

As of April 12th, 2023

次回はoceanic.jsの記事を書くとか言っていた気がするけどそれはまた次回以降
Poetryなる便利なPythonの依存関係管理ツールを知ったのが昨日。
今までは依存関係を個別にインストールしてpm2で運用してました。
さて、pm2で死活管理×Poetryで依存関係の管理は出来るのでしょうか??

筆者の環境

  • さくらのVPS(契約プランやリージョンは非公開)
  • Ubuntu 22.04 LTS
  • Node.js v18.15.0
  • pm2 v5.2.2(npm i -g pm2)
  • Python v3.11.3
  • Poetry v1.4.2(pip install poetry)
  • Git v2.40.0

やってみよう!

まずは確認

npmをpm2で死活管理する場合のコマンドは以下の通り

pm2 start npm --name appname -- start

Pythonをpm2で死活管理する場合のコマンドは以下の通り

pm2 start index.py --name appname --interpreter=python3

PoetryでPythonを動かす場合のコマンドは以下の通り

poetry run python3 index.py

いざまいる

じゃあ、こう?

pm2 start poetry --name appname -- run python3 index.py

以下が結果

SyntaxError: Invalid or unexpected token
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1195:20)
    at Module._compile (node:internal/modules/cjs/loader:1239:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Module._load (node:internal/modules/cjs/loader:972:12)
    at Object.<anonymous> (/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
    at Module._compile (node:internal/modules/cjs/loader:1275:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
/home/ubuntu/.local/bin/poetry:2
# -*- coding: utf-8 -*-
^

ん~!Nodeで動かそうとしてエラー出てますねえ。
Poetryはbashのコマンドだから、こう?

pm2 start index.py --name appname --interpreter=bash -- poetry run python3

結果は...

/home/ubuntu/TwiChecker/index.py: line 1: import: command not found
/home/ubuntu/TwiChecker/index.py: line 2: from: command not found
/home/ubuntu/TwiChecker/index.py: line 4: import: command not found
/home/ubuntu/TwiChecker/index.py: line 5: import: command not found
/home/ubuntu/TwiChecker/index.py: line 6: from: command not found
/home/ubuntu/TwiChecker/index.py: line 9: syntax error near unexpected token `client'
/home/ubuntu/TwiChecker/index.py: line 9: `client = discord.Bot()'

index.pyはbashのスクリプトじゃないからダメみたい...
これでどうだ!

pm2 start index.py --name appname --interpreter=python3 -- poetry run python3

この結果はこれ。

Traceback (most recent call last):
  File "/home/ubuntu/TwiChecker/index.py", line 6, in <module>
    from dotenv import load_dotenv
ModuleNotFoundError: No module named 'dotenv'

python-dotenvはインストールしてない代わりに
pyproject.tomlに記載しているので

poetry install

したときに入ってるはずだから、ローカルのPython環境で動かそうとしてるみたい。。

pyproject.toml
[tool.poetry]
name = "twichecker"
version = "1.0.0"
description = "Check Twitter's profile and send that as embed on Discord."
authors = ["nh-chitose"]
license = "MIT"
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"
py-cord = "^2.4.1"
python-dotenv = "^1.0.0"
requests = "^2.28.2"

[tool.poetry.group.dev.dependencies]
pylint = "^2.17.2"
isort = "^5.12.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

最終手段

以下の内容のindex.shを用意しました。

index.sh
#!/bin/sh
exec poetry run python3 ./index.py

これで動かないはずはない!

pm2 start index.sh --name appname --interpreter=bash

動きはしたけど、これって本当に死活管理できてるの...?

最後に

どなたか詳しい方教えて頂けないでしょうか?
どうなっているのか自分でもよくわからないです。

また、ぼちぼちTypeScript, Python, Discord関連の記事を書いていきたいと思っておりますので引き続きどうぞよろしくお願いいたします。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0