How to compress python .exe file size [duplicate] - python

This question already has answers here:
Reducing size of pyinstaller exe
(5 answers)
Closed 11 months ago.
I have converted python file to exe using this
But the size of .exe is 800mb, is there any way I can reduce its size?

Using Pyinstaller
The --onefile flag with pyinstaller (e.g., pyinstaller myprogram.py --onefile or pyinstaller myprogram.py -F puts everything in just a single executable, including all the code of all your installed dependencies, even if you're using just one function from them. That's a lot!
The --onedir or -D flag instead puts the those dependencies in a directory that's packaged alongside your compiled code. It tends to reduce the size of the .exe file considerably.
But still, I wouldn't recommend using Pyinstaller. Having experimented with it extensively, I've found that execution time is incredibly slow on machines that don't already have a Python-friendly environment. (Most of the slow execution time comes from loading those dependencies.)
Using cx_Freeze
For the executables that I build for my department, I use cx_Freeze to build an installer or a disk image.
To do so, all you have to do is write a setup script and then call py setup.py bdist_msi (Windows) or py setup.py dbist_dmg (Linux).
The resulting installer will be relatively small and, once it's installed, it will execute much faster than any of the Pyinstaller options I've seen.

Related

How to create a onefile executable from a python script?

I have a decently complicated .py script, which uses several packages, incl matplotlib, numpy, and one custom, that is compiled from a fortran code. I want to make a windows executable out of this, that is distributeable in a way, that users don't need to have python installed on their computers for it to work.
When I make my .exe with pyinstaller --onefile myprogram.py the resulting .exe doesn't run on its own (if I doubleclick on it, the GUI it contains doesn't open), however, if I have anaconda installed, from anaconda prompt I can run it with .\path\to\program\myprogram.exe and it works all nicely (without creating an environment with matplotlib etc)
My question is: how can I make it so that the one file includes all the dependencies and I can just doubleclick on it and go?
Thanks in advance!

How to install pyinstaller (windows) [duplicate]

This question already has answers here:
How to install PyInstaller? [closed]
(3 answers)
Closed 2 years ago.
I want to convert .py files to .exe files so I can execute them on a webpage. However. every method of doing this involves pyinstaller which I do not know how to install. Could someone either explain the procedure to get pyinstaller OR tell me if there are any other methods to convert .py to .exe without using pyinstaller
Installing Pyinstaller
Installing pyinstaller is pretty simple and straight forward. All you gotta do is pip install pyinstaller or python -m pip install pyinstaller (obviously you have to make sure that your python and pip are in PATH).To check if pyinstaller got installed correctly, simply type pyinstaller in your cmd, if no errors appear, it means, you have pyinstaller installed. If for some reason, pyinstaller installs correctly but still doesn't work, you might want to add it in your PATH. You can consider adding C:\Users\user_name\AppData\Roaming\Python\Python37\Scripts to the Path.
Using Pyinstaller
To convert the .py file into a .exe file, you can run the following commands (Make sure you are in the same directory as your python file) -
pyinstaller file_name.py - Simply converts your python file into an executable. This will create a few folders and files, and when you will run your .exe file, it will open up console and run your script.
pyinstaller -w file_name.py - By adding a -w, console will no longer appear while running your .exe.
pyinstaller -F file_name.py - By adding a -F, only one folder will be created, which will be containing your .exe and no other files.
pyinstaller -i icon.ico filename.py - By adding a -i, you will be able to add a .ico file as your icon.
And of course you can use flags in combination to achieve even better results.
[External Links]
Using pyinstaller - https://pyinstaller.readthedocs.io/en/stable/usage.html#options
Installing pyinstaller - https://pyinstaller.readthedocs.io/en/stable/installation.html#installing-in-windows
Working of pyinstaller - https://pyinstaller.readthedocs.io/en/stable/operating-mode.html

Running python script anywhere [duplicate]

