LoginSignup
5
3

More than 3 years have passed since last update.

python 実行可能な圧縮ファイルの作成 (zipapp)

Last updated at Posted at 2019-05-18

pythonで作ったバッチを1ファイルにして管理を楽にする場合、
python 3.5で入った機能のzipappが使えるようだがちょっと詰まったのでメモしておく。

前提となるファイル構成

ファイル配置

myapp
+ test1.py
+ test2.py

ファイル内容

test1.py

from test2 import hello

def main():
    print('main!!')
    hello()

if __name__ == "__main__":
    main()

test2.py

def hello():
    print('hello!!')

シンプルな方法

公式サイト
https://docs.python.org/ja/3/library/zipapp.html

myappの1階層上のディレクトリで以下のコマンドを実行。

myapp> cd ..
> python -m zipapp myapp -m "test1:main"
> python myapp.pyz
main!!
hello!!

myapp: 圧縮するディレクトリ
test1: モジュール名(pyファイル名)
main: 実行する関数名

便利な方法

実際には普段の実行はmyapp内で実行することが多いので、
1階層上に移動するのとか面倒。
よって、myapp内で実行するなら以下のコマンド。

PS D:\myapp> python -m zipapp ../myapp -m "test1:main" -o ./myapp.pyz
PS D:\myapp> python myapp.pyz
main!!
hello!!

myapp: 圧縮するディレクトリ
test1: モジュール名(pyファイル名)
main: 実行する関数名
hello.pyz: 圧縮ファイル

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3