DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Traceroute Command in Python and How to Read a Traceroute
  • Using Python Libraries in Java
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning

Trending

  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Top Book Picks for Site Reliability Engineers
  • DGS GraphQL and Spring Boot
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  1. DZone
  2. Coding
  3. Languages
  4. Making a Stand Alone Executable from a Python Script Using PyInstaller

Making a Stand Alone Executable from a Python Script Using PyInstaller

There are a number of tools that can help you execute your Python script, and PyInstaller does an excellent job. Check it out here.

By 
Naurin Idris user avatar
Naurin Idris
·
Oct. 31, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
117.9K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

There are plenty of tools available for converting Python script into an executable. For example, check out:

  • PyInstaller
  • py2exe

For Python 2, I used to prefer py2exe. It's a neat tool that does the trick by making a stand alone executable from a Python script. The one problem I faced was, py2exe used to support only Python 2! Then I moved on to PyInstaller. The good thing is, py2exe now provides support to Python 3 as well! I might share my py2exe experience as well but, that will be in another article.

This article is all about PyInstaller! It's a handy tool that lets you create a .exe file for Windows with only one command! Although I prefer Linux, I had to create executables for Windows all the time targeting various Windows distributions (yes, it was the clients’ requirement).

Lets explore the process of creating executable using PyInstaller.

Installation

Hassle free installation, all you have to do is to run the following command (if you have already installed Python) and that’s it!

pip install pyinstaller

Note: If you are working on Windows. You might need to install PyWin32 as well! Download it from here. And, if you need additional help, you can always head right to the official documentation of PyInstaller.

If you are using a different package management tool such as conda (for Anaconda) then you will have to use the following commands instead:

conda install -c conda-forge pyinstallerconda install -c anaconda pywin32

Please, find suitable installation documentation for your package management tool for more details.

Create an Executable

Now that you have installed PyInstaller, all you have to do is find the Python script that you want to convert to an executable. Just navigate to your Python script directory. Now, open up your Terminal/Command Prompt in the script directory and, test your Python script:

python your_script.py

Make sure the script works as expected. Now, the command I prefer for compiling the script into executable is the following one.

Run the command:

pyinstaller --onefile <your_script_name>.py

This will create a standalone executable in the dist directory of your script folder. Don’t worry, if the folder doesn’t exist it will create one automatically.

Notice that we passed an argument “ — onefile ”. This argument tells PyInstaller to create only one file. If you don’t specify this, the libraries will be distributed as a separate file along with the executable.

Note: The format/extension of the executable will depend on which operating system you used for compilation. For example, if you run the PyInstaller command on Windows the executable file will be .exe. If you run it on Linux the extension will depend on the distribution you are using.

You can only create executable for your Operating system, i.e. the Operating system you used to compile the executable.

For example, it is not possible to create a Windows executable (.exe) by directly running a Pyinstaller command on a Linux Distribution and vice versa. (If you know a way to do it, feel free to comment).

What you can do is run Virtualbox or similar application to run the OS virtually and, create executable in that virtual os and export it later. Also, Wine works as well! (Though, I didn’t try it myself)

If your Python script depends on additional executables, for example, phantomjs or chromedriver, you may have to put these executable in the same directory of your executable!

For these type of dependencies, when I’m working on Windows Platform. I usually package them using NSIS.

The procedure is like below (Assuming NSIS already installed):

  • Create a ZIP of all the necessary files i.e script’s executable, dependencies, readme etc.
  • run NSIS
  • Click on the make installer from zip archive option.
  • Select the zip file
  • Provide a name and create the Installer.exe/Setup.exe file.

This executable is basically an extractor that extracts all the necessary file in a directory. Cool!

Valuable Resources:

  • Read the answers of this Stack Overflow question
  • PyInstaller Source Code
  • PyInstaller Supported Python Packages
  • py2exe official tutorial
  • Read reddit discussion on this article

Thanks for reading this far! If you liked this article please, feel free to clap and, spread the love!

Executable Python (language) operating system

Opinions expressed by DZone contributors are their own.

Related

  • Traceroute Command in Python and How to Read a Traceroute
  • Using Python Libraries in Java
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!