Search code examples
pythonlinux

How to execute python file in linux


Using linux mint, to run a python file I have to type python [file path] in the terminal. Is there a way to make the file executable, and make it run the python command automatically when I double-click it?


See also: Why do I get non-Python errors like "./xx.py: line 1: import: command not found" when trying to run a Python script on Linux? for a common problem encountered while trying to set this up.


Solution

  • You have to add a shebang. A shebang is the first line of the file. Its what the system is looking for in order to execute a file.

    It should look like that :

    #!/usr/bin/env python
    

    or the real path

    #!/usr/bin/python
    

    You should also check the file have the right to be execute. chmod +x file.py

    As Fabian said, take a look to Wikipedia : Wikipedia - Shebang (en)