I need to convert a Python script to a Windows executable.
I have Python 2.6 installed to
I have created one script and kept it in
C:pythonscript. Inside this folder there are two files
oldlogs.py (this file need coversion)
setup.py code is
from distutils.core import setup import py2exe setup(console=['oldlogs.py'])
How can I convert oldlogs.py to an exe file?
Here is how one could fairly easily use PyInstaller to solve the issue at hand:
From the tool’s documentation:
PyInstaller analyzes myscript.py and:
- Writes myscript.spec in the same folder as the script.
- Creates a folder build in the same folder as the script if it does not exist.
- Writes some log files and working files in the build folder.
- Creates a folder dist in the same folder as the script if it does not exist.
- Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
I recommend PyInstaller, a simple python script can be converted to an exe with the following commands:
utils/Makespec.py [--onefile] oldlogs.py
which creates a yourprogram.spec file which is a configuration for building the final exe. Next command builds the exe from the configuration file:
More can be found here
Since other SO answers link to this question it’s worth noting that there is another option now in PyOxidizer.
It’s a rust utility which works in some of the same ways as pyinstaller, however has some additional features detailed here, to summarize the key ones:
- Single binary of all packages by default with the ability to do a zero-copy load of modules into memory, vs pyinstaller extracting them to a temporary directory when using
- Ability to produce a static linked binary
(One other advantage of
pyoxidizer is that it does not seem to suffer from the
GLIBC_X.XX not found problem that can crop up with pyinstaller if you’ve created your binary on a system that has a glibc version newer than the target system).
Overall pyinstaller is much simpler to use than PyOxidizer, which often requires some complexity in the configuration file, and it’s less Pythony since it’s written in Rust and uses a configuration file format not very familiar in the Python world, but PyOxidizer does some more advanced stuff, especially if you are looking to produce single binaries (which is not pyinstaller’s default).
# -*- mode: python -*- block_cipher = None a = Analysis(['SCRIPT.py'], pathex=[ 'folder path', 'C:\Windows\WinSxS\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_50c6cb8431e7428f', 'C:\Windows\WinSxS\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_c4f50889467f081d' ], binaries=[(''C:\Users\chromedriver.exe'')], datas=, hiddenimports=, hookspath=, runtime_hooks=, excludes=, win_no_prefer_redirects=False, win_private_assemblies=False, cipher=block_cipher) pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, name='NAME OF YOUR EXE', debug=False, strip=False, upx=True, runtime_tmpdir=None, console=True )