Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze a python script for every OS

Tags:

In the last few years I've written a nice little program in python and now I'd like to distribute it, but my first attempt didn't encounter much enthusiasm, since many potential users didn't like the idea of downloading and installing python and all the dependencies necessary to run my application.

I looked around for weeks searching for a solution to compile somehow my code into something easy to run for everybody, but I'm not really satisfied of what I found.

I have no problem with the idea of freezing the application instead of really compiling it. It just means that the end user will wait a few seconds more to download the application and will occupy more space on his/her hard disk.

My problem is to find a way to package all the application into an executable for every OS (let's say the big ones: windows, mac, linux - obviously a different executable for every OS) without the need to install virtual machines or wine or similar options. I strongly suspect it is possible, since I saw a bunch of ren'py games packaged exactly the way I'd like to package my application, but I don't know how to obtain the same result for my program.

Schematically, my application requires a console to run and includes a few home-made packages, numpy and matplotlib. It also has a system to support multiple languages that dynamically includes a language file, overwriting the previous settings:

exec('from lang.%s import lang' % language) 

I suspect this could create some problems. Finally, the program requires a few folders to save its settings, logs, etc in the first run, so these should be included too. My computer runs ubuntu mate 16.10 (x64) and the python version is 2.7, but in the future I plan to translate it to python 3. The executables created can be single heavy files that contain almost everything needed or lightweight files and everything else can be found in a near folder; I don't really have a preference for one solution or the other.

I am aware that this question pops up from time to time and was probably already answered, but the last occurrence of this kind of question I could find dates back to 2014, which sounds like eons ago if we consider how quickly things change in this field.

Edit: Since there seems to be no solution with the conditions I proposed, I can think of setting up some emulators/wrapper/compatibility layer/whatever to run a freezer for different OSs, but I'm not sure what to do. I read here and there that wine doesn't always work properly and can't find anything to compile for mac on linux. As far as I understand the program shouldn't require a different compilation for 32/64 bit systems, but if there is an easy way to compile for every possibility it'd be nice. Virtual machines are still a no go, right now I don't have the space on my disk to set up two or more virtual machines (that's the great downside of ssd disks...), let alone paying for licences for OSs that would be used a few times a year to compile a free software.

like image 515
GRB Avatar asked Feb 26 '17 11:02

GRB


People also ask

How can I protect my Python code but still make it available to run?

Instead of converting some parts of the code to C, they hide the entire python code inside a protective C layer. Then, if they want a module importable by python, they write a thin python extension on top of the C. Open source is a much easier way of life.

Can Python run on all operating systems?

Python is cross-platform and will work on Windows, macOS, and Linux. It is mostly a matter of personal preferences when it comes to choosing an operating system. According to Stack Overflow's 2020 survey, 45.8% develop using Windows while 27.5% work on macOS, and 26.6% work on Linux.

What is frozen Python?

Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.


1 Answers

Couple things first.

  • Python is already cross-platform
  • Python code is interpreted, not a natively compiled language and does not need compiling
  • There exist standard methods in Python setuptools to distribute executable scripts with cross-platform compatibility
  • Third party python libraries can include native code (in C) that will either need compiling when installing or for binary packages to have been available by the package maintainers. NumPy and matplotlib are in this category and do have binary packages available for windows AFAIK.

Here is some example setup.py code to install an executable script:

from setuptools import setup, find_packages setup(<...>,       entry_points={'console_scripts': [           '<console cmd name here> = my_package.module:main' ]}) 

Where my_package.module is, for example

my_package/module.py:

<..> def main():     <what to run when <console cmd name here> is executed> 

The package can then be distributed as a standard python module, installable via pip. If the code is open source, it can be hosted on PyPi (see instructions on PyPi site for how to setup).

Otherwise, a source or binary wheel package (python setup.py sdist and python setup.py bdist_wheel respectively) can be built and distributed to your users.

Binary wheels will need to built on the platform they are to be installed on, source distributions will work on any platform but will require code to be executed at installation as they contain purely source code.

Note that building/installing binary wheel packages requires pip version >= 6.0 and setuptools >= 26.0.

When this package is installed, a < console cmd name here > executable will be available to run on all platforms the package can be successfully installed.

For a simple example package that does this, see hello-world-python-package, specifically its setup.py.

See pythonwheels site for more information on binary wheel packages.

like image 76
danny Avatar answered Oct 29 '22 04:10

danny