Deborah R. Fowler
Python Compiled
Posted on Feb 5 2022 to include python to exeConverting python files to exe
Python is an interpreted language and in general terms it will be slower than a compiled language. But, did you know you can compile python code? Here's where we get into grey areas.
I decided to do this one day because I wanted students to compare user input design of several exercise results without showing the code. So of course I googled it and found an excellent instructions at
https://towardsdatascience.com/how-to-easily-convert-a-python-script-to-an-executable-file-exe-4966e253c7e9
In order to convert your python files to executable format, there are two easy ways:
- auto-py-to-exe (has a gui interface)
- PyInstaller
AUTO-PY-TO-EXE
I used the first option. In a cmd window you can pip install auto-py-to-exe and to run it just type the name and it brings up a gui
Open a cmd window and type
pip install auto-py-to-exe
then
auto-py-to-exe
Follow the instructions on the gui
Select your file, then convert py to exe. It will run the conversion and put the result in an output folder located where you ran the command in the cmd window - in this case my C:\Users\Deborah folder
Open output folder and place the executable with the data files in this case. Done!
PYINSTALLER METHOD 2:
I couldn't resist testing the other method as well. In a cmd window do the following:
pip install pyinstaller
cd toTheFileLocation
pyinstaller --onefile scriptname.py
It also spews out information into the terminal (cmd) window and the resulting exe file is placed in the dist folder. I deleted the extra info and moved the executable to the top level so that the data files would be read properly.
Either way works!
The above methods do not really speed up the code, but make it more convenient, particularly if you do not want to share the source. However, there is something called
Cython https://cython.org/
Thanks again to Christoph Gohlke for his repository of whl files
- I downloaded Cython-0.29.28-py3-none-any.whl (to install pip install name.whl)
- For this to work you much have python3-dev installed