2

把Python项目打包成一个文件

 2 years ago
source link: https://allenwind.github.io/blog/11208/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Mr.Feng Blog

NLP、深度学习、机器学习、Python、Go

把Python项目打包成一个文件

把Python项目源码打包成一个文件

├── program
│   ├── core.py
│   └── __main__.py

core.py

from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer


def run_server(port=8080):
print("listen at", port)
with TCPServer(("", port), SimpleHTTPRequestHandler) as httpd:
httpd.serve_forever()

__main__.py

from core import run_server

if __name__ == "__main__":
run_server()

__main__.py 的意义:把当前package当做script运行,__main__.py则是这个script的入口。

打包成一个文件

#!/usr/bin/env bash
cd program;zip -r ../app.zip .;cd ..
echo '#!/usr/bin/env python3' | cat - app.zip > app
chmod a+x app

项目地址python-one-file

转载请包括本文地址:https://allenwind.github.io/blog/11208/
更多文章请参考:https://allenwind.github.io/blog/archives/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK