如何使我的 python 脚本易于移植?或者如何编译成具有所有模块依赖项的二进制文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4556424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:18:00  来源:igfitidea点击:

How to make my python script easy portable? or how to compile into binary with all module dependencies?

pythonbinaryfreebsd

提问by andriyko

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.

有没有办法将python脚本编译成二进制文件?我有一个文件 python 脚本,它使用了很多模块。我想要的是在其他机器(freebsd)上有它的副本,但不需要在每台主机上安装所有需要的模块。

What are possible solutions in such cases?

在这种情况下有哪些可能的解决方案?

采纳答案by Lennart Regebro

Programs that can do what you ask for are:

可以满足您要求的程序有:

  • PyInstaller:http://www.pyinstaller.org/ [Windows、Linux、OS X]
  • cx_freeze: http://cx-freeze.sourceforge.net/[Windows, Linux]
  • py2exe:http://www.py2exe.org/ [Windows]
  • py2app:http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html [os x]

But as mentioned you can also create a Package with Distribute and have the other packages as dependencies. You can then uses pipto install that package, and it will install all of the packages. You still need to install Python and pip, though.

但如前所述,您也可以使用 Distribute 创建一个包,并将其他包作为依赖项。然后您可以使用pip来安装该软件包,它将安装所有软件包。不过,您仍然需要安装 Python 和 pip。

回答by Ian Wetherbee

Python will also look for importmodules in the current directory, so you don't have to install them into the python directory. Your distribution file structure might look like:

Python 还会import在当前目录中查找模块,因此您不必将它们安装到 python 目录中。您的分发文件结构可能如下所示:

main.py
module1/
    __init__.py, ...
module2/
    __init__.py, ...

Where main.py has import module1, module2

main.py 在哪里 import module1, module2

回答by Jochen Ritzel

You probably want to create a Python package from your script. In the end you will be able to do pip install mypackageon any host and all the required modules will be downloaded and installed automatically.

您可能想从您的脚本创建一个 Python 包。最后,您将能够pip install mypackage在任何主机上进行操作,并且所有必需的模块都将自动下载并安装。

See this question on how to create such a package.

请参阅有关如何创建此类包的问题

回答by swdunlop

cx_freezewill 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.

cx_freeze会将您的 Python 脚本附加到一个独立的 Python 加载器,并生成一个包含程序和共享库依赖项的目录。然后,您可以将生成的发行版复制到独立于 Python 或您的模块的其他机器上。

$ cat hello.py 
print "Hello, World!"
$ ls dist/
datetime.so  _heapq.so  hello  libpython2.6.so.1.0  readline.so
$ cat hello.py 
print "Hello, World!"
$ cxfreeze hello.py 
... <snip> ...
$ ls dist/
datetime.so  _heapq.so  hello  libpython2.6.so.1.0  readline.so
$ ./dist/hello
Hello, World!

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"

更好的答案可能是创建一个 PIP 包,将这些第三个模块标识为依赖项,因此安装可以像“pip install mypackage; ./package”一样简单