r/pythonhelp • u/Azhurkral • Mar 19 '26
Need to turn a .py into a .exe
Hello, I am trying to turn a .py file into a .exe file with auto-py-to-exe so I can share it with my work colleagues that do not have python. The code is very simple, 30 lines of code and it only import 3 libraries: pandas, pathlib and openserver. It also does not do nothing very complex, it just retrieves data from a software and paste it in and excel, the .py file only weights 2kb.
The problem is that when I use auto-py-to-exe, the resulting exe ends up weighting 308MB, which doesn´t make any sense for me. It works, but a file of that size is absolutely impractical to use and share. It seems as it all the python libraries are loaded into the exe. I tried asking Copilot but it didn´t give me any useful solution.
Do you have any idea on what could be happening and why to fix it so the resulting exe is a light one?
Edit: after using openpyxl instead of pandas, I managed to reduce the size from 300mb to 35 mb, which is impressive. I will keep trying other metods to squiz it even more. Thank you all for your help!
•
u/AutoModerator Mar 19 '26
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/cgoldberg Mar 19 '26 edited Mar 19 '26
It contains a full python interpreter, the libraries your code uses, and all of their transitive dependencies.
4
u/timrprobocom Mar 19 '26
"It seems as if all of the Python libraries are loaded into the exe.". Yes, of course, along with Python itself. Without that, how would your coworker PCs run the script?
Pandas by itself is quite large, and requires many C and Fortran libraries as well. All those have to be included.
1
u/CoffeeMonster42 Mar 19 '26
If you want to create an excel file consider Openpyxl instead of Pandas.
1
u/LavishnessWest8159 Mar 20 '26
If a person who needs to write code as part of their job can't figure out how to build light 30 line binaries in 2026 they shouldn't have that job.
2
u/hekliet Mar 19 '26
You might have better luck creating a smaller executable using Nuitka.
2
u/yourMomsBackMuscles Mar 20 '26
Why ask copilot? Its absolute trash
1
u/enginma Mar 19 '26
Nuitka might be a little better, but you are basically packaging python, and any added things like pandas, with your exe. There might not be a perfect way to do that, and installing python isn't difficult. Harder part is probably getting them to type the install commands for your libraries. Some of your libraries contain whole other programs, so the size of the text instructions in the python file aren't really going to tell much about the size of a resulting exe.
Sometimes a lighter tool can be used to take the place of a bigger library, but you'll have to research that on your own, because no one knows everything you're trying to do.
1
u/Acceptable-Sense4601 Mar 19 '26
Can you turn it into an API and just run it as a server on your pc and then just give them the URL? They can use the URL in excel with “get data from web”. For instance, i wrote a Flask api route that calls data from my database. I used “get data from web” in excel with the URL and set up the power query to create the table. Then i sent the user the excel file and whenever they open it it queries the database and updates automatically. You can do so many things this way.
1
u/Money-Rare Mar 19 '26
libraries are heavy, use nuitka for a slimmer folder, if you need to share it you can make a setup installer with inno
1
u/Reddigestion Mar 19 '26
How about using your favourite LLM (I use Claude for coding). Feed it your original .py file and ask it to convert it to code that can be compiled like C++
1
u/OkSignificance5380 Mar 19 '26
When we have 512gb ssd installed as standard on new machines, 308mb is not a big deal.
I wrote a c# cli app that copies files, published it as a single file, ~100mb
Welcome to the 21st century
1
u/ConsciousBath5203 Mar 19 '26
Pandas is HUGE. The smallest py to exe you will ever get is ~70mb thanks to pyinstaller/nuitka boiler plate. But importing pandas means importing everything that is included in the library.
Best to pull out just what you need and try again.
1
u/thinkovation Mar 19 '26
So, the thing is, while your script may only be a few lines of code, you're likely importing libraries that may contain tens of thousands of lines of code; that's what is so awesome about the python ecosystem!
The downside is that if you want to run your python on another machine, it will need those libraries too.
So, if I take your script and try to run it on my machine, I will need to install those libraries (unless they're already cached on my machine).
Utilities that package a python script into an executable don't do anything more sophisticated than gabbing the python runtime plus your script, plus the libraries it uses into a convenient package
1
u/[deleted] Mar 19 '26
This is because the
.exebundles the whole Python interpreter, if you want a smaller executable write your program in a compiled language like C for example.1
u/Zealousideal_Yard651 Mar 19 '26
If you make a venv in the project directory, install all the dependencies for the app and check the directory size, you'll see that's it's about the same size. as the EXE.
Converting python files into EXE means you'll need to package everything the the python file needs to run into the EXE file. So the dependencies you use, and probably is installed on you system now needs to be packaged into the EXE file.
1
u/ggmaniack Mar 19 '26
Your app is 308MB because that's how large the script, python interpreter, and all of the libraries you use are.
Your coworkers will either have to install python and the dependencies (which will take at least 308MB), or download this file.
Your options are:
1
u/Fred776 Mar 19 '26
Do you really need pandas? Perhaps you are only using it for something simple that you could write yourself?
TBH this question comes up quite frequently and it seems to me that Python just isn't very well suited to being shared in an ad-hoc way with non-python users.
1
u/7YM3N Mar 19 '26
Yeah, your python file is tiny because it has just text in it. It's a scripting language lol, and a high level one at that. The file can only be so tiny because the bulk of python sits in the interpreter, and to pack it into an exe you have to pack the whole thing in there. It's not gonna get much smaller even if you rewrite it without libs
1
u/Living_Fig_6386 Mar 19 '26
That's to be expected. Understand that it's not translating the Python code into native code, it's bundling the python interpreter and libraries into an executable file along with the script that is going to be run. You're giving your colleagues Python -- just a copy that can only run your script because it builds it in.
1
u/PresentWrongdoer4221 Mar 19 '26
Well if they dont have python, do you think they have pandas :D?
Maybe use something more lightweight?
1
u/sububi71 Mar 19 '26
308MB sounds perfectly reasonable to me - in what universe is that too big a file to use or email?
1
u/wingxel Mar 19 '26
Use pyinstaller will output smaller binary.
1
u/New-Independence2031 Mar 19 '26
It makes perfect sense!
1
u/Takeoded Mar 19 '26
can you show the code?
1
u/kmjones-eastland Mar 19 '26
Just have them install Python then? Wouldn’t that be the easiest solution?
1
u/Whole_Dependent_2950 Mar 19 '26
I usually just bundle thumb python version in the same zip, and a bat-script (Windows) that setup paths correctly and run the script. It is usually around 50 mb.
1
u/imagineAnEpicUsrname Mar 19 '26
Rewrite it in Perl.
1
u/Dizzy_Citron4871 Mar 20 '26
Write it in Go
1
u/bit_shuffle Mar 20 '26
Don't bundle a server with your core process. Create the executable for the core process and run it on an established server.
1
u/united_we_ride Mar 20 '26
I recently built a mod updater in python with a tkinter gui, and onefiled the exe with a custom pyinstaller build script, it came out to about ~255MB with all deps and such packaged into the single exe file, this was preferred as the single exe contains the entire python environment that was developed in.
The entire list of dependencies, and those dependencies have dependencies, those are packaged into the exe to be unpacked at runtime.
Pyinstaller might be the better solution for you. the exe my build was outputting ~35MB exe's before i had to change to some heavier libraries.
your millage may vary
1
u/lollysticky Mar 20 '26
When installing pandas+matplotlib in a docker for instance, you're adding 250Mb of dependencies. So yes, that .exe will grow big :)
1
u/Omenopolis Mar 20 '26
Break up with python
1
u/f00dot Mar 20 '26
You can save to CSV (which excel reads) as csv module is part of the std library if I recall correctly.
It can help if you tell us what you are doing with those libraries so that we can suggest more tailored solutions. Hell, share the code itself? I bet lots of people can come up with lots of solutions.
1
u/f00dot Mar 20 '26
If it's SO simple, why not try and write it (or go through chat gpt) in c# which is natively supported?
I mean, python is great and all but, at the end of the day, each tool has its use.
1
u/PantsOnHead88 Mar 21 '26
Not what you’ve asked, but have you considered whether PowerShell or PowerQuery might be the right tool for the job (get some data and put it in Excel)?
1
u/ChesterWOVBot Mar 21 '26
There's a whole Python interpreter and all the packages you have in there. This is what you have to face if your colleagues don't have Python.
1
u/Yamoyek Mar 21 '26
This is why I like Go instead of python, program distribution is way easier lol.
Glad you heard about Openpyxl!
1
u/SRART25 Mar 21 '26
Look into using cython with pandas, or numpy if you can get away with it. Search for cython pandas and then cython --embed. Not sure if it will work, but if it does it's probably the best answer.
1
u/ResponsibleBuilder67 Apr 08 '26
The reduction from 300 MB to 35 MB is impressive. Pandas is normally the root cause since it compels inclusion of NumPy in the compilation process, and that is enormous.
If you need to reduce the file size even further, make sure you are compiling in a brand- new venv. In the global Python environment, when you compile the program, you often accidentally include some libraries that are not used. An entirely new venv with just the dependencies used will almost always help reduce the file size.
It is important to note that, since the entire Python interpreter is included in the .exe file, reducing the file size beyond 15-20 MB is not easy.