How to make python file to exe
How to make python file to exe
Create Executable of Python Script using PyInstaller
In this guide, you’ll see how create an executable of a Python script using PyInstaller?
Here are the full steps to accomplish this goal in Windows.
Steps to Create an Executable using PyInstaller
Step 1: Add Python to Windows Path
An easy way to add Python to the path is by downloading a recent version of Python, and then checking the box to ‘Add Python to PATH’ at the beginning of the installation:
Add Python to PATH
Finish the installation, and you should be good to go.
Step 2: Install the PyInstaller Package
Next, open the Windows Command Prompt, and then type the following command to install the PyInstaller package:
Step 3: Save your Python Script
Now save your Python script at your desired location.
For illustration purposes, let’s create a simple Python script that displays ‘Hello World!’ when clicking a button:
For demonstration purposes, let’s say that the Python script is stored in the following folder:
Where Python script is called ‘hello‘ and the file extension is ‘.py‘
Step 4: Create the Executable using PyInstaller
Now you’ll be able to create the executable of the Python script using PyInstaller.
Simply go to the Command Prompt, and then type:
cd followed by the location where your Python script is stored
Here is the command for our example:
Press Enter (after you typed the location where the Python script is stored on your computer).
Then, refer to the following template to create the executable:
Press Enter for the last time.
Step 5: Run the Executable
Your executable will be created at the location that you specified.
For our example, it will be under the same folder where the ‘hello’ script was originally stored:
You’ll notice that few additional files were created at that location.
To find the executable file, open the dist folder. You’ll then see the executable file:
Double click on the file, and you should be able to launch your program (if you get an error message, you may need to install Visual C++ Redistributable).
In our case, once you click on the ‘hello’ executable, you’ll get a display with a single button.
And if you click on that button, you’ll see the following expression:
You can read more about PyInstaller by visiting the PyInstaller manual.
Creating Executable Files from Python Scripts with py2exe
Introduction
If you want to create a simple application and distribute it to lots of users, writing it as a short Python script is not difficult, but assumes that the users know how to run the script and have Python already installed on their machine.
The most popular way to achieve this is by using the py2exe module. In this article, we’ll quickly go through the basics of py2exe and troubleshoot some common issues. To follow along, no advanced Python knowledge is needed, however you will have to use Windows.
Converting an interpreted language code into an executable file is a practice commonly called freezing.
Installing py2exe
To use the py2exe module, we’ll need to install it. Let’s do so with pip :
Converting Python Script to .exe
First, let’s write up a a program that’s going to print some text to the console:
Let’s run the following commands in the Windows command line to make a directory ( exampDir ), move the code we already wrote to said directory, and finally, execute it:
This should output:
Always test out the scripts before turning them into executables to make sure that if there is an error, it isn’t caused by the source code.
Setup and Configuration
Make another file called setup.py in the same folder. Here we will keep configuration details on how we want to compile our program. We’ll just put a couple of lines of code into it for now:
If we were dealing with an app with a graphical UI, we would replace console with windows like so:
Now open Command Prompt as administrator and navigate to the directory we just mentioned and run the setup.py file:
dist folder
And you’ll be greeted by our Latin quote, followed by the value of 4!:
Or, you can double click it and it’ll run in the console.
And re-run the commands to generate the .exe file.
Now, your end-users can run your scripts without any knowledge or prerequisites installed on their local machines.
Troubleshooting
How to Fix Missing DLL-s After Using py2exe
DLL stands for «dynamic-link library», and they’re not there just to make bugs, promise. DLLs contain code, data, and resources which our program might need during execution.
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
Or the command line says:
How to Generate 32/64-bit Executables Using py2exe?
To make a 64-bit executable, install 64 bit Python on your device. The same goes for the 32-bit version.
How to use py2exe on Linux or Mac
py2exe doesn’t support on Linux or Mac, as it’s aimed to create .exe files which is a Windows-unique format. You can download a Windows virtual machine on both Mac and Linux, use Wine or use a different tool like Pyinstaller on Linux, or py2app on Mac.
Conclusion
To make Python projects easier to run on Windows devices, we need to generate an executable file. We can use many different tools, like Pyinstaller, auto-py-to-exe, cx_Freeze, and py2exe.
Binary files may use DLL-s, so make sure to include them with your project.
I’ve been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don’t understand much of the documentation, nor am I confident enough to experiment too much.
Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I’d appreciate knowing that too.
Running Vista 32bit, python 2.7
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
There are two distinct ways of freezing python scripts to create executables:
The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that’s the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).
You can start with Py2exe tutorial and ‘hello-world’ to get acquainted with that how compilation is done. As far as I know it’s a simplest way to get your goal.
And the last thing, you can’t create cross-platform native executables as their file formats are strongly operating system and hardware dependent.
Create a single executable from a Python project [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
The community reviewed whether to reopen this question 12 days ago and left it closed:
Original close reason(s) were not resolved
I want to create a single executable from my Python project. A user should be able to download and run it without needing Python installed. If I were just distributing a package, I could use pip, wheel, and PyPI to build and distribute it, but this requires that the user has Python and knows how to install packages. What can I use to build a self-contained executable from a Python project?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
There are several different ways of doing this.
PyInstaller:
cx_Freeze:
py2exe:
py2app:
The main thing to keep in mind is that these types of programs will generally only produce an exe for the operating system you run it in. So for example, running Pyinstaller in Windows will produce a Windows exe, but running Pyinstaller in Linux will produce a Linux exe. If you want to produce an exe for multiple operating systems, you will have to look into using virtual machines or something like Wine.
Of course, that’s not the only way of doing things:
pynsist:
Pynsist will create a Windows installer for your program which will directly install Python on the user’s computer instead of bundling it with your code and create shortcuts that link to your Python script.
The pynsist tool itself requires Python 3.5+ to run, but supports bundling any version of Python with your program.
Pynsist will create Windows installers only, but can be run from Windows, Mac, and Linux. See their FAQ for more details.
Nuitka:
Nuitka will literally compile your Python code and produce an exe (as opposed to the other projects, which simply include Python) to try and speed up your code. As a side effect, you’ll also get a handy exe you can distribute. Note that you need to have a C++ compiler available on your system.
cython:
Cython is similar to Nuitka in that it is a Python compiler. However, instead of directly compiling your code, it’ll compile it to C. You can then take that C code and turn your code into an exe. You’ll need to have a C compiler available on your system.
My personal preference is to use PyInstaller since it was the easiest for me to get up and running, was designed to work nicely with various popular libraries such as numpy or pygame, and has great compatibility with various OSes and Python versions.
However, I’ve also successfully built various exes using cx_Freeze without too much difficulty, so you should also consider trying that program out.
I haven’t yet had a chance to to try pynist, Nuitka, or Cython extensively, but they seem like pretty interesting and innovative solutions. If you run into trouble using the first group of programs, it might be worthwhile to try one of these three. Since they work fundamentally differently then the Pyinstaller/cx_freeze-style programs, they might succeed in those odd edge cases where the first group fails.
In particular, I think pynist is a good way of sidestepping the entire issue of distributing your code altogether: Macs and Linux already have native support for Python, and just installing Python on Windows might genuinely be the cleanest solution. (The downside is now that you need to worry about targeting multiple versions of Python + installing libraries).
Nuitka and Cython (in my limited experience) seem to work fairly well. Again, I haven’t tested them extensively myself, and so my main observation is that they seem to take much longer to produce an exe then the «freeze» style programs do.
All this being said, converting your Python program into an executable isn’t necessarily the only way of distributing your code. To learn more about what other options are available, see the following links:
brentvollebregt/auto-py-to-exe
Use Git or checkout with SVN using the web URL.
Work fast with our official CLI. Learn more.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
As of PyInstaller 4.0, Python 2.7 is no longer supported. Read «Python 2.7 Support» below for steps on how to use this tool with Python 2.7.
Installation and Usage
You can install this project using PyPI:
Then to run it, execute the following in the terminal:
Then to run it, execute the following in the terminal:
Running Locally Via Github (no install)
You can run this project locally by following these steps:
Using the Application
Usage: auto-py-to-exe [-nc] [-c [CONFIG]] [-o [PATH]] [filename]
Instead of inserting the same data into the UI over and over again, you can export the current state by going to the «Configuration» section within the settings tab and exporting the config to a JSON file. This can then be imported into the UI again to re-populate all fields.
This JSON config export action does not save the output directory automatically as moving hosts could mean different directory structures. If you want to have the output directory in the JSON config, add the directory under nonPyinstallerOptions.outputDirectory in the JSON file (will need to create a new key).
If you need something visual to help you get started, I made a video for the original release of this project; some things may be different but the same concepts still apply.
Issues Using the Tool
If you’re having issues with the packaged executable or using this tool in general, I recommend you read my blog post on common issues when using auto-py-to-exe. This post covers things you should know about packaging Python scripts and fixes for things that commonly go wrong.
Language | Code | Translator | Translated |
---|---|---|---|
Brazilian Portuguese (Português Brasileiro) | pt_br | marleyas | UI (partial) |
Chinese Simplified (简体中文) | zh | jiangzhe11 | UI and README |
Chinese Traditional (繁體中文) | zh_tw | startgo | UI |
Czech | cz | Matto58 | UI |
English | en | — | UI and README |
French (Français) | fr | flaviedesp | UI |
German (Deutsch) | de | hebens | UI (partial) |
Indonesian (Bahasa Indonesia) | id | MarvinZhong | UI |
Italian (Italiano) | it | itsEmax64 | UI |
Russian (Русский) | ru | Oleg | UI |
Spanish (Español) | sp | enriiquee | UI |
Thai (ภาษาไทย) | th | teerut26 | UI (partial) |
Turkish (Türkçe) | tr | mcagriaksoy | UI |
Want to add a translation for another language? Update i18n.js and submit a PR or attach it in an issue.
Python 2.7 Support
Tests are located in tests/ and are run using pytest:
Источники информации:
- http://stackabuse.com/creating-executable-files-from-python-scripts-with-py2exe/
- http://stackoverflow.com/questions/11915462/how-to-convert-python-py-file-into-an-executable-file-for-use-cross-platform
- http://stackoverflow.com/questions/12059509/create-a-single-executable-from-a-python-project
- http://github.com/brentvollebregt/auto-py-to-exe