📦

Raspberry Pi OS + Pyenv + Pipenvで仮想環境作成

2022/08/30に公開
2

Pyenvを使用してPythonの複数バージョンを切り替えが可能になったので次は開発毎それぞれの開発環境を作成・管理するためにPipenvで開発環境を作成することにしました。

Pyenvのインストール

https://zenn.dev/technicarium/articles/00b32d390e82ec

環境

OS

>cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Pyenvのバージョン & Pythonインストール状況

$ pyenv --version
pyenv 2.3.3
$ pyenv versions
* system (set by /home/hoto/.pyenv/version)
  3.10.6

Pipenvインストール

pipの最新化

$ pip install --upgrade pip
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: pip in /usr/lib/python3/dist-packages (20.3.4)
Collecting pip
  Downloading https://www.piwheels.org/simple/pip/pip-22.2.2-py3-none-any.whl (2.0 MB)
     |████████████████████████████████| 2.0 MB 4.7 MB/s 
Installing collected packages: pip
  WARNING: The scripts pip, pip3 and pip3.9 are installed in '/home/hoto/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.2.2

$ pip -V
pip 22.2.2 from /home/hoto/.local/lib/python3.9/site-packages/pip (python 3.9)
$ pip3 -V
pip 22.2.2 from /home/hoto/.local/lib/python3.9/site-packages/pip (python 3.9)

Pipenvインストール

$ pip install pipenv
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting pipenv
  Downloading https://www.piwheels.org/simple/pipenv/pipenv-2022.8.19-py2.py3-none-any.whl (3.4 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.4/3.4 MB 1.4 MB/s eta 0:00:00
Requirement already satisfied: setuptools>=36.2.1 in /usr/lib/python3/dist-packages (from pipenv) (52.0.0)
Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from pipenv) (2020.6.20)
Collecting virtualenv-clone>=0.2.5
  Downloading https://www.piwheels.org/simple/virtualenv-clone/virtualenv_clone-0.5.7-py3-none-any.whl (7.6 kB)
Collecting virtualenv
  Downloading https://www.piwheels.org/simple/virtualenv/virtualenv-20.16.3-py2.py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 1.8 MB/s eta 0:00:00
Collecting platformdirs<3,>=2.4
  Downloading https://www.piwheels.org/simple/platformdirs/platformdirs-2.5.2-py3-none-any.whl (14 kB)
Collecting filelock<4,>=3.4.1
  Downloading https://www.piwheels.org/simple/filelock/filelock-3.8.0-py3-none-any.whl (10 kB)
Collecting distlib<1,>=0.3.5
  Downloading https://www.piwheels.org/simple/distlib/distlib-0.3.5-py2.py3-none-any.whl (466 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 467.0/467.0 kB 861.2 kB/s eta 0:00:00
Installing collected packages: distlib, virtualenv-clone, platformdirs, filelock, virtualenv, pipenv
  WARNING: The script virtualenv-clone is installed in '/home/hoto/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script virtualenv is installed in '/home/hoto/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The scripts pipenv and pipenv-resolver are installed in '/home/hoto/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed distlib-0.3.5 filelock-3.8.0 pipenv-2022.8.19 platformdirs-2.5.2 virtualenv-20.16.3 virtualenv-clone-0.5.7

パスの追加

pyenv同様~/.bashrcにパスを追加

export PATH="$PATH:$HOME/.local/bin"

現在のシェルにパスを反映させpipenvのコマンドパスを確認

$ source ~/.bashrc
$ which pipenv
/home/hoto/.local/bin/pipenv

仮想環境の作成

作業フォルダ(プログラム保存フォルダ)作成

$ cd ~/src/test_project
$ pipenv --python 3.10.6
$ pipenv --venv
/home/hoto/.local/share/virtualenvs/test_project-pLUeFHFK

テスト用に追加パッケージ(requests)をインストール

$ cd ~/src/test_project/
$ pipenv install requests
Installing requests...
Adding requests to Pipfile's [packages]...
✔ Installation Succeeded 
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success! 
Locking [dev-packages] dependencies...
Updated Pipfile.lock (a290a1)!
Installing dependencies from Pipfile.lock (a290a1)...
  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

作業フォルダ内のPipfileに追加したパッケージ情報が追加される

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

[packages]
requests = "*"

[dev-packages]

[requires]
python_version = "3.10"

テストプログラム作成&実行

main.py作成

作業フォルダ(~/src/test_project/)にrequestsパッケージを使用したテストプログラム(main.py)を作成。

import requests
def main():
    response = requests.get('https://www.google.co.jp')
    print(response.status_code)

if __name__ == '__main__':
    main()

仮想環境を有効(shell)にして実行、開発環境から抜ける(exit)

$ cd ~/src/test_project
$ pipenv shell
$ python main.py
200
exit

仮想環境を有効にせずコマンド(プログラム)実行

$ cd ~/src/test_project
$ pipenv run python main.py
200

開発環境を作業フォルダ配下に作成するように変更

これまでに作成した仮想環境を削除

$ cd ~/src/test_project
$ pipenv --rm
Removing virtualenv (/home/hoto/.local/share/virtualenvs/test_project-pLUeFHFK)...

~/src/test_project配下にはPipfile、Pipfile.lock、main.pyが残っているのでプログラムファイル以外のPipfile、Pipfile.lockを削除

環境変数の設定

~/.bashrcに環境変数を設定

export PIPENV_VENV_IN_PROJECT=true

仮想環境を作成

現在のシェルにパスを反映させてから仮想環境を作成

$ cd ~/src/test_project
$ source ~/.bashrc
$ pipenv --python 3.10.6
Creating a virtualenv for this project...
Pipfile: /home/hoto/src/test_project/Pipfile
Using /home/hoto/.pyenv/versions/3.10.6/bin/python3 (3.10.6) to create virtualenv...
⠇ Creating virtual environment...created virtual environment CPython3.10.6.final.0-64 in 1042ms
  creator CPython3Posix(dest=/home/hoto/src/test_project/.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/hoto/.local/share/virtualenv)
    added seed packages: pip==22.2.2, setuptools==63.4.1, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment! 
Virtualenv location: /home/hoto/src/test_project/.venv
Creating a Pipfile for this project...

作業フォルダ配下のファイル、フォルダを確認

$ ls -la
.venv
Pipfile
Pipfile.lock
main.py

.venvフォルダ配下を確認するとpythonの本体が格納されていることが確認できました。


やっと次はVisualStudioCodeのインストールです。

Discussion