Python executable file on linux

Compile Python Scripts to Executable Files

Writing scripts that are based on Python libraries and are meant to be executed more than once is a good practice. It is a good way to ensure that your code is not executing on a system that may not have all the required Python modules installed. Compiling a python script to an executable file is also a good practice. This will ensure that script runs independently of the Python installed on the system. This tutorial will discuss different ways to compile Python scripts to executable files. This includes compiling the script on Linux and windows.

Prerequisite:

For compiling the python script into a .exe file first, we need a Python script here we created a simple python script that gives you simple messes when clicking one button.

import tkinter as tk root= tk.Tk() canvas1 = tk.Canvas(root, width = 299, height = 299, bg='black') canvas1.pack() def hello (): label1 = tk.Label(root, text= 'Hello Programmer!', fg='red', font=('helvetica', 12, 'bold')) canvas1.create_window(150, 200, window=label1) button1 = tk.Button(text='say hello',command=hello, bg='white',fg='black') canvas1.create_window(150, 150, window=button1) root.mainloop()

How to Compile a python script for windows:

We are going to use three python libraries to Compile a python script into an executable file for windows

Читайте также:  Import xml with php

1.Compiling the script using Pyinstaller library:

Step 1:

To install pyinstaller use the following command in the command prompt.

Pyinstaller: With PyInstaller, you can bundle your Python application and all of the packages it relies on into a single, compact executable. This reduces the need to install dozens or hundreds of packages when deploying an application or when helping students set up their programming environment. PyInstaller supports Python 2 and above.

Step 2:

Create a folder and save the python script in it. For example, we created a folder name exe_app and saved my python script named app.py in it.

Step 3:

Open a command prompt in the same folder where our python script is saved and type the following command and press enter.

pyinstaller --onefile app.py

Output:

Step 4:

After running the command you can see that more files and folders are created in the root folder. There you can see folder name dist in that you can find the . EXE file.

You can open the app.exe file to see it running like a normal windows application, you can click on the say hello button that shows the message.

2. Compiling the script using Py2exe library:

Step 1:

To install Py2exe use the following command in the command prompt.

Py2exe: py2exe library is the same as the pyinstaller library.

Step 2:

You need to create a new file setup.py at the same directory where you have the script. This file contains the below parameters.

from distutils.core import setup import py2exe setup(windows=['app.py'])

in place of app.py, you can add your python file name. And if your script runs in the console replace windows for the console.

Step 3:

Open a command prompt in the same folder where our python script is saved type the following command and press enter.

Step 4:

After running the code folder name dist is created in the root folder and in the dist folder, you can find that app.exe is created.

3. Compiling the script using the auto-py-to-exe library:

Step 1:

To install auto-py-to-exe use the following command in the command prompt.

auto-py-to-exe (GUI Tool): auto-py-to-exe is also a library that converts Python scripts into executable Windows programs, able to run without requiring a Python installation. But it has a graphical interface.

Step 2:

To open the auto-py-to-exe use the following command in the command prompt.

Output:

As you can see it opens a graphical interface that is already self-explanatory you just have to select the app.py file and select what you want in the given option and press on convert.py to .exe and that’s it. Then it will automatically open the folder of the exe file that you lunch.

How to Compile a python script for Linux:

Step 1:

For Linux, we can use the pyinstaller library as well but for compiling python script for Linux we need to do this process on a Linux system otherwise it will automatically make a .exe file

To install pyinstaller use the following command in the terminal.

Step 2:

Same as before creating a folder and saving the python script in it. For example, we created a folder name exe_app and saved my python script named app.py in it.

Step 3:

Open a terminal in the same folder where our python script is saved type the following command and press enter.

pyinstaller --onefile app.py

Output:

Step 4:

After running the command you can see that more files and folders are created in the root folder. There you can see folder name dist in that you can find the file name app.

you can launch the app file by using the following common in the terminal in the same directory.

Conclusion:

It is sometimes necessary to compile (aka build) a python script into an executable file that can be run independently of a python environment. Python does come with its built-in tools for doing so by itself, and in this post, we looked at the many types of the python library that you can easily create a standalone executable program on windows and Linux. for Windows systems, we used pyinstaller and py2exe. We also looked at the auto-py-to-exe, which is a graphical frontend for automatic script compilation. For Linux, we examined pyinstaller. All of these methods are easy to use and if you have any questions don’t hesitate to comment below!

Источник

2. Using Python on Unix platforms¶

2.1. Getting and installing the latest version of Python¶

2.1.1. On Linux¶

Python comes preinstalled on most Linux distributions, and is available as a package on all others. However there are certain features you might want to use that are not available on your distro’s package. You can easily compile the latest version of Python from source.

In the event that Python doesn’t come preinstalled and isn’t in the repositories as well, you can easily make packages for your own distro. Have a look at the following links:

2.1.2. On FreeBSD and OpenBSD¶

pkg_add -r python pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages//python-.tgz
pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz

2.1.3. On OpenSolaris¶

You can get Python from OpenCSW. Various versions of Python are available and can be installed with e.g. pkgutil -i python27 .

2.2. Building Python¶

If you want to compile CPython yourself, first thing you should do is get the source. You can download either the latest release’s source or just grab a fresh clone. (If you want to contribute patches, you will need a clone.)

The build process consists of the usual commands:

./configure make make install

Configuration options and caveats for specific Unix platforms are extensively documented in the README.rst file in the root of the Python source tree.

make install can overwrite or masquerade the python3 binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix /bin/python version .

These are subject to difference depending on local installation conventions; prefix and exec_prefix are installation-dependent and should be interpreted as for GNU software; they may be the same.

For example, on most Linux systems, the default for both is /usr .

Recommended location of the interpreter.

prefix /lib/python version , exec_prefix /lib/python version

Recommended locations of the directories containing the standard modules.

prefix /include/python version , exec_prefix /include/python version

Recommended locations of the directories containing the include files needed for developing Python extensions and embedding the interpreter.

2.4. Miscellaneous¶

To easily use Python scripts on Unix, you need to make them executable, e.g. with

and put an appropriate Shebang line at the top of the script. A good choice is usually

which searches for the Python interpreter in the whole PATH . However, some Unices may not have the env command, so you may need to hardcode /usr/bin/python3 as the interpreter path.

To use shell commands in your Python scripts, look at the subprocess module.

2.5. Custom OpenSSL¶

  1. To use your vendor’s OpenSSL configuration and system trust store, locate the directory with openssl.cnf file or symlink in /etc . On most distribution the file is either in /etc/ssl or /etc/pki/tls . The directory should also contain a cert.pem file and/or a certs directory.
$ find /etc/ -name openssl.cnf -printf "%h\n" /etc/ssl 
$ curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz $ tar xzf openssl-VERSION $ pushd openssl-VERSION $ ./config \ --prefix=/usr/local/custom-openssl \ --libdir=lib \ --openssldir=/etc/ssl $ make -j1 depend $ make -j8 $ make install_sw $ popd 
$ pushd python-3.x.x $ ./configure -C \ --with-openssl=/usr/local/custom-openssl \ --with-openssl-rpath=auto \ --prefix=/usr/local/python-3.x.x $ make -j8 $ make altinstall

Patch releases of OpenSSL have a backwards compatible ABI. You don’t need to recompile Python to update OpenSSL. It’s sufficient to replace the custom OpenSSL installation with a newer version.

Источник

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