pip install pyinstaller
mkdir ~/my_build
cd ~/my_build
If Windows or Mac need to add -w if the code runs in a window
pyinstaller --onefile -w /path/to/the/code/mycode.py
Inside the ~/my_build folder will be created
~/my_build/build
~/my_build/dist
~/my_build/mycode.spec
The executable is inside dist and you can run it
~/my_build/dist/mycode
If your code needs to access assets (or resources) files like fonts, images, sounds, you need to add another option
pyinstaller --onefile -w --add-data /path/to/the/code/datafiles/myfile.png:datafiles /path/to/the/code/mycode.py
If you have multiple assets, add more "--add-data" options for each file
pyinstaller --onefile -w --add-data /path/to/the/code/datafiles/myfile.png:datafiles --add-data /path/to/the/code/datafiles/more/secondfile.png:datafiles/more /path/to/the/code/mycode.py
For any file access in your code, replace any 'filepath/myimage.png' or even os.path.join('filepath','myimage.png') for the funtion below
bundle_dir = getattr(sys, "_MEIPASS", os.path.abspath(os.path.dirname(__file__)))
def resource_path(relative):
return os.path.abspath(os.path.join(bundle_dir, relative))
resource_path('filepath/myimage.png')
This new option "--add-data" will be saved inside the .spec file.
To add more files, you can edit the .spec file directly and rebuild the project by just calling the .spec file
cd ~/my_build
pyinstaller --onefile -w mycode.spec