これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2025/06/19 08:39:27.157 (台灣標準時)
自分で書いた Python のコードをパッケージとしてまとめ、別の計算機でも簡 便にインストールできるような配布物を作る方法については、以下を参照すれ ばよいようでござる。
例えば、以下のファイルを用意すればよいようでござる。
mypkg ├── LICENSE ├── README.md ├── pyproject.toml ├── src │ └── mypkg │ ├── __init__.py │ ├── calc.py │ └── const.py └── tests
実際に作ってみるでござる。まず、ディレクトリを作るでござる。
% mkdir -p mypkg/src/mypkg mypkg/tests % ls -lF mypkg total 16 drwxr-xr-x 3 daisuke taiwan 4096 Jun 11 16:23 src/ drwxr-xr-x 2 daisuke taiwan 4096 Jun 11 16:23 tests/ % ls -lF mypkg/src total 8 drwxr-xr-x 2 daisuke taiwan 4096 Jun 11 16:23 mypkg/
mypkg/src/mypkg/const.py の内容は以下の通りでござる。
# # Time-stamp: <2025/06/11 16:27:38 (UT+08:00) daisuke> # # value of pi pi = 3.141592653589793 # value of speed of light in m/s c = 299792458.0
mypkg/src/mypkg/calc.py の内容は以下の通りでござる。
# # Time-stamp: <2025/06/11 16:28:45 (UT+08:00) daisuke> # # importing math module import math # value of pi pi = math.pi # a function to calculate sin (x) for given x def mysin (angle, unit): # angle in radian if (unit == 'radian'): angle_rad = angle elif (unit == 'degree'): angle_rad = angle / 180.0 * pi else: print (f'ERROR:') print (f'ERROR: unit must be either "radian" or "degree"') print (f'ERROR:') # calculation of sin (angle) sin_angle = angle_rad - angle_rad**3 / (2*3) + angle_rad**5 / (2*3*4*5) \ - angle_rad**7 / (2*3*4*5*6*7) + angle_rad**9 / (2*3*4*5*6*7*8*9) \ - angle_rad**11 / (2*3*4*5*6*7*8*9*10*11) \ + angle_rad**13 / (2*3*4*5*6*7*8*9*10*11*12*13) \ - angle_rad**15 / (2*3*4*5*6*7*8*9*10*11*12*13*14*15) \ + angle_rad**17 / (2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17) # returning sin (angle) return sin_angle
mypkg/src/mypkg/__init__.py の内容は以下の通りでござる。
# # Time-stamp: <2025/06/11 16:25:08 (UT+08:00) daisuke> # # list of available module files __all__ = [ "const", "calc" ]
LICENSE ファイルが必要でござる。 GPL ならば、以下のようにすればよいよ うでござる。
% wget -O LICENSE https://www.gnu.org/licenses/gpl-3.0.txt --2025-06-11 16:32:27-- https://www.gnu.org/licenses/gpl-3.0.txt Resolving www.gnu.org (www.gnu.org)... 209.51.188.116, 2001:470:142:5::116 Connecting to www.gnu.org (www.gnu.org)|209.51.188.116|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 35149 (34K) [text/plain] Saving to: ‘LICENSE’ LICENSE 100%[===================>] 34.33K 160KB/s in 0.2s 2025-06-11 16:32:28 (160 KB/s) - ‘LICENSE’ saved [35149/35149]
README.md ファイルを作るでござる。
% echo "# README.mf file of "mypkg" Python package" > README.md
pyproject.toml ファイルを作るでござる。以下のような内容とするでござる。
# # Time-stamp: <2025/06/11 16:36:50 (UT+08:00) daisuke> # [build-system] requires = ["hatchling >= 1.26"] build-backend = "hatchling.build" [project] name = "mypkg" version = "1.0" authors = [ { name = "Daisuke", email="abc@def.ghi" }, ] description = "My very first Python package" readme = "README.md" requires-python = ">=3.10" classifiers = [ "Programming Language :: Python :: 3", "Operating System :: OS Independent", ] license = "GPL-3.0-or-later" license-files = ["LICEN[CS]E*"]
これでよいようでござる。以下のコマンドを実行すれば、配布パッケージが作 成されるでござる。
% python3.13 -m build * Creating isolated environment: venv+pip... * Installing packages in isolated environment: - hatchling >= 1.26 * Getting build dependencies for sdist... * Building sdist... * Building wheel from sdist * Creating isolated environment: venv+pip... * Installing packages in isolated environment: - hatchling >= 1.26 * Getting build dependencies for wheel... * Building wheel... Successfully built mypkg-1.0.tar.gz and mypkg-1.0-py3-none-any.whl
dist ディレクトリが作られ、その中にパッケージが置かれているでござる。
% ls -lF dist/ total 64 -rw-r--r-- 1 daisuke taiwan 14302 Jun 11 16:41 mypkg-1.0-py3-none-any.whl -rw-r--r-- 1 daisuke taiwan 13455 Jun 11 16:40 mypkg-1.0.tar.gz
mypkg-1.0.tar.gz に含まれるファイルを見てみるでござる。
% tar tzvf dist/mypkg-1.0.tar.gz -rw-r--r-- 0 0 0 123 Feb 2 2020 mypkg-1.0/src/mypkg/__init__.py -rw-r--r-- 0 0 0 946 Feb 2 2020 mypkg-1.0/src/mypkg/calc.py -rw-r--r-- 0 0 0 147 Feb 2 2020 mypkg-1.0/src/mypkg/const.py -rw-r--r-- 0 0 0 35149 Feb 2 2020 mypkg-1.0/LICENSE -rw-r--r-- 0 0 0 41 Feb 2 2020 mypkg-1.0/README.md -rw-r--r-- 0 0 0 501 Feb 2 2020 mypkg-1.0/pyproject.toml -rw-r--r-- 0 0 0 381 Feb 2 2020 mypkg-1.0/PKG-INFO
なぜ、日付が 2020 年 02 月 02 日になっているのかは不明でござる。
mypkg-1.0.tar.gz をインストールするには、以下のようにすればよいようで ござる。
% pip-3.13 install dist/mypkg-1.0.tar.gz Defaulting to user installation because normal site-packages is not writeable Processing ./dist/mypkg-1.0.tar.gz Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: mypkg Building wheel for mypkg (pyproject.toml) ... done Created wheel for mypkg: filename=mypkg-1.0-py3-none-any.whl size=14302 sha256=f9da9dd0c2bef133f43e4d1468408f37e0bc5b5d40cd673df00ef50682eece5f Stored in directory: /home/daisuke/.cache/pip/wheels/56/a5/b0/1c914606285d3e73848e32a4852625c24c7d06be42e6fcb359 Successfully built mypkg Installing collected packages: mypkg Successfully installed mypkg-1.0
インストールが済むと、 ~/.local にインストールされたファイ ルが置かれているようでござる。
mypkg を使うプログラムを書いてみるでござる。
#!/usr/pkg/bin/python3.13 # # Time-stamp: <2025/06/11 16:49:32 (UT+08:00) daisuke> # # importing mypkg module import mypkg.calc import mypkg.const # value of c c = mypkg.const.c # printing value of c print (f'speed of light in vacuum = {c}') # calculating sin (45 deg) angle_deg = 45.0 sin_45deg = mypkg.calc.mysin (angle_deg, unit='degree') # printing sin (45 deg) print (f'sin ({angle_deg} deg) = {sin_45deg}')
実行結果は以下の通りでござる。
% python3.13 test_mypkg_00.py speed of light in vacuum = 299792458.0 sin (45.0 deg) = 0.7071067811865475
パッケージを作成するのに必要なファイルを自動生成してくれるコマンドがあ るようでござる。以下のようにすればよいようでござる。
% hatch new mynewpackage mynewpackage ├── src │ └── mynewpackage │ ├── __about__.py │ └── __init__.py ├── tests │ └── __init__.py ├── LICENSE.txt ├── README.md └── pyproject.toml
以下のファイルが作成されるでござる。
% ls -lF mynewpackage total 40 -rw-r--r-- 1 daisuke taiwan 1094 Jun 11 16:51 LICENSE.txt -rw-r--r-- 1 daisuke taiwan 505 Jun 11 16:51 README.md -rw-r--r-- 1 daisuke taiwan 1558 Jun 11 16:51 pyproject.toml drwxr-xr-x 3 daisuke taiwan 4096 Jun 11 16:51 src/ drwxr-xr-x 2 daisuke taiwan 4096 Jun 11 16:51 tests/ % ls -lF mynewpackage/src/mynewpackage total 16 -rw-r--r-- 1 daisuke taiwan 124 Jun 11 16:51 __about__.py -rw-r--r-- 1 daisuke taiwan 102 Jun 11 16:51 __init__.py % ls -lF mynewpackage/tests total 8 -rw-r--r-- 1 daisuke taiwan 102 Jun 11 16:51 __init__.py