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

PythonLinuxFile Permissions

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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/distutils/builtdist.html

Solution 4 - Python

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

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

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

Solution 8 - Python

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 /usr/local/bin/<executable-name-you-want> <path-to-your-script>

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionclintonView Question on Stackoverflow
Solution 1 - PythonVincent Van Den BergheView Answer on Stackoverflow
Solution 2 - PythonMihai8View Answer on Stackoverflow
Solution 3 - PythonLeo PepeView Answer on Stackoverflow
Solution 4 - PythonMohit DabasView Answer on Stackoverflow
Solution 5 - PythonNilesh K.View Answer on Stackoverflow
Solution 6 - Pythondan_the_ham-manView Answer on Stackoverflow
Solution 7 - PythonCocoView Answer on Stackoverflow
Solution 8 - PythonGandharva S MurthyView Answer on Stackoverflow