Compile python to exe on linux

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
Читайте также:  Farming simulator 22 dedicated server linux

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

Источник

How to Convert .Py Files to .Exe (Windows, macOS, Linux)

How to Convert PNG to TIFF in Python?

A .py file extension is used when the file of a program or script is written in Python . So, these files ending with the .py extension can be written and modified with almost any text editor. However, if you want to run the file, you would need a Python interpreter .

On the other hand, a .exe file extension is used by Windows executable program files. EXE stands for “executable” and it’s very commonly used by programs on Windows which is why it may sound familiar to you. This is because EXE files contain code that can be easily understood and directly executed by a computer.

That being said, there are many reasons why someone may want to convert a .py file to .exe. Maybe you just created an app using Python and want your friend to test it. Besides that, maybe you need someone to run a script you just wrote to automate a task for them. So, double-clicking on an executable file is much easier than for example, downloading and installing Python and running the file from the command line.

Читайте также:  Jedi academy server linux

Fortunately for us, there are tools that we can use to do this in a few simple steps.

In this guide, we’ll go over how to convert a .py file to .exe on Windows , Linux , and macOS .

Table of Contents

Using pyinstaller to convert .py file to .exe (Windows, Linux, and macOS)

PyInstaller is a Python package that compiles a Python application and its dependencies into one package. So, this packaged app can then be executed by the user without the need to install the Python interpreter or any other modules.

Before we begin the conversion of a file from .py to .exe, we will need to install a package called PyInstaller . In order to install a package using the native command line, simply enter:

For this example, I will be using a simple program called test.py that prints out a string. Once PyInstaller has been installed, proceed to:

open terminal on macOS or Linux

Using pyinstaller to convert .py files or scripts to .exe files

Once you see this at the end of the command line, you’re good to go!

Using pyinstaller to convert .py files or scripts to .exe files

  1. Next, navigate to your project’s folder. There will be a few new files and folders created in your project’s folder, one of them being called dist . Open the dist folder and there will be another folder called test .

Using pyinstaller to convert .py files or scripts to .exe files

Inside this test folder, there will be an exe file named after your original python file. In this case, it is named test.exe.

Opening py files that have been converted to .exe file

When double-clicked, a Command Prompt window will open and the code will be executed.

Opening py files that have been converted to .exe file

This same method can be used to convert files from .py to .exe on Linux and macOS as well.

Using cx_freeze to convert .py file to .exe

Another way to convert .py files to .exe is by using cx_freeze . Cx_freeze is used to create executable files from python scripts and it is cross-platform.

Before starting, let’s say we want to convert a program called test.py . To use cx_freeze, we have to:

  1. First, create a file called setup.py in the same folder where test.py is located.
  2. Then, edit the setup.py file to contain the following:
from cx_Freeze import setup, Executable setup(name = "test", version = 1, executables = [Executable("test.py")])
  1. After that, open the Command Prompt and navigate to the folder where setup.pyand test.py are located.

run Command Prompt as an administrator on Windows

Once completed, the test.exe file will be found in a folder that is located inside the same folder as setup.py. For example, my setup.py is inside a folder called test. Hence, my test.exe can be found in test\build\exe.win-amd64-3.8 .

Opening py files that have been converted to .exe file

Using Auto PY to EXE application to convert one or more .py files to .exe (Windows)

Auto PY to EXE is a GUI application that makes it extremely easy and straightforward to convert files. In order to use Auto PY to EXE, we have to first install it through the command line:

run Command Prompt as an administrator on Windows

Then, a GUI should appear on your screen.

Читайте также:  Drweb linux серийный номер

Using Auto PY to EXE application to convert one or more .py files to .exe

The next step is to browse and select the .py file that you wish to convert to .exe.

Using Auto PY to EXE application to convert one or more .py files to .exe

Once the file has been selected, you need to choose either the One Directory or One File option. One Directory essentially means that all the files and dependencies related to the project file will be placed in a single directory. One File means that the application will create a single .exe file consisting of all project dependencies excluding media files.

For this example, I will be proceeding with the following configuration:

Using Auto PY to EXE application to convert one or more .py files to .exe

Once the conversion has been completed, you will see a similar message in the output box:

Using Auto PY to EXE application to convert one or more .py files to .exe

So, proceed to the folder mentioned to find the converted .exe file:

Opening py files that have been converted to .exe file

You can read more about using Auto PY to EXE here .

Conclusion

Learning how to convert a python file to an executable file is a very useful skill for any python developer or programmer.

There are many reasons why someone would have to convert a python file to an executable file. This seemingly simple but yet important task can help in many ways ranging from increasing the user-friendliness of a program to enabling a non-computer savvy person to be able to use and understand a script they made to help them automate a certain task.

In this article, we looked at the many different ways for a developer to convert .py files to .exe. These include using PyInstaller , cx_Freeze , and a GUI application called Auto PY to EXE .

With that being said, we hope you’ve found this guide helpful when it comes to making your python files executable in your development space.

Feel free to share this post with your fellow coders to guide them through converting their .py files to .exe!

Источник

Компиляция python кода в exe под Linux

Если на linux машинах python установлен повсеместно, то windows все наоборот. Решил скомпилировать свое приложения в exe, причем желательно что бы это был одинокий exe файл и не тянул с собой дополнительные dll библиотеки.
Для компиляции python кода в бинарные файлы есть очень удобная программа pyinstaller.
Поддержку cross компиляции они уже начали добавлять, но пока она работает не полноценно, поэтому я решил установить python под wine.
Устанавливаем wine. Качаем python.
У меня, кстати, python 2.7.2 отказался устанавливать под wine, а вот 2.7.1 встал без проблем.
Для работы pyinstaller под windows нужен пакет pywin32 , качаем и устанавливаем(http://sourceforge.net/projects/pywin32/.
Качаем pyinstaller(http://www.pyinstaller.org/.
После того как у нас установлен python и pywin32 под wine, мы готовы.
Распаковываем pyinstaller куда-нибудь поближе к установленном python, у меня это было /home/pavel/.wine/drive_c/Python27/progs/pyinstaller-1.5.1
Туда же, в progs, копируем наше python приложение.
Для начала надо что pyintaller создал файл конфигурации для нашей системы

wine python.exe progs/pyinstaller-1.5.1/Configure.py 

Далее создаем spec файл для нашего python приложения

wine python.exe progs/pyinstaller-1.5.1/Makespec.py -F progs/foobar.py 

ключик -F как раз нужен для того чтобы на выходе у нас получился stand-alone executables
Makespec.py сгенерирует файл foobar.spec , который нам необходим для компиляции.
И последнее что осталось сделать, это сбилдить наш бинарник.

wine python.exe progs/pyinstaller-1.5.1/Build.py foobar.spec 

И в директории dist получаем наш foobar.exe .

Источник

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