How to make a Python script standalone executable to run without ANY dependency? [closed]

若如初见. 提交于 2019-11-25 21:44:41

问题


I\'m building a Python application and don\'t want to force my clients to install Python and modules. I also want to make my application closed-source.

So, is there a way to compile Python scripts to standalone executables?


回答1:


You can use py2exe as already answered and use cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so in linux, much harder to revert than common .pyo and .pyc files (and also gain in performance!)




回答2:


You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.

PyInstaller Quickstart

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist.

For a more detailed walkthrough, see the manual.




回答3:


You might wish to investigate Nuitka. It takes python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.

You will probably also get a performance improvement if you use it. Recommended.




回答4:


I would like to compile some useful information about creating standalone files on windows using Python 2.7.

I have used py2exe and it works, but I had some problems.

  • It has shown some problems for creating single files in Windows 64 bits: Using bundle_files = 1 with py2exe is not working;

  • It is necessary to create a setup.py file for it to work. http://www.py2exe.org/index.cgi/Tutorial#Step2;

  • I have had problems with dependencies that you have to solve by importing packages in the setup file;

  • I was not able to make it work together with PyQt..

This last reason made me try PyInstaller http://www.pyinstaller.org/ .

In my opinion, it is much better because:

  • It is easier to use.

I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in Windows Path):

pyinstaller.exe --onefile MyCode.py
  • You can create a single file, among other options (https://pyinstaller.readthedocs.io/en/stable/usage.html#options).

  • I had only one problem using PyInstaller and multiprocessing package that was solved by using this recipe: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing .

So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.




回答5:


And a third option is cx_Freeze, which is cross-platform.




回答6:


Yes, it is possible to compile Python scripts into standalone executable.

PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX. It is one of the recommended converters.

py2exe converts Python scripts into only executable in Windows platform.

Cython is a static compiler for both the Python programming language and the extended Cython programming language.




回答7:


you may like py2exe. you'll also find in there infos for doing it on linux




回答8:


Use py2exe.... use below set up files:

 from distutils.core import setup
 import py2exe

 from distutils.filelist import findall
 import matplotlib

 setup(
       console=['PlotMemInfo.py'],

       options={
                'py2exe': {
                'packages' : ['matplotlib'],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll',
                 'libgdk_pixbuf-2.0-0.dll']
                          }
                },
       data_files = matplotlib.get_py2exe_datafiles()
     )



回答9:


I also recommend pyinstaller for better backward compatibility such as python 2.3 - 2.7.
for py2exe, you have to have python 2.6




回答10:


For Python 3.2 scripts the only choice is Cxfreeze. Build it from sources otherwise it won't work.

For python 2.x I suggest pyinstaller as it can package a python program in a single executable, unlike CxFreeze which outputs also libraries.




回答11:


py2exe will make the exe file you want but you need to have the same version of MSVCR90.dll on the machine you're going to use your new exe. See http://www.py2exe.org/index.cgi/Tutorial for more info.




回答12:


You can find the list of distribution utilities listed @ https://wiki.python.org/moin/DistributionUtilities.

I use bbfreeze and it has been working very well (yet to have python 3 support though).




回答13:


I like pyinstaller - especially the "windowed" variant:

pyinstaller --onefile --windowed myscript.py

It will create one single *.exe file in a dist/ folder.




回答14:


Not exactly a packaging of the python code, but there is now also grumpy from google, which transpiles the code to Go. It doesn't support the python C api, so it may not work for all projects.




回答15:


Use Cython to convert to c, compile and link with gcc. Another could be, make the core functions in c (the ones you want to make hard to reverse), compile them and use python boost to import the compiled code ( plus you get a much faster code execution). then use any tool mentioned to distribute.




回答16:


Using pyinstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyways there's probably some data files around and if you're running a site-based app then your program depends on html, js, css files too. No point in moving all these files somewhere.. instead what if we move the working path up.

Make a shortcut to the exe, move it at top and set the target and start-in paths as specified, to have relative paths going to dist\folder: Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\" Can rename shortcut to anything so renaming to "GTFS-Manager"
Now when I double-click the shortcut, it's as if I python-ran the file! I found this approach better than the --onefile one as:

  1. In onefile's case, there's a problem with a .dll missing for win7 OS which needs some prior installation etc. Yawn. With the usual build with multiple files, no such issues.
  2. All the files that my python script uses (it's deploying a tornado web server and needs a whole freakin' website worth of files to be there!) don't need to be moved anywhere: I simply create the shortcut at top.
  3. I can actually use this exact same folder in ubuntu (run python3 myfile.py) and windows (double-click the shortcut).
  4. I don't need to bother with the overly complicated hacking of .spec file to include data files etc.

Oh, remember to delete off the build folder after building, will save on size.




回答17:


I'm told that PyRun, https://www.egenix.com/products/python/PyRun/, is also an option.



来源:https://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency

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