如何使我的 python 脚本易于移植?或者如何编译成具有所有模块依赖项的二进制文件?
有没有办法将python脚本编译成二进制文件? 我有一个文件 python 脚本,它使用了很多模块。 我想要的是在其他机器(freebsd)上有它的副本,但不需要在每台主机上安装所有需要的模块。
在这种情况下有哪些可能的解决方案?
Is there any way to compile python script into binary?
I have one file python script which uses a lot of modules.
What I would like is to have its copy on other machines (freebsd) but without installing all needed modules on every host.
What are possible solutions in such cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以满足您要求的程序有:
但正如前面提到的,您也可以使用 Distribute 和 创建一个包将其他包作为依赖项。然后,您可以使用
pip
安装该软件包,它将安装所有软件包。不过,您仍然需要安装 Python 和 pip。Programs that can do what you ask for are:
But as mentioned you can also create a Package with Distribute and have the other packages as dependencies. You can then uses
pip
to install that package, and it will install all of the packages. You still need to install Python and pip, though.cx_freeze 会将您的 Python 脚本附加到独立的 Python 加载器,并生成一个包含程序和共享库的目录依赖关系。然后,您可以将生成的发行版复制到独立于 Python 或您的模块的其他计算机上。
更好的答案可能是创建一个 PIP 包,将这些第三个模块标识为依赖项,因此安装可以像“pip install mypackage; ./package”一样简单
cx_freeze will append your python scripts to a standalone Python loader and produce a directory containing the program, and shared library dependencies. You can then copy the resulting distribution to other machines independent of Python or your modules.
A better answer may be to create a PIP package that identifies these third modules as dependencies, so installation can be as simple as "pip install mypackage; ./package"
Python 还会在当前目录中查找 import 模块,因此您不必将它们安装到 python 目录中。您的分发文件结构可能如下所示:
其中 main.py 有
import module1, module2
Python will also look for
import
modules in the current directory, so you don't have to install them into the python directory. Your distribution file structure might look like:Where main.py has
import module1, module2
您可能想从脚本创建一个 Python 包。最后,您将能够在任何主机上执行
pip install mypackage
,并且所有必需的模块都将自动下载并安装。请参阅此问题,了解如何创建此类包。
You probably want to create a Python package from your script. In the end you will be able to do
pip install mypackage
on any host and all the required modules will be downloaded and installed automatically.See this question on how to create such a package.