Compile python code in linux

Compiling Python

why the downvotes? Seems like a perfectly valid question for a person who’s unfamiliar with python to ask.

8 Answers 8

You have to have python installed first. It will automatically compile your file into a .pyc binary, and then run it for you. It will automatically recompile any time your file changes.

Yes. They’re byte-code and binary, though they’re not «binaries» in the traditional «the system can just run them» sense.

Python compiles its files to bytecode before executing them. That means you have to have a Python interpreter installed on the target machine.

If you don’t want to install Python on the target machine use py2exe, py2app or something similar.

@Helen Neely, sometimes it’s nice to get a specific answer quickly, instead of mining through a laborious tutorial. Currently, a net of 3 people have found this question useful; that should tell you something.

If you just want to compile sources, without running them, you can do this

this command will compile python code in that directory recursively

compileall script is usually located in directory like

i.e. /lib/python2.6 (or similar, depending on prefixes set a python configuration)

As Lulu suggests, you should make sure that resulting .pyc and .pyo files are executable by the users you care about.

compileall can also be used as a module

import compileall compileall.compile_dir(path) 

Python is an interpreted language, so you don’t need to compile it; just to run it. As it happens, the standard version of python will compile this to «bytecode», just like Java etc. does, and will save that (in .pyc files) and run it next time around, saving time, if you haven’t updated the file since. If you’ve updated the file, it will be recompiled automatically.

You can also run python with a -O flag, which will generate .pyo files instead of .pyc. I’m not sure it makes much difference. If speed is important, use psyco.

And yes, on Unix (including Linux, BSD, and Mac OS X, or in a unix shell on windows) you can use a shebang line at the top of the file to make the file automatically run using python. On windows, the equivalent is to associate .py files with python.exe, and then make sure your PATHEXT environment variable includes «.PY» extensions.

However, for windows, you more likely want to write a gui program in python (possibly using PyQT4 and ERIC4) which has a .pyw file as its main script, and has .pyw associated with pythonw (which comes with python on windows). This will let you run python scripts on windows just like other GUI programs. For publishing and distribution, you probably want to compile to an executable file using something like py2exe, as others mentioned.

Читайте также:  Metasploit kali linux туториал

Источник

How to compile python code in linux

It is also possible to run installed modules using , but that takes module names, not filenames, so is not suitable for a shebang script. (notice that execve(2) forbids nested shebang interpreters; I don’t know if that applies for your case) Look into your log files, probably and You may also need to explicitly add (the compiled C executable for the wrapper) to ; see shells(5) Look also into scsh.

Compile python script in linux

cx_Freeze is a cross-platform way to «freeze» a Python script into standalone binary form. According to their site:

cx_Freeze is a set of scripts and modules for freezing Python scripts into executables in much the same way that py2exe and py2app do. Unlike these two tools, cx_Freeze is cross platform and should work on any platform that Python itself works on. It requires Python 2.3 or higher since it makes use of the zip import facility which was introduced in that version.

Generally, if the first line is

And the file has «x» mode set ( chmod +x yourfile.py )

Then it’s executable. No compiling required.

And yes, folks have to install the things on which you depend. It’s (a) simpler and (b) less surprising if they actually do the installation, so they know what’s really going on.

In linux, try to avoid such things. Most package managers handle dependencies quite fine, just distribute your script and tell what dependencies it needs.

Compile python script in linux, cx_Freeze is a cross-platform way to «freeze» a Python script into standalone binary form. According to their site: cx_Freeze is a set of scripts and modules for freezing Python scripts into executables in much the same way that py2exe and py2app do. Unlike these two tools, cx_Freeze is cross platform and …

How To Make a Python Program Executable in Linux

http://www.learnroosky.com/This video will show you how to create an executable python program in linux

How to compile python with PyInstaller in Linux

the binary file is in the dist folder not build folder

If this is just about the location of that .so; see here:

/usr/lib/python3.5 $ find . -name "*.so" | grep libpython ./config-3.5m-x86_64-linux-gnu/libpython3.5.so ./config-3.5m-x86_64-linux-gnu/libpython3.5m.so 

Another way to find it would be by running

> locate libpython3.5m.so /usr/lib/python3.5/config-3.5m-x86_64-linux-gnu/libpython3.5m.so 

In other words: it should be part of the python3.5 installation of your system. Probably you can just copy it from there for further experiments.

Compilation — Compile Python Program Linux, 2 Answers. Sorted by: 6. You need to add a Shebang at the first line of your file like this: #!/usr/bin/env python. and make your file executable by typing (in a terminal): chmod u+x my_file.py. Usage examplechmod u+x my_file.pyFeedback

Using a compiled Python shell in Linux

CPython’s .pyc files are not text, and do not allow use of a shebang line. The traditional method is to have your called script be tiny; it would simply import a module with the rest of the program, which can then be precompiled. For instance, here is the main script of xonsh:

#!/usr/bin/env python3 -u from xonsh.main import main main() 

This script takes negligible time to compile. It is also possible to run installed modules using -m , but that takes module names, not filenames, so is not suitable for a shebang script.

Читайте также:  Аналог pl sql developer linux

I suggest to code a small C wrapper program running your python shell.

(notice that execve(2) forbids nested shebang interpreters; I don’t know if that applies for your case)

Look into your log files, probably /var/log/messages and /var/log/auth.log

You may also need to explicitly add (the compiled C executable for the wrapper) to /etc/shells ; see shells(5)

Your sshd daemon is probably using Linux Plugin Authentification Modules. So read more about PAM.

Create a file /usr/bin/shell_wrapper that contains this one line:

#!/usr/bin/python /usr/bin/ourshell.pyc 

The compiled bytecode ourshell.pyc has to live in /usr/bin , or else change the path accordingly. The python path should go to the same version that compiled the bytecode.

Then make sure to have your /etc/passwd use /usr/bin/shell_wrapper for the shell executable:

user:x:1000:1000::/home/user:/usr/bin/shell_wrapper 

How to compile python with PyInstaller in Linux, I am using Python 3.5.2, PyQt 5.7, PyInstaller 3.2 and I’m in Linux I can compile file.py with : pyinstaller file.py but …

Источник

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

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.

Читайте также:  Все рабочие среды kali linux

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!

Источник

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