Converting a Python Program to an Executable (.exe) with CX_Freeze

In order to actually run a Python script, we must have a copy of python downloaded on our machine with the appropriate interpreter of course. Let’s say we created a program that uses an external library we have to download off the internet also (using pip or another package manager). What happens if we gave this program to someone else to use who didn’t have a version of Python on their machine or have the necessary packages to run the scripts? In short, they wouldn’t be able to run the program.

*Note: I really like to use cx_freeze specifically with JetBrains PyCharm ID. The reason being is I’ll usually have a bunch of separate files within my project. I found if I use another library like pyinstaller things get to be a bit messy. This is the cleanest way I’ve found so far.

Step 1:

You’ll see below I created  GUI application within PyCharm. In your project window create a new Python file called setup.py. Add the following code to the file….

Next, navigate to the command prompt (or terminal) and change your current directory to your project. After that, enter the following command to run cs_freeze: python setup.py

*If you get the following error “Python isn’t recognized….” this means you’ll need to add an environment variable to get Python registered as a valid command. If this does occur, I appended directions to the bottom of this post. Check them out…

After you do this, you’ll see a “build” folder be created in your project directory.

Once the build is completed, navigate within the build folder with the executable file you named within the setup.py file.

Here’s the result:

And there you have it! No Python and no external libraries needed!

Troubleshooting “Python is not recognized” in the command prompt:

Navigate to your start menu and type in environment variables. You’ll get to the following window.

Next click Path (Highlighted in blue here)…

Add the scripts directory within the location of the version of Python you’re using (3.6 in my case).

After you’ve done this restart the command prompt. You should see this when you type and enter “python”…

 

About the author

programmingforfinance

Hi, I'm Frank. I have a passion for coding and extend it primarily within the realm of Finance.

View all posts

Leave a Reply

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