Making a Windows executable from a Python script
Published 9th November 2019 by Henry
Packaging up your Python script into a standalone executable is really useful for portability and distributing your tools for others to use. Here I’ll outline how to setup Python on Windows if it isn’t already, then install the package manager Pip so that we can finally install PyInstaller, a Python module which does the heavy lifting of packaging your script with all its dependencies into our .exe file.
Setting up Python
First you’ll need to check what version of Python you have installed on your system.
- Open a command prompt by clicking Start, typing cmd hitting Enter.
- Type
python -v
and hit Enter to show which version you have installed. If you have version >3 then great, you can proceed. Otherwise you will need to download a new version from https://www.python.org/downloads/ - To install the package manager Pip, download get-pip.py and save it to C:\ (or wherever’s convenient)
- In Command Prompt, go to C:\ or wherever you downloaded get-pip.py to and type
python get-pip.py - Once this has finished installing, we now need to downgrade Pip to version 18 due to a compatibility issue with PyInstaller:
python -m pip install pip=18 - Next install PyInstaller:
python -m pip install pyinstaller
- You may need to add the Scripts folder in your Python install to your Windows PATH environment variable. To do this, search your PC for the file pyinstaller.exe and copy the full path. It should be something like C:\Python\Scripts
- Click Start and type environment variables. Click the option Edit the system environment variables and add the scripts path to the end with a preceding semi-colon.
- Restart your computer for the environment variables to take effect. You should now be able to use any of the tools in the Python scripts folder from any location in Command Prompt
- Open Command Prompt again and type
pyinstaller -v
All being well, you should see a readout of the PyInstaller current version - Package up your script! Go to the location of your python script in Command Prompt and try the following:
pyinstaller --onefile yourscript.py
If everything works as expected, which it almost NEVER will, your script will be bundled into an exe called yourscript.exe ready for distribution. Woop!
If it didn’t work, drop me an email and let me know.
Leave a Reply