This question already has answers here:
How can I distribute python programs?
(8 answers)
Closed 2 years ago.
I have developed some scripts which I need to share with colleagues who does not have python nor have underlying distributions to run it. How to automatically configure environment and more importantly run the script without even python installed?
I saw some solutions on SO like py2exe. Not sure that it’s the best option. Docker is also not possible since in my case I need something what can work simply by running python3 path/to/program
You can use online coding platforms like repl.it to run scripts from the browser so u don't want to install python locally.
Convert it into a exe file using pyinstaller
Following are the steps:
1. Open cmd on the folder in which you stored your py file
2.type it on the cmd
pyinstaller -- onefile filename.py
If you don't have pyinstaller module then install it using
pip install pyinstaller
It depends on the complexity of the script.
If it is doing complex things that it needs to be run on your computer for, like file input & output, then PyInstaller is probably the way:
pip isntall
pyinstaller -- onefile script.py
If it is just a short script, then Repl.It is a great way to save and share scripts that can be viewed and run right in the browser. It supports installing pip packages and environments. It even has a feature where you can host a terminal app as a website: repl.run
I am using PyInstaller to create a complete standalone executables, that does not depend on a machine having python interpreter. Here is a complete guide: https://datatofish.com/executable-pyinstaller/

Is there a workaround for using Openpyxl with Pyinstaller?

I am getting errors when trying to freeze a script that imports openpyxl - and no .exe is generated.
The same errors if the script contains only 'import openpyxl'.
I am currently using Python 3.7.1, Pyinstaller 3.6, and Openpyxl 2.5.12. I freeze the script by copying it into the Anaconda scripts folder and then using powershell to summon pyinstaller to freeze the script into a single .exe. I have successfully frozen some other scripts using this method.
Searching for answers online showed that openpyxl doesn't like being frozen, but a number of places seemed to say that they had managed to find workarounds.
I have tried a number of the solutions suggested, including placing the hook file in the directory and calling it out as a hidden import in the powershell window:
.\pyinstaller --hidden-import=openpyxl --onefile -w 'script.py'
However, nothing I have found has worked. My script is rather large and I would prefer to not have to re-write it using a different excel module. Is there a way I can freeze my script to allow others to use it without having to install python?
Try using cx_freeze it often gives less errors. Save the file names as given below and run the build.bat file
setup.py
import cx_Freeze
from cx_Freeze import *
setup (
name = "give_name",
options = {'build_exe':{'packages': ['package_name']}},
executables = [
Executable(
"give_name.py",
)
]
)
build.bat
py setup.py build

Is it possible to create a Python program distributable for Windows that can be run from anywhere?

I think my question is quite badly phrased, which may be why I haven't been able to find the answer yet.
I created my program in Python, created an installable .exe file from it using bdist_winisnt. Once the program is installed, I would like to be able to run it from anywhere. It's a command line program, so I would like the user to be able to be in a different directory and still be able to type example.py in command line and the program can run.
Is this possible? Is there a way of including some kind of path instruction in the setup.py which will be run on install so that the computer will always know where it is?
I would also like to be able to do this in Linux at some point, will it work the same?
I'm very new to programming, so I may have made some mistakes with what I have said, apologies in advance.
EDIT: turns out there was a really simple way to do it by adding one line to the setup.py file
Good answer to your question is: http://docs.python-guide.org/en/latest/shipping/freezing/
Options are:
bbFreeze
py2exe (supports Python 3)
pyInstaller (does not support Python 3)
cx_Freeze
py2app (Mac)
Your installer only copies the python script to a specified directory.
In order to run a python script you need to have python installed.
You can use a tool like PyInstaller to convert your script (.py file) into an executable (.exe on windows). The way that works is PyInstaller copies both the python interpretor and your script into a single file so that you can distribuite your program easily.
After you have converted your script into an executable, you need to add it to the path so that your operating system knows where to find it. After you do that, you can run your program from the command line from any directory.
The same process will also work on Linux, but you'd have to make separate distributions of the executable because windows executables are different from linux executables.
Check PyInstaller
PyInstaller is promising solution for creation of executables.
I have tested it on Ubuntu, but documentation claims, MS Windows is supported too.
There are multiple options, one of them being single executable file (which include complete Python).

Categories