BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
Posted by Andy Brown on 17 December 2021
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Creating executable files from Python programs
To package a program that you've created into a single executable file, the first thing that you need to do is to install the pyinstaller module. In Visual Studio Code, for example, you can do this by running the following command in the terminal window:
On my current version of VS Code this installed a lot of dependent modules too!
Installing Pyinstaller adds all of these other modules too.
You then need to write a program. I'm using one which displays this form:
My program displays a basic form, and counts the number of links in it.
For the above example, this would show the following at the time of writing:
The program counts the number of hyperlinks in the given webpage, and shows this number.
Please don't judge me on this program: I just wanted something quick to use for an executable, which imported at least one installed module (in this case requests). You can download the program here if you want to use it.
You can then go to the Visual Studio Code (or other) terminal window, change the directory to your folder and run the pyinstaller script:
Change your directory to the folder containing your Python program, then install it by running the command shown. The --onefile directive ensures you will get a single executable file.
This builds the following files and folders:
The installer has added 3 folders in my location: it's the dist one which contains the executable file.
You can now run your executable!
You can double-click on the file created to run it.
This will run on your computer even if you don't have Python or any of the modules installed. The only limitation is that you must run it on the same platform as you used to create the installation (be this Windows, Linux or Mac). You can see the full pyinstaller manual here.