Run Python program without installing required modules

守給你的承諾、 提交于 2020-12-06 07:06:26

问题


Is there a way to move a my Python program to other computer without the need of re installing all the required module?

Maybe by compilation?


回答1:


I think you're looking for PyInstaller. By definition, PyInstaller is a program that converts (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, Solaris and AIX.

PyInstaller as of this writing, doesn't support Python 3. There is however this page on freezing your code or shipping:

Solution    | Windows | Linux | OS X | Python 3 | License | One-file mode | Zipfile import | Eggs | pkg_resources support
bbFreeze    | yes     | yes   |  yes | no       | MIT     | no            | yes            | yes  | yes
py2exe      | yes     | no    | no   | no       | MIT     | yes           | yes            | no   | no
pyInstaller | yes     | yes   | yes  | no       | GPL     | yes           | no             | yes  | no
cx_Freeze   | yes     | yes   | yes  | yes      | PSF     | no            | yes            | yes  | no

See cx_Freeze's documentation here.




回答2:


If you just wanna you python program to run on another computer, PyInstaller or py2exe would be two good recommendations.

PyInstaller supports to several different operating systems: Windows, Linux, Mac OS and so on, while py2exe only supports Windows.

For details, visit:

PyInstaller: http://www.pyinstaller.org/

py2exe: http://www.py2exe.org/




回答3:


You could execute it with the help of pyinstaller

Install pyinstaller first by -

pip install pyinstaller

Usage -

pyinstaller example.py

This will generate binary in in a subdirectory called build/ & the dependencies will be under dist/ in source folder of your script.

ls -lha build/example/example
ls -lha dist/example/

For manual - https://pyinstaller.readthedocs.io/en/stable/usage.html#running-pyinstaller-with-python-optimizations




回答4:


You can combine the application and all the pure Python modules that it uses into one zipped file.

Examples of this approach are youtube-dl and my own lamprop program.

Basically, you make a directory that contains directories for all your modules, and your program file, which should be named __main__.py.

> ls
__main__.py  lamprop/
> ls lamprop/
__init__.py html.py latex.py parser.py text.py types.py

This is then wrapped up in a zip-file, and given a shebang-line for use on UNIX-like systems;

cd src; zip -q ../foo.zip __main__.py lamprop/*.py
echo '#!/usr/bin/env python' >lamprop
cat foo.zip >>lamprop
chmod a+x lamprop
rm -f foo.zip

(The commands above are written for use on a UNIX-like system. And on Windows systems the combined file should be given the py extension so that it is handed to the Python interpreter.)

The Python interpreter knows how to handle zipped source code archives. It unpacks them and runs __main__.py.

You could copy installed third-party modules into your source tree and include them in the same way. But there are a couple of things to keep in mind;

  • Licensing: If you want to include third-party modules, their licenses must allow you to do that.
  • Binary modules: some Python libraries like e.g. numpy contain shared libraries that by their nature are platform specific. A numpy module compiled on OS X or Linux will not work on Windows and vice verse.


来源:https://stackoverflow.com/questions/25469869/run-python-program-without-installing-required-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!