Python make linux executable

How do I make a python script executable?

See also: python — setup.py and adding file to /bin/ — Stack Overflow for detailed instruction on the setuptools console_scripts method.

5 Answers 5

  1. Add a shebang line to the top of the script: #!/usr/bin/env python
  2. Mark the script as executable: chmod +x myscript.py
  3. Add the dir containing it to your PATH variable. (If you want it to stick, you’ll have to do this in .bashrc or .bash_profile in your home dir.) export PATH=/path/to/script:$PATH

It is: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Development/adt-bundle/sdk/platform-tools:/Development/adt-bundle/sdk/tools

The best way, which is cross-platform, is to create setup.py , define an entry point in it and install with pip .

Say you have the following contents of myscript.py :

Then you add setup.py with the following:

from setuptools import setup setup( name='myscript', version='0.0.1', entry_points= < 'console_scripts': [ 'myscript=myscript:run' ] >) 

Entry point format is terminal_command_name=python_script_name:main_method_name

Finally install with the following command.

pip install -e /path/to/script/folder 

-e stands for editable, meaning you’ll be able to work on the script and invoke the latest version without need to reinstall

After that you can run myscript from any directory.

@NikitaProkaiev In myscript update , update is a command line argument to the script — the same as if you ran python myscript.py update . It would have no effect unless you wrote code in your script to handle command line arguments. You could define another entry in console_scripts like myscript_update=myscript:update to call, in this case, a function called update .

I usually do in the script:

$: chmod 755 yourfile.py $: ./yourfile.py 

permission table

Another related solution which some people may be interested in. One can also directly embed the contents of myscript.py into your .bashrc file on Linux (should also work for MacOS I think)

For example, I have the following function defined in my .bashrc for dumping Python pickles to the terminal, note that the $ is the first argument following the function name:

depickle() < python ', 'rb') while True: try: print(pickle.load(f)) except EOFError: break EOPYTHON > 

With this in place (and after reloading .bashrc), I can now run depickle a.pickle from any terminal or directory on my computer.

Источник

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

Читайте также:  Установка локали в linux

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

What do I use on linux to make a python program executable I am looking to «complie» the .py to a standalone program so users in a linux environment like ubuntu can run it out of the box without installing python and the libraries I used because no root access. I just installed a linux system (Kubuntu) and was wondering if there is a program to make python programs executable for 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.

Just put this in the first line of your script :

Make the file executable with

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
Читайте также:  Посмотреть версию линукс в терминале

Linux desktop executable python script, The correct way is to use time.sleep () which works both in Python 3.x and 2.x. Use the following code to do that. import time # Should be the first statement. # Some code is below.

Make python program standalone executable under linux

I have done some research but I am not looking for a way to make a script and run the .py file like answered in this question. What do I use on linux to make a python program executable

I am looking to «complie» the .py to a standalone program so users in a linux environment like ubuntu can run it out of the box without installing python and the libraries I used because no root access. I found py2exe for windows. I would think there is a way to achieve this in linux?

Doesn’t pyinstaller compile under the OS you are using?

if you want a windows exe — use pyinstaller in windows environment.

For Linux, use pyinstaller in Linux.

You can use Jython to compile it to JVM. Using the Jython compiler

This would, however, require Java to be installed.

There are also a couple of other resources to compile Python . From Compiling Python Code , some cross-platform tools are

but I do not know anything about those.

Convert Python Code To Executable For Linux, The first line needs to be: #!/usr/bin/python. (no space before ‘python’), and the script needs to be marked executable ( chmod +x scriptname) Share. Improve this answer. answered Apr 24, 2017 at 9:30.

How to create a Linux executable file using python code

For an example I have created welcomeGUI.py file with has a simple GUI as follows using Tkinter.

