A blog about technical art, scripting, van renovation, and some other gubbins.

@henloco

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.

  1. Open a command prompt by clicking Start, typing cmd hitting Enter.
  2. 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/
  3. To install the package manager Pip, download get-pip.py and save it to C:\ (or wherever’s convenient)
  4. In Command Prompt, go to C:\ or wherever you downloaded get-pip.py to and type
    python get-pip.py
  5. 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
  6. Next install PyInstaller:
    python -m pip install pyinstaller
  7. 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
  8. 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.
  9. 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
  10. Open Command Prompt again and type
    pyinstaller -v
    All being well, you should see a readout of the PyInstaller current version
  11. 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.

More

Previous

Better Mix

Next

Week 10 Logbook

Leave a Reply

Your email address will not be published. Required fields are marked *