[SOLVED] How to shrink a Python 2.7.3 installation for low-storage system?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to shrink a Python 2.7.3 installation for low-storage system?
Hi,
I'm working on a project involving building a Linux-based controller system, and I want to use Python to write some scripts and some controller code.
I'm working with an x86-based system, so I don't need to do anything really odd. No compiling for ARM here or anything to worry about - it books Linux and runs it just as a PC does.
However, I only have 128MB of flash-based storage to work with for the core OS in the final system. I have 256MB of RAM and the storage limitation is not a problem at all for anything else, except Python's default installation wants to eat up well over 200MB.
Most of this is in /usr/lib/python-2.7, with a large chunk (almost 100MB) being in /usr/lib/python-2.7/lib-dynload.
I've read you can remove things like the "test" directory and such but I've only been able to successfully bring the install down to about 170MB.
It seems to me there has to be a way to reduce the size of this installation. Python is a really good language to accomplish the tasks I'm doing, but I don't have the room to install it in its full implementation.
Is there any good information available on how I can shrink the installation down as much as possible? I could probably spare a max of say 30MB or so for Python. (The rest of the OS is LFS-based and fits in under 70MB.) We obviously want to keep a little space free for logfiles or whatnot.
(Please, no obvious answers like "upgrade the storage" - that's not an option at this point. I'm looking to see if there's actually a way to trim down a Python installation.)
It seems that there would be some way to achieve this - after all, Python3's installation fits in about 90MB, and (for comparison's sake) PHP5 fits into 6MB! (There's of course also implementations for things like Symbian/S60 that fit into a couple of MB...) Just seems unlikely that there's no way at all to reduce this installation. As an absolute last resort I could write some of the scripts in PHP but Python is just a lot more suited for the task at hand.
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,513
Rep:
No python "Development Environment" is required to run your application.
A basic run-time python, see e.g. the Fedora 18 "run time" :
The python-2.7.3 package is 90 kB installed.
And python-libs-2.7.3 : A 5.5 MB rpm, 24 MB installed >
> 2200 files, and you may even not need all those.
I guess you can set up a "development computer" with your target OS.
And develop the app on a full python install.
Then transfer the result + the dependencies to the "Controller system".
The most simple (and radial) way to shrink Python installation is not install Python at all. But you still want to write your software in Python... So you can install Python on your development box, and then use pyinstaller http://www.pyinstaller.org/ to pick up only python files that your code will actually use
knudfl: Your post intrigued me to research further about why my Python install is so huge. Turns out it was simple - the shared Python library wasn't being compiled, thus all my .so files were huge. (5MB for time.so did seem kind of big to me!)
I recompiled with --enable-shared and this brought the full install down to 70MB.
Then I did all of the following:
* Use "strip" to remove all debugging symbols from the shared library and from all libraries in lib-dynload
* Removed the test directory
* Removed the .py source files for the libraries and the .pyo optimized versions (after reading docs, there's no advantage to using the optimized versions over the pyc versions)
All of this brought the install down to 28MB - on par with the package size you mentioned!
Some selective library removal I'm sure will reduce this even more. I've now met my goal. :-)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.