pipenv プロジェクトのサービス化

www.instrumedley.net

つづきです。


環境

$ cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

$ uname -r
3.10.0-862.9.1.el7.x86_64

$ which python
/bin/python

$ python -V
Python 2.7.5


systemd サービスユニットの設定ファイル作成

# cat <<EOF >/etc/systemd/system/test.service 
[Unit]
Description=Pipenv Test
After=network.target

[Service]
Type=simple
Restart=always
WorkingDirectory=/home/testuser/blue
ExecStart=/home/testuser/.local/bin/pipenv run python server.py
User=testuser
Group=testuser

[Install]
WantedBy=multi-user.target
EOF


サービスの有効化と起動

# sysetmctl daemon-reload
# systemctl list-unit-files --type=service | grep test.service
test.service                                  disabled
# systemctl enable test
Created symlink from /etc/systemd/system/multi-user.target.wants/test.service to /etc/systemd/system/test.service.
# systemctl start test
$ curl 127.0.0.1:8000
Hello World

pipenv プロジェクトのコピー

www.instrumedley.net

つづきです。



環境

$ cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

$ uname -r
3.10.0-862.9.1.el7.x86_64

$ which python
/bin/python

$ python -V
Python 2.7.5

$ id
uid=1000(testuser) gid=1000(testuser) groups=1000(testuser)


flask アプリケーションの起動

例として flask アプリケーションを起動してみます。

$ cd ~/green
$ cat <<EOF >server.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World\n'

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=8000)
EOF

$ pipenv run python server.py 
 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
$ curl 127.0.0.1:8000
Hello World


プロジェクトのコピー

$ cd ~
$ cp -a green blue

$ cd blue
$ pipenv --venv
No virtualenv has been created for this project yet!

$ pipenv sync
Creating a virtualenv for this project...
Pipfile: /home/testuser/blue/Pipfile
(snip)
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!

$ pipenv run python -V
Python 3.7.0

$ pipenv graph
Flask==1.0.2
  - click [required: >=5.1, installed: 6.7]
  - itsdangerous [required: >=0.24, installed: 0.24]
  - Jinja2 [required: >=2.10, installed: 2.10]
    - MarkupSafe [required: >=0.23, installed: 1.0]
  - Werkzeug [required: >=0.14, installed: 0.14.1]

$ pipenv run python server.py
 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:8000/ (Press CTRL+C to quit)
$ curl 127.0.0.1:8000
Hello World

pipenv sync を実行することで、コピー元と同じ環境を再現することが確認できました。


つづきます。pipenv のプロジェクトを systemdでサービス化します。

www.instrumedley.net

pipenv の使い方

www.instrumedley.net

これのつづきです。


環境

$ cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

$ uname -r
3.10.0-862.9.1.el7.x86_64

$ which python
/bin/python

$ python -V
Python 2.7.5

$ id
uid=1000(testuser) gid=1000(testuser) groups=1000(testuser)


プロジェクトの条件

  • python-3.7
  • flask
  • tqdm

ここでは、 Webアプリケーションフレームワークである flask を例としてインストールします。 Python は 3.7.0 をインストールします。 また、アンインストールの例に tqdm を利用します。


python 3.7 をビルドするために必要なパッケージをインストール

$ sudo yum groupinstall -y "Development Tools"
$ sudo yum install -y readline-devel zlib-devel bzip2-devel sqlite-devel openssl-devel libXext.x86_64 libffi-devel

Python 3.7.0 をビルドするために必要なパッケージをインストールしておきます。


pipenv の使い方

プロジェクトディレクトリの作成と python-3.7 のインストール

$ mkdir -p ~/green
$ cd ~/green

$ pipenv --python 3.7.0
Warning: Python 3.7.0 was not found on your system...
Would you like us to install CPython 3.7.0 with pyenv? [Y/n]:
(snip)
Creating a Pipfile for this project...

$ ls
Pipfile

$ pyenv versions
* system (set by /home/testuser/.pyenv/version)
  3.7.0


実行

runは与えられたコマンドが仮想環境内でじっこうされます。shellは仮想環境が有効化されたシェルを起動されます。

$ pipenv run python -V
Python 3.7.0

$ pipenv shell
(green-jYRliImI) $ python -V
Python 3.7.0
(green-jYRliImI) $ exit
exit


python パッケージのインストールとアンインストール

tqdm のインストールとアンインストール
$ cd ~/green

$ pipenv install tqdm
Installing tqdm...
(snip)
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

$ ls
Pipfile  Pipfile.lock

$ pipenv uninstall tqdm
Un-installing tqdm...
(snip)
Removing tqdm from Pipfile...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (a65489)!
flask のインストールとpythonパッケージリスト
$ pipenv install flask

$ pipenv graph
Flask==1.0.2
  - click [required: >=5.1, installed: 6.7]
  - itsdangerous [required: >=0.24, installed: 0.24]
  - Jinja2 [required: >=2.10, installed: 2.10]
    - MarkupSafe [required: >=0.23, installed: 1.0]
  - Werkzeug [required: >=0.14, installed: 0.14.1]

$ pipenv run pip freeze -l
click==6.7
Flask==1.0.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1


仮想環境のディレクトリ情報

$ pipenv --venv
/home/testuser/.local/share/virtualenvs/green-jYRliImI


仮想環境の削除

$ pipenv --rm
Removing virtualenv (/home/testuser/.local/share/virtualenvs/green-jYRliImI)...


completion の設定

$ echo 'eval "$(pipenv --completion)"' >> ~/.basrh_profile
$ source ~/.bash_profile


プロジェクトのコピーにつづきます。 www.instrumedley.net