Are you one of those developers that create scripts to make their own life easier? coincidentally, do you like Python? and you have Windows?. Instead of work repetitively with the console executing your scripts manually in the console, you need to know that there's an easy way to execute them and even create little console applications with them in Windows. We are talking about creating .exe
(yeah, application files) files with python scripts, thanks to pyinstaller. PyInstaller is a program that freezes (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX.
In this article, you'll learn how to create an executable from a Python console script easily using Pyinstaller in windows.
Requirements
To create our executable, we are going to use the pyinstaller package. To download pyinstaller, execute the following command in your command prompt (cmd.exe):
pip install pyinstaller
The installation will proceed and you'll have available pyinstaller in your environment:
You can read more about this package in the official website. To check if pyinstaller was correctly installed, you can check if it's available in the console as an environment variable executing pyinstaller --h
.
Implementation
The creation of an executable using pyinstaller is very straightforward, customizable and very easy to do. In our example, we are going to create an executable of the following script. The filename of the script is file-creator.py
and contains the following code (it just prompt the user for the name of a new file and the content):
def start_setup():
# raw_input returns the empty string for "enter"
yes = set(['yes','y', 'ye', ''])
no = set(['no','n'])
prompt = '> '
print ("Hi, welcome to Text File Creator 2000")
print ("To get started, give a name to your file (without extension)")
filename = input(prompt)
print ("Awesome, now provide the content of the file !)")
content = input(prompt)
confirm = input("> Do you want to continue with the creation of " + filename + ".txt ? (Yes/No)").lower()
if confirm in yes:
text_file = open(filename + ".txt", "w")
text_file.write(content)
text_file.close()
print ("File succesfully created, come back soon !")
return True
elif confirm in no:
print ("Ok, nothing will happen !")
return False
else:
print ("Please answer yes or no !, the setup will start again")
# start again
start_setup()
start_setup()
As you can see, it is a simple console Python application. Now to create the executable, navigate with the console (cmd.exe) to the folder where the script of python is located (in this case Desktop\pythonScripts):
cd C:\Users\sdkca\Desktop\pythonScripts
Now, proceed with the creation of the executable using the following command:
pyinstaller file-creator.py
That's the most simple command to create an executable of your script, so that should be enough to create the executable in the folder where the script is located. Note that our application is based only in the console (if you want an executable with the manifest in the same folder and other dependencies, you can add the --console parameter to the command , if you use GUI with libraries like wxPython, then instead of the --console
parameter use --windowed
.).
Finally, you can test the created executable in the dist folder that will be created where the script is located:
Tips and suggestions
- You can change the icon of your executable adding the icon parameter (with the path of the file as value) to the command (pyinstaller
script.py --icon=c:\path-to\icon.ico
). - There are different areas of typical problems which can occur when using PyInstaller. For fixing problems in your application, please refer to the "if things go wrong readme" in the repository here.
Have fun !