From version 3.6, Python provides a zipapp
module, which can zip a directory contains multiple Python scripts
into a pyz file, then run with python myapp.pyz
.
The pyz file uses standard zip format. zipapp is no more than a thin wrapper on zipfile module from Python 2.6. See section FAQ of PEP 441.
Demo
The following codes create a standalone pyz file add_cols.pyz from a folder contains 2 Python scripts. Run it to to create a new csv file by adding a column huiliuxiang of an existing csv file ycz6502.csv to another column zuchuan.
take pyzipappEx
head ycz6502.csv
time,station,xiaoshi,huiliuxiang,zuchuan,point,value
20170102 08:45:00,YCZ,65,2,1,59798,0.649999976
20170102 09:00:00,YCZ,65,2,1,59798,3.539999962
20170102 10:15:00,YCZ,65,2,1,59798,5.799999714
20170102 11:00:00,YCZ,65,2,1,59798,6.559999943
20170102 11:30:00,YCZ,65,2,1,59798,6.819999695
20170102 12:00:00,YCZ,65,2,1,59798,7.019999981
20170102 12:30:00,YCZ,65,2,1,59798,7.099999905
20170102 13:45:00,YCZ,65,2,1,59798,6.319999695
20170102 14:00:00,YCZ,65,2,1,59798,6.079999924
mkdir add_cols
cat << EOF > add_cols/mylib.py
import pandas as pd
def add2col(inpfile: str, outfile: str):
inp = pd.read_csv(inpfile)
inp['zuchuan'] = inp['huiliuxiang'] + inp['zuchuan']
inp.to_csv(outfile, index=False)
EOF
cat << EOF > add_cols/main.py
import sys
from mylib import add2col
def entry():
inp = sys.argv[1]
out = sys.argv[2]
print(f'read file {inp}, and convert to file {out}')
add2col(inp, out)
EOF
python -m zipapp add_cols -m 'main:entry'
python add_cols.pyz ycz6502.csv res.csv
unzip -l add_cols.pyz
unzip -c add_cols.pyz __main__.py