How can I make a Python script standalone executable to run without ANY dependency?

后端 未结 19 2755
眼角桃花
眼角桃花 2020-11-21 04:51

I\'m building a Python application and don\'t want to force my clients to install Python and modules.

So, is there a way to compile a Python script to be a standalone

相关标签:
19条回答
  • 2020-11-21 05:43

    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 in the 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.

    0 讨论(0)
  • 2020-11-21 05:43

    I'm told that PyRun is also an option. It currently supports Linux, FreeBSD and Mac OS X.

    0 讨论(0)
  • 2020-11-21 05:44

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

    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 on the Windows platform.

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

    0 讨论(0)
  • 2020-11-21 05:44

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

    0 讨论(0)
  • 2020-11-21 05:45

    Using PyInstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyway, there are probably some data files around and if you're running a site-based app then your program depends on HTML, JavaScript, and CSS files too. There isn't any point in moving all these files somewhere... Instead what if we move the working path up?

    Make a shortcut to the EXE file, 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\"
    

    We can rename the 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 the Windows 7 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 on 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. It will save on size.

    0 讨论(0)
  • 2020-11-21 05:46
    pyinstaller yourfile.py -F --onefile
    

    This creates a standalone EXE file on Windows.

    Important note 1: The EXE file will be generated in a folder named 'dist'.

    Important note 2: Do not forget --onefile flag

    You can install PyInstaller using pip install PyInstaller

    NOTE: In rare cases there are hidden dependencies...so if you run the EXE file and get missing library error (win32timezone in the example below) then use something like this:

    pyinstaller --hiddenimport win32timezone -F "Backup Program.py"
    
    0 讨论(0)
提交回复
热议问题