Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何压缩 site-packages #25

Open
rainzee opened this issue Oct 25, 2022 · 5 comments
Open

如何压缩 site-packages #25

rainzee opened this issue Oct 25, 2022 · 5 comments

Comments

@rainzee
Copy link

rainzee commented Oct 25, 2022

site-packages会得到一个很高的压缩率,通常能大幅度减少体积,甚至官方Windows embeddable package包中,标准库就是一个Zip压缩文件,通过.pth引入,请问Pystand如何压缩site-packages,或者有无关于官方库zipapp的最佳实践,期待您的回复。

@myd7349
Copy link
Contributor

myd7349 commented Oct 30, 2022

对这一块儿了解得不多,就找了一些参考资料,加一些自己的理解,抛砖引玉:

  • https://docs.python.org/3/library/zipimport.html

    Any files may be present in the ZIP archive, but importers are only invoked for .py and .pyc files. ZIP import of dynamic modules (.pyd, .so) is disallowed. Note that if an archive only contains .py files, Python will not attempt to modify the archive by adding the corresponding .pyc file, meaning that if a ZIP archive doesn’t contain .pyc files, importing may be rather slow.

    这种方式只能导入 .py 或 .pyc 文件,不能导入 .pyd 这样的动态库。所以,如果你引用的包包含了动态库(比如:PyQt 这种),可能这种方式不太可行。

    或者,可以尝试将动态库单独拎出来。比如:Python Windows embeddable package 里,sqlite3 包的 Python 部分是以 .pyc 格式存储于 python38.zip 中的,但 sqlite3.dll、_sqlite3.pyd 是放在压缩包外的。如果只是一两个动态库可能还好说,但像 PyQt 这种有一堆动态库的,估计比较麻烦。

  • https://realpython.com/python-zip-import/

    这篇文章讲了如何从 zip 包中导入模块。

@skywind3000
Copy link
Owner

skywind3000 commented Oct 30, 2022

不要一口气把整个 site-packages 压缩了,里面的 dll/pyd/so 不支持压缩。
你可以手工按项目压缩一些纯 py 的项目,然后写 ._pth 文件,声明一下用的是那个 zip 文件。

@skywind3000
Copy link
Owner

skywind3000 commented Oct 30, 2022

dll/pyd 可以试试 upx 压缩,但不是所有都可以 upx 压缩。

@rainzee
Copy link
Author

rainzee commented Oct 30, 2022

对这一块儿了解得不多,就找了一些参考资料,加一些自己的理解,抛砖引玉:

  • https://docs.python.org/3/library/zipimport.html

    Any files may be present in the ZIP archive, but importers are only invoked for .py and .pyc files. ZIP import of dynamic modules (.pyd, .so) is disallowed. Note that if an archive only contains .py files, Python will not attempt to modify the archive by adding the corresponding .pyc file, meaning that if a ZIP archive doesn’t contain .pyc files, importing may be rather slow.

    这种方式只能导入 .py 或 .pyc 文件,不能导入 .pyd 这样的动态库。所以,如果你引用的包包含了动态库(比如:PyQt 这种),可能这种方式不太可行。
    或者,可以尝试将动态库单独拎出来。比如:Python Windows embeddable package 里,sqlite3 包的 Python 部分是以 .pyc 格式存储于 python38.zip 中的,但 sqlite3.dll、_sqlite3.pyd 是放在压缩包外的。如果只是一两个动态库可能还好说,但像 PyQt 这种有一堆动态库的,估计比较麻烦。

  • https://realpython.com/python-zip-import/
    这篇文章讲了如何从 zip 包中导入模块。

我认为仔细过一边官方的压缩实现可能很有启发,我去看看,感谢您的回复。

@rainzee
Copy link
Author

rainzee commented Oct 30, 2022

不要一口气把整个 site-packages 压缩了,里面的 dll/pyd/so 不支持压缩。 你可以手工按项目压缩一些纯 py 的项目,然后写 ._pth 文件,声明一下用的是那个 zip 文件。

那你认为Pystand能够和zipapp做什么有机结合呢,看起来非常搭配,能够完整打包压缩项目和依赖

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants