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

10

119

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

Integrant answered 20/11, 2008 at 10:27 Comment(4)
Already answered here http://stackoverflow.com/questions/193077/...Garaway
Dupe of #193577Prosody
No, it's not answered there. That question queries about distribution issues.Abaca
No, it isn't a dupe. That question is related to distributing python software avoiding library availability and compatibility issues.Abaca
W
194

Just put this in the first line of your script :

#!/usr/bin/env python

Make the file executable with

chmod +x myfile.py

Execute with

./myfile.py
Weissmann answered 20/11, 2008 at 10:32 Comment(10)
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?Replace
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.Classieclassification
@JonathanHartley Do note that this is for Linux, a non-Windows OS. Most Linux flavors have Python pre-installed, always 2, and sometimes 3.Aperture
@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.Classieclassification
@JonathanHartley As seen in #6787193 , most Linux distros have at least Python 2. If you're focusing on the Python 3 part of my comment, I did say sometimes.Aperture
If we are using a virtualenv for our python, would the path for the shebang change?Bisulcate
Is there a way to make a script executable only by only python 3 and not python 2.7?Acosta
@PrahladYeri use #/usr/bin/env python3Unstudied
I had the issue of ./myFile: Command not found. To fix this I encoded it in ansii and not utf-8Colombes
@Replace 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.Underskirt
T
24

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

Tentation answered 9/1, 2013 at 9:53 Comment(1)
Can I decompile PyInstaller's output?Hornback
B
20

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

http://pythonhosted.org/PyInstaller/#using-pyinstaller

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

https://docs.python.org/3.10/library/distutils.html

Britneybritni answered 12/12, 2014 at 17:12 Comment(0)
C
5

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;

#!/usr/bin/python

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

Croat answered 9/1, 2013 at 7:55 Comment(0)
A
5

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

and execute with ./hello.py

Alver answered 7/2, 2018 at 10:24 Comment(0)
T
3

I do the following:

  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.

Toshiatoshiko answered 23/5, 2019 at 1:48 Comment(0)
D
1

Do the following steps:

  1. Add this as first line in to your execution entry point python file
#!/usr/bin/python
  1. Modify script to executable
    chmod +x <script-name>.py
  1. Create a symbolic link to your <script-name>.py from /usr/local/bin
ln -s <path-to-your-script> /usr/local/bin/<executable-name-you-want>

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

Doubleripper answered 19/3, 2021 at 6:57 Comment(2)
You have incorrect args order in step 3. ln -s [source_file_path] [sym_link_path]Hervey
On MacOS systems, the source file path requires the full path, not relative to where you are specifying it.Midbrain
M
1

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:

setup.py:

from setuptools import setup
setup()

setup.cfg:

[metadata]
name = awesome
version = 0.0.1
description = My awesome program is 'awesomer' than yours
author =Awesome Name
email = [email protected]

[options]
packages = find:
install_requires = 
    <YOUR-REQUIREMENTS-HERE-DELETE-IF-NONE>

[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

Masuria answered 21/3, 2023 at 11:4 Comment(0)
B
0

Another way to do it could be by creating an alias. For example in terminal write:

alias printhello='python /home/hello_world.py'

Writing printhello will run hello_world.py, but this is only temporary. To make aliases permanent, you have to add them to bashrc, you can edit it by writing this in the terminal:

gedit ~/.bashrc
Burack answered 11/10, 2016 at 14:49 Comment(0)
F
0

As addition to leo pepes answer above for beginners - I just used auto-py-to-exe and followed these steps in Ubuntu 22.04/Python 3.10/VSC: (auto-py-to-exe is a web-gui using the pyinstaller mentioned above)

pip3 install auto-py-to-exe

start it with the command: auto-py-to-exe

choose your .py file under "scriptfile location"

click on "convert to exe"

later on you can: choose additional files convert in only one file (without folder) export your config to a json file(settings) and much more over the GUI

Fillian answered 7/9, 2023 at 8:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.