Python compile to executable linux

Create a single executable from a Python project [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

I want to create a single executable from my Python project. A user should be able to download and run it without needing Python installed. If I were just distributing a package, I could use pip, wheel, and PyPI to build and distribute it, but this requires that the user has Python and knows how to install packages. What can I use to build a self-contained executable from a Python project?

3 Answers 3

There are several different ways of doing this.

The first — and likely most common — way is to use «freeze» style programs. These programs work by bundling together Python and your program, essentially combining them into a single executable:

  • PyInstaller:Website || Repo || PyPi Supports Python 3.7 — 3.10 on Windows, Mac, and Linux.
  • cx_Freeze:Website || Repo || PyPi Supports Python 3.6 — 3.10 on Windows, Mac, and Linux.
  • py2exe:Website || Repo || PyPi Supports Python 3.7 — 3.10 on Windows only.
  • py2app:Website || Repo || PyPi Supports Python 3.6 — 3.10 on Macs only.
Читайте также:  Python txt to jpg

The main thing to keep in mind is that these types of programs will generally only produce an exe for the operating system you run it in. So for example, running Pyinstaller in Windows will produce a Windows exe, but running Pyinstaller in Linux will produce a Linux exe. If you want to produce an exe for multiple operating systems, you will have to look into using virtual machines or something like Wine.

Of course, that’s not the only way of doing things:

  • pynsist:Website || Repo || PyPi Pynsist will create a Windows installer for your program which will directly install Python on the user’s computer instead of bundling it with your code and create shortcuts that link to your Python script. The pynsist tool itself requires Python 3.5+ to run, but supports bundling any version of Python with your program. Pynsist will create Windows installers only, but can be run from Windows, Mac, and Linux. See their FAQ for more details.
  • Nuitka:Website || Repo (Github mirror) || PyPi Nuitka will literally compile your Python code and produce an exe (as opposed to the other projects, which simply include Python) to try and speed up your code. As a side effect, you’ll also get a handy exe you can distribute. Note that you need to have a C++ compiler available on your system. Supports Python 2.6 — 2.7 and Python 3.3 — 3.10 on Windows, Mac, and Linux.
  • cython:Website || Repo || PyPi Cython is similar to Nuitka in that it is a Python compiler. However, instead of directly compiling your code, it’ll compile it to C. You can then take that C code and turn your code into an exe. You’ll need to have a C compiler available on your system. Supports Python 2.7 and Python 3.3 — 3.11 on Windows, Mac, and Linux.
Читайте также:  Insert value in session php

My personal preference is to use PyInstaller since it was the easiest for me to get up and running, was designed to work nicely with various popular libraries such as numpy or pygame, and has great compatibility with various OSes and Python versions.

However, I’ve also successfully built various exes using cx_Freeze without too much difficulty, so you should also consider trying that program out.

I haven’t yet had a chance to to try pynist, Nuitka, or Cython extensively, but they seem like pretty interesting and innovative solutions. If you run into trouble using the first group of programs, it might be worthwhile to try one of these three. Since they work fundamentally differently then the Pyinstaller/cx_freeze-style programs, they might succeed in those odd edge cases where the first group fails.

In particular, I think pynist is a good way of sidestepping the entire issue of distributing your code altogether: Macs and Linux already have native support for Python, and just installing Python on Windows might genuinely be the cleanest solution. (The downside is now that you need to worry about targeting multiple versions of Python + installing libraries).

Nuitka and Cython (in my limited experience) seem to work fairly well. Again, I haven’t tested them extensively myself, and so my main observation is that they seem to take much longer to produce an exe then the «freeze» style programs do.

All this being said, converting your Python program into an executable isn’t necessarily the only way of distributing your code. To learn more about what other options are available, see the following links:

Источник

How transform a python program .py in an executable program in Ubuntu? [duplicate]

I have a simple python program and I want an executable version (for Ubuntu Linux) of this program to avoid running it in the terminal with python myprogram.py . How can I do that ?

5 Answers 5

There is no need to. You can mark the file as executable using

Make sure it has a shebang line in the first line:

And your linux should be able to understand that this file must be interpreted with python. It can then be ‘executed’ as

As various others have already pointed out you can add the shebang to the top of your file

#!/usr/bin/python or #!/usr/bin/env python

and add execution permissions chmod +x program.py

allowing you to run your module with ./program.py

Another option is to install it the pythonic way with setuptools. Create yourself a setup.py and put this in it:

from setuptools import setup setup( name = 'Program', version = '0.1', description = 'An example of an installable program', author = 'ghickman', url = '', license = 'MIT', packages = ['program'], entry_points = , ) 

This assumes you’ve got a package called program and within that, a file called program.py with a method called main(). To install this way run setup.py like this

This will install it to your platforms site-packages directory and create a console script called prog. You can then run prog from your terminal.

You can try using a module like cxfreeze

At the top op your python program add:

I know the easiest, exact and the best solution. I had the same problem like you but now, I can run my Python/Tkinter(GUI) program with its icon.

As we create .bat files on Windows, we can also create equivalent the .bat files easily in Linux too. Thanks to this file, that, we can start our programs without terminal even if it needs to get command on terminal to start (like Python programs) with double click to its icon (really .png icon 🙂 ) or we can write commands to facilitate our works. So, how is this going to happen ?

For example, if we want to run our .py program, we just need to write this command to terminal :

So if we create a file that can automatically run this command, problem would be solved. In addition to that, you can have your own icon and even you don’t have to open terminal !

Источник

What do I use on linux to make a python program executable

I just installed a linux system (Kubuntu) and was wondering if there is a program to make python programs executable for linux.

No, it isn’t a dupe. That question is related to distributing python software avoiding library availability and compatibility issues.

9 Answers 9

Just put this in the first line of your script :

Make the file executable with

I’m confused. How does the «#!/usr/bin/env python» work when the hash is supposed to make it a commented line? I tried running the script without the hash line, but it didn’t work. So obviously the line is required, but how does it work if it’s a comment?

If you’re sending scripts to a fellow programmer, this is fine. But this is not a suitable way to distribute Python programs to end users. What if the user doesn’t have Python installed? What if they do, but it’s a different version than you wrote the program in? Overall this will only work for a tiny percentage of users, especially on Windows.

@MathManiac If you proceed as you’re implying, about 15% of users will be unable to run your application. This will be a crippling support burden, not to mention a fantastically hostile user experience, which will generate a torrent of hateful «application X sucks» posts. I stand by my assertion that this is not a suitable way to distribute applications to end-users.

@Nav That’s called a Shebang. It’s commented out because it shouldn’t be interpreted by python. It gives information to the operating system. More specifically it says what program should be used to execute the script.

If you want to obtain a stand-alone binary application in Python try to use a tool like py2exe or PyInstaller.

You can use PyInstaller. It generates a build dist so you can execute it as a single «binary» file.

Python 3 has the native option of create a build dist also:

Putting these lines at the starting of the code will tell your operating systems to look up the binary program needed for the execution of the python script i.e it is the python interpreter.

So it depends on your operating system where it keeps the python interpreter. As I have Ubuntu as operating system it keeps the python interpreter in /usr/bin/python so I have to write this line at the starting of my python script;

After completing and saving your code

  1. Start your command terminal
  2. Make sure the script lies in your present working directory
  3. Type chmod +x script_name.py
  4. Now you can start the script by clicking the script. An alert box will appear; press «Run» or «Run in Terminal» in the alert box; or, at the terminal prompt, type ./script_name.py

If one want to make executable hello.py

first find the path where python is in your os with : which python

it usually resides under «/usr/bin/python» folder.

at the very first line of hello.py one should add : #!/usr/bin/python

then through linux command chmod

one should just make it executable like : chmod +x hello.py

  1. put #! /usr/bin/env python3 at top of script
  2. chmod u+x file.py
  3. Change .py to .command in file name

This essentially turns the file into a bash executable. When you double-click it, it should run. This works in Unix-based systems.

These steps works irrespective of whether you have single standalone python script or if you have multiple dependent script called by your main file.

On MacOS systems, the source file path requires the full path, not relative to where you are specifying it.

as I find it a bit ambiguous, as to what exactly you refer to with a »Program», I present here an answer, how to make a »package»-program executable from the command line in Linux, as this was not answered in this question before.

Essentially you have to follow the official instructions, but in essence, you have to do the following steps:

1.) Refactor your program into the structure presented here (you essentially have the choice between two structures)

2.) Assuming you chose the »flat layout» and your project name is awesome (i.e. assuming your source files lie in program/awesome ), you create two files, setup.py and setup.cfg file, at your program level (i.e. program ), with the contents below:

from setuptools import setup setup() 
[metadata] name = awesome version = 0.0.1 description = My awesome program is 'awesomer' than yours author =Awesome Name email = awesome@program.earth [options] packages = find: install_requires = [options.entry_points] console_scripts = awesome = awesome:main 

3.) In your program/awesome folder you create a __init__.py file, with a main function, where you can then start your »real» program. I.e. put into your __init__.py file at least the following code to see an effect:

def main(): print("MY AWESOME PROGRAM WORKS!") 

4.) Install it using e.g. python setup.py install

5.) Execute it from the command line using awesome , e.g. $> awesome

Hope this helps anyone — Thinklex

Источник

Оцените статью