Skip to content

Commit 7e71173

Browse files
committed
[feat] Draft package management basic
1 parent 62eb181 commit 7e71173

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
Pythonのパッケージ管理の基本をおさえる
2+
======================================================================
3+
4+
上級の話に入る前準備
5+
6+
* インストールとは
7+
* 仮想環境とは
8+
9+
パッケージをインストールする、とは
10+
======================================================================
11+
12+
* ``pip install`` がしていること。それは
13+
* パッケージの実体のコード(Pythonファイルなど)のコピーをあなたのマシンに置く
14+
15+
.. TODO packageにまつわる用語 distributionとimport
16+
17+
実体のコードはどこから入手するの?
18+
--------------------------------------------------
19+
20+
* パッケージインデックス
21+
* (GitHubも可能)
22+
23+
PyPI
24+
--------------------------------------------------
25+
26+
* Python Package Index
27+
* ``pip install`` がデフォルトで見ている
28+
29+
インストールしたパッケージはマシンのどこにあるの?
30+
--------------------------------------------------
31+
32+
* :file:`site-packages` というディレクトリ
33+
34+
.. code-block:: shell
35+
36+
% python3 -m site --user-site
37+
/Users/private/Library/Python/3.12/lib/python/site-packages
38+
39+
仮想環境とは
40+
======================================================================
41+
42+
* **ディレクトリ**
43+
* Node.jsでいう :file:`node_modules`
44+
45+
仮想環境は **ディレクトリ**
46+
--------------------------------------------------
47+
48+
仮想環境とは、特定のバージョンの Python と幾つかの追加パッケージを含んだ Python インストールを構成するディレクトリです。
49+
50+
`12.1. はじめに(Python チュートリアル 12. 仮想環境とパッケージ) <https://docs.python.org/ja/3/tutorial/venv.html#introduction>`__
51+
52+
仮想環境を作るツール
53+
--------------------------------------------------
54+
55+
* 標準ライブラリの `venv <https://docs.python.org/ja/3/library/venv.html>`__
56+
* サードパーティの `virtualenv <https://virtualenv.pypa.io/en/latest/index.html>`__
57+
* virtualenvが先にあり、人気を受けて標準に入った(`PEP 405 <https://peps.python.org/pep-0405/>`__)
58+
59+
なぜ仮想環境が必要なのか
60+
--------------------------------------------------
61+
62+
* 仮想環境を使わないとき :file:`site-packages` が1つしかない
63+
* 同名ライブラリのバージョン違いが共存できない
64+
65+
Pythonでは開発プロジェクトごとに依存パッケージを分けよう
66+
------------------------------------------------------------
67+
68+
* プロジェクトごとに仮想環境を用意する
69+
* プロジェクトごとの :file:`site-packages`
70+
71+
.. 仮想環境以外のアプローチもあります
72+
73+
仮想環境の仕組み
74+
--------------------------------------------------
75+
76+
* 環境変数 ``PATH`` を更新
77+
78+
.. code-block:: shell
79+
80+
$ source .venv/bin/activate
81+
$ python -V
82+
Python 3.12.3
83+
$ .venv/bin/python -V # PATHが更新されていて、これが見つかっている
84+
Python 3.12.3
85+
$ type python
86+
python is /.../.venv/bin/python
87+
88+
環境を再現するには
89+
--------------------------------------------------
90+
91+
``pip freeze`` と ``pip install``
92+
93+
.. revealjs-break::
94+
95+
チュートリアル `12.3. pip を使ったパッケージ管理 <https://docs.python.org/ja/3/tutorial/venv.html#managing-packages-with-pip>`__
96+
97+
``python -m pip freeze`` はインストールされたパッケージ一覧を、``python -m pip install`` が解釈するフォーマットで生成します。
98+
99+
環境を再現するコマンド
100+
--------------------------------------------------
101+
102+
.. code-block:: shell
103+
104+
$ python -m pip freeze > requirements.txt
105+
$ python -m pip install -r requirements.txt
106+
107+
* freeze:動作する環境に入っているライブラリのバージョンを列挙
108+
* install:指定されたバージョンのライブラリをインストール
109+
110+
依存パッケージには2種類ある
111+
--------------------------------------------------
112+
113+
* direct
114+
* transitive
115+
116+
.. directをpip installする。transitiveが入ってくる
117+
118+
:command:`pip freeze` の小さな課題
119+
--------------------------------------------------
120+
121+
* transitiveの依存の削除が大変
122+
123+
.. TODO もっと例を入れよう

0 commit comments

Comments
 (0)