. Advertisement .
..3..
. Advertisement .
..4..
Computers are mostly used to store information and extract information from it. Python is one of the most powerful programming languages. We can use Python to get computers to do most of the routine processes. This blog is all about How To Create Executable of Python Script using PyInstaller.
Using PyInstaller
The pyinstaller command has the following syntax:
pyinstaller[options]script[script…] |specfile
Set the current directory to the path of your program myscript.py in the simplest scenario, then run:
pyinstaller myscript.py
In adition to the analyzing myscript.py function, PyInstaller also is used for:
-
Writing
myscript.spec
in the identical folder just as the script. -
Creating a folder
build
in the identical folder like the script if it doesn’t exist. -
Writing several log files and running files in the folder of
build
. -
Creating a folder
dist
in one folder like the script in the case it doesn’t exist. -
Writing the
myscript
executable folder in the folder ofdist
.
The bundled app that you give to your users is located in the dist folder. On the command line, one script is often named. If you add more names, they are all examined and added to the output. However, the name of the executable folder and file is provided by the first script name given. At run-time, its code is the first to be executed at the time it runs.
The guide to create executable of Python script using PyInstaller
Simply follow our step-by-step instructions to create executable of Python script using PyInstaller.
Step 1: Include Python In Your PATH Variable
First and foremost, the thing you need to do is adding your Python path to your environment variable, as described in this article. Let’s follow the below process to do that:
- To begin with, you have to press the start button.
- Next, at the bottom, let’s look for Environment Variable and then choose Environment Variables.
- Now, in System variables,you can look for PATH and edit it. Next, in the New box, paste the path to the Python installation.
- The path must include the python install directory, bin, and lib-scripts.
You can skip this stage if your Python PATH has already been included in your Environment Variable.
Step 2: Install Pyinstaller Package
To install Pyinstaller, you have to access a command prompt and type the following command.
pip install pyinstaller
Step 3: Keep a copy of your Python script
After installing the Pyinstallser package, you must write your Python script and save it. Here is an example of a basic Python script.
import tkinter as tk
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 500, height = 500)
canvas1.pack()
def hello ():
label1 = tk.Label(root, text= 'Hello World!', fg='red')
canvas1.create_window(250, 400, window=label1)
button1 = tk.Button(text='Click Here',command=hello, bg='brown',fg='white')
canvas1.create_window(250, 250, window=button1)
root.mainloop()
In your Python To Exe File Folder, you can save the above code as Python to exe.py.
Step 4: Make the Executable File utilizing Pyinstaller
You can now use this command to create an executable file. All you have to do is access the folder containing your Python script and then open the Terminal from that folder.
For example, the Python to exe.py file is located in the Python To Exe File Folder. So, you should open the Terminal from Python to the Exe File Folder.
To make an executable file, simply run the following Pyinstaller command.
pyinstaller --onefile pythonScriptName.py
Python to exe.py is the name of the file in this case. So, your command is as follows.
pyinstaller --onefile Python_to_exe.py
This is the result:
Microsoft Windows [Version 10.0.19044.1466]
(c) Microsoft Corporation. All rights reserved.
F:\Python To Exe File>pyinstaller --onefile Python_to_exe.py
210 INFO: PyInstaller: 4.8
210 INFO: Python: 3.7.8
.
.
.
.
14977 INFO: Appending PKG archive to EXE
18273 INFO: Building EXE from EXE-00.toc completed successfully..
Now, all of the files in your Python To Exe File Folder will look like this.
......... ADVERTISEMENT .........
..8..
Step 5: Run The Executable File
You should now be able to see the dist folder.
Simply access the dist folder and look for the Executable File, which has the same name as your Python file.
Simply run the file. When you double-click the Python_to_exe.exe executable file in my example, you’ll see a screen with a single Click Here button.
And if you push that button, the Hello World expression will appear!
Conclusion
How To Create Executable of Python Script using PyInstaller is a confusing problem. We hope this blog has helped clear the air around how to do it. If you have more questions about this topic, please leave a comment below. Thank you for reading, we are always excited when one of our posts can provide useful information on a topic like this!
Read more
Leave a comment