from Tkinter import * import ttk class Application(): def __init__(self, master): frame = Create_widgets(master) frame.pack() class Create_widgets(Frame): def __init__(self, master): Frame.__init__(self, master) self.toplabel = Label(master, text="Welcome!", font=('cambria', 20, 'bold'), fg="white", bg="Midnight Blue") self.toplabel.pack(fill=X, ipady=100) statuslabel = Label(master, bg="Midnight Blue") statuslabel.pack(fill=X) self.midlabel = Label(master, text="Device ready,connect a flash drive to continue. ", font=('Ubuntu-L', 12), fg= "white", bg="Midnight Blue", anchor="n") self.midlabel.pack(fill=X, ipady=5) bottomlabel = Label(master, bg="Gainsboro") bottomlabel.pack(side=BOTTOM, fill=X) button1 = ttk.Button(bottomlabel, text="Close") button1.pack(side=BOTTOM) #**** Main **** root = Tk() root.title("Projector Plus") root.configure(bg="Midnight Blue") root.minsize(550, 550) pro = Application(root) root.mainloop() 

Then I need to create this file that can be installed on Ubuntu (To create an executable file on Ubuntu). In Windows this is done very easily with .exe file(Using cx-Freeze). Really I have no idea about the file system of Ubuntu and shell files.

Please help me to get an idea about this problem. I don’t have any idea how to enter this matter.

Basically, to make a file executable in Unix systems, you just have to do one thing: allow it to be executed (very surprising 😉 ). To do it, you must use the chmod command as follows: chmod +x youfile.py . The +x add the right to be executed.

Now, your system will allow you to execute the script, but for now it’s just a simple text file. Ubuntu doesn’t know that he must use the python command to run it, so you’ll have undermined comportment. To resolve this, we use the sha-bang line (for more information, see the wikipedia page): at the first line of your script, you must write #! program_to_use , in your case it’s python. Generally, we take benefit of the env variables, and use #! /usr/bin/env python , but you can also choose yourself the version of python you want, doing #! /usr/bin/pythonX.X where **** is the version of python.

Читайте также:  Защита linux от ддоса

1 — Add on the top of your file this line: #!/bin/env python (This may differ depending on your ENV variable)

2 — make your file executable by:

a — In Unix system, run the following command:

b — In Windows System, you can use the utility py2exe found on http://www.py2exe.org/

more on this can be found at:

Executable Python program with all dependencies for, Use PyInstaller in Linux based systems PyInstaller is a program used to convert Python scripts into standalone deployable applications. Install PyInstaller from PyPI: pip install pyinstaller. Go to your program’s directory and run: pyinstaller yourprogram.py. This will generate the bundle in a subdirectory called …

Making python script executable from any directory [duplicate]

I have a python script, which has multiple command line options. I want to make this script runnable without having to type «python myscript.py» and without having to be in the same directory as the script. For example, if one installs git on linux, regardless of which directory the user is in, the user can do «git add X, etc..». So, an example input I would like is «myscript -o a,b,c -i» instead of «python myscript.py -o a,b,c -i». I already added «#! /usr/bin/ env python » to the top of my script’s code, which makes it executable when I type «./myscript», however I don’t want the ./, and I want this to work from any directory.

You should add the folder that contains the script to your system’s $PATH variable (I assume you’re on Linux). This variable contains all of the directories that are searched looking for a specific command. You can add to it by typing PATH=/path/to/folder:$PATH . Alternately, you need to move the script into a folder that’s already in the $PATH variable (which is generally a better idea than messing with system variables).

Your script needs to be in a location searchable via your PATH . On Unix/Linux systems, the generally accepted location for locally-produced programs and scripts that are not part of the system is /usr/local/bin . So, make sure your script is executable by running chmod +x myscript , then move it to the right place with sudo mv myscript /usr/local/bin (while in the directory containing myscript ). You’ll need to enter an admin’s password, then you should be all set.

Easiest way to create executable for windows from a, I want to make a Python 3.6 program which I develop on Ubuntu into an executable for windows. From what I searched online it seems I will need pyinstaller over wine. If there is an easier way, you can stop reading here, and suggest another way. I tried to follow the answer on Cross-compiling a Python …

Источник

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