Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
pipenv環境構築のすすめ
nkhr
June 14, 2020
Technology
0
160
pipenv環境構築のすすめ
nkhr
June 14, 2020
Tweet
Share
Other Decks in Technology
See All in Technology
さいきんのRaspberry Pi。 / osc22do-rpi
akkiesoft
6
5.1k
eBPF for Security Observability
lizrice
0
160
JJUG2022_spring_Keycloak (Red Hat Single Sign-on)
tinoue
0
200
miisan's career talk
mii3king
0
220
インフラのCI/CDはGitHub Actionsに任せた
mihyon
0
110
The application of formal methods in Kafka reliability engineering
line_developers
PRO
1
170
20220622_FinJAWS_あのときにAWSがあったらこうできた
taketakekaho
0
110
Scrum Fest Osaka 2022 フルリモート下でのチームビルディング
moritamasami
2
1.2k
マネージャーからみたスクラムと自己管理化
shibe23
0
1k
ROS再入門-はじめてのSLAM-
miura55
0
400
サイボウズの アジャイル・クオリティ / Agile Quality at Cybozu
cybozuinsideout
PRO
4
2.3k
IoTLT88-NTKanazawa-laundry-dry
yukima0707
0
220
Featured
See All Featured
The Invisible Customer
myddelton
110
11k
Creatively Recalculating Your Daily Design Routine
revolveconf
207
10k
The Illustrated Children's Guide to Kubernetes
chrisshort
15
36k
Git: the NoSQL Database
bkeepers
PRO
415
59k
jQuery: Nuts, Bolts and Bling
dougneiner
56
6.4k
Code Reviewing Like a Champion
maltzj
506
37k
From Idea to $5000 a Month in 5 Months
shpigford
373
44k
Web development in the modern age
philhawksworth
197
9.3k
4 Signs Your Business is Dying
shpigford
169
20k
The Invisible Side of Design
smashingmag
290
48k
How to Ace a Technical Interview
jacobian
265
21k
The Mythical Team-Month
searls
209
39k
Transcript
PipENV Python環境バージョン管理のすすめ @nkhr
2 実行時のPythonバージョンを意識している... [y/n] 仮想環境を利用している...[y/n] ライブラリは仮想環境ごとに管理してい る...[y/n] yesの方法を紹介します!
本日の内容 ⩥ Pipenvの概要 ⩥ Pipenvインストール ⩥ Pipenvの構成 ⩥ 個人利用のすすめ ⊳
デモ ⩥ プロジェクト利用TIPS(おまけ) ⩥ QA 3
Pipenvの概要 Pipenvで出来ることの紹介
シンプルに作業環境を管理 ⩥ 仮想環境の構築(virtualenv)+ライブラリ管理 (pip) を1つのツールで実現 ⊳ 仮想環境の作成・起動(virtualenv利用) ⊳ 利用するPythonのバージョンを指定 ⊳
インストールライブラリを管理 ▪ ハッシュ値によるバージョン管理 ▪ 依存関係の可視化(グラフ) ▪ PF、default/devの区別 5
Pipenvインストール Pipenvをインストールしてみよう(Let's TRY)
インストール方法 Mac $ python --version $ pip --version $ pip
install --upgrade pip $ brew install pipenv OR $ pip install --user pipenv Windows $ python --version $ pip --version $ pip install --upgrade pip $ pip install --user pipenv ※--userは、ユーザの HOME環境にインストー ルする Linux (検証なし) $ python --version $ pip --version $ brew install pipenv OR $ sudo dnf install pipenv ※brewはLinuxbrewをイ ンストール済の場合 7
WINDOWSのインストール+設定 Run Terminal <コマンドプロンプト> 検索バーで「cmd」と入力→ 検索結果から起動 下記はできている前提 $ python --version
$ pip --version $ echo %PATH% Install Pipenv $ pip install --user pipenv vertualenv.exe, virtualenv-clone.exe, pipenv-resolver.exeのイン ストール先がPATHに存在 しないというWarningが表 示される場合あり。 PATH setting Warningで表示されたパスを環境変数の PATHに追加 $ python -m site --user-site の表示パスの「site-packages」を 「Scripts」に変更 コントロールパネル>システムとセキュリ ティ>システム>システムの詳細設定 環境変数の変更 8
Pipenvの構成 利用するファイルや設定を紹介
利用ファイルと基本コマンド 10 作業ディレクトリ(test) Pipfile Pipfile.lock vertualenv環境(user base配下) bin/ lib/ ・・・
「ディレクトリ名」と、「ディレクトリパスのハッ シュ値」で管理 例)test-a3de50 存在しない場合は pipenv install [libraryname] で作成 install/shell/runを実行時 に自動的生成 pipenv --rmで削除 PIPENV_VENV_IN_PROJECT=1 (.venv)
仮想環境の保存場所変更 11 Mac .bash_profileや.zshrcに設定 export PIPENV_VENV_IN_PROJECT=1 sourceコマンドで適用or terminal再起動 ※macは下記の環境変数が設定されていな いとValueErrorが発生する場合あり
export LANG='ja_JP.UTF-8' export LC_ALL='ja_JP.UTF-8' Windows 環境変数に下記の変数を新規追加 (コントロールパネル>システムとセ キュリティ>システム>システムの詳細 設定) PIPENV_VENV_IN_PROFJECT=true ※一時的に設定(cmd.exe) $ SET PIPENV_VENV_IN_PROFJECT=true
個人利用のすすめ Pipenvをはじめてみよう
ディレクトリを作成 13 仮想環境を作成 Pipfileと.venv確認 requestsをinstall (default) pytestをinstall (dev) 依存関係を表示 仮想環境をactivate
ライブラリをインストールしてPythonを実行する $ mkdir test; cd test/; $ pipenv --python 3.8 (or --three, --two) $ ls -la $ pipenv install requests $ pipenv install pytest --dev $ pipenv graph $ pipenv shell; pip freeze; exit; コードの実行は、次に紹介 するpipenv runの方が使 い勝手がよいかも。
ディレクトリを作成 14 Pipfile/Pipfile.lock Copy Install Library確認 (testpyなし) main.pyを実行 仮想環境削除 作成済のPipfileからインストール/Python実行
$ cd ..; mkdir test2; cd test2; $ cp ../test/Pipfile* ./ $ pipenv install $pipenv run pip freeze ←pipenv run <scripts> $ pipenv run python main.py $ pipenv --rm ←or $ rm -rf .venv ※project配下の場合 システム上のPython verではなく、 Pipfile or実行時指定したPython ver でvenvを作成 (pyenvの場合は確認済) uninstall $ pipenv uninstall requests
TIPS https://pipenv.pypa.io/en/latest/ 15 Lock Clear $ pipenv lock --clear Update
$ pipenv update <pkg> ※<pkg>省略でall requirement > Pipfile $ pipenv install -r path/to/requirements.txt Usage $ pipenv -h security vulnerabilities Check $ pipenv check 仮想環境の保存場所 $ pipenv --venv
DEMO インストールできた人は一緒にやってみよう!
プロジェクト利用TIPS 開発プロセスで利用できる機能を紹介(おまけ)
Dev/PROD環境管理 Pipfile [dev-packages] セクション --devの場合のみ [packages] セクション 常にインストール (prod) pipenv
install (dev) pipenv install --dev Pipfile.lock Pipfileのlibrary verが新しくPipfile.lock が古い場合 $ pipenv install → PipfileのVer優先 $ pipenv install --deploy →Error $ pipenv sync →Pipfile.lock Ver優先 18
その他TIPS requirement.txt生成 (packagesのみ) $ pipenv lock -r > requirements.txt (dev-packages
+ packages) $ pipenv lock -r --dev (dev-packages のみ) $ pipenv lock -r --dev-only > dev-requirements.txt SystemのPIPを利用 (not virtualenv) $ pipenv install --system 環境変数(.env)の読み取り 「.env」ファイルは、pipenv run, pipenv shellの実行時 に自動で読み込まれるファイル 「.env」が別フォルダに存在する場合は 「PIPENV_DOTENV_LOCATION」で指定 「.env」を読み込みたくない場合は 「PIPENV_DONT_LOAD_ENV=1」を指定 19
Q&A 20 質問タイム
21 thanks! Enjoy Python Life!