Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Gộp nhiều module Python thành một

Giả sử bạn dùng Python 2.6 trở lên, bạn có thể package script vào file zip, thêm __main__.py và chạy trực tiếp file zip. Ví dụ, zip tất cả file vào my_app.zip và đặt main script ở __main__.py, chạy zip bằng python:

$ python my_app.zip

Nếu muốn dùng script thứ ba, xem module stickytape. Nó chuyển script Python và module dependency thành single-file Python script. Cài stickytape:

$ pip install stickytape

Giả sử script và dependency ở scripts/sample, dùng stickytape:

$ stickytape scripts/sample --add-python-path . > ./sample-standalone

Cũng dùng tham số --output-file:

$ stickytape scripts/sample --add-python-path . --output-file ./sample-standalone