33

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.

Karl Knechtel
61.5k14 gold badges134 silver badges194 bronze badges
asked Dec 18, 2012 at 12:36
0

7 Answers 7

49

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)

answered Dec 18, 2012 at 12:39
Sign up to request clarification or add additional context in comments.

1 Comment

Use the /usr/bin/env method instead of a fixed path. This will play nicely with virtualenv when someone (possibly you in the future) will want to execute your file with a different version of Python than the default on the system.
14

I suggest that you add

#!/usr/bin/env python

instead of #!/usr/bin/python at the top of the file. The reason for this is that the python installation may be in different folders in different distros or different computers. By using env you make sure that the system finds python and delegates the script's execution to it.

As said before to make the script executable, something like:

chmod u+x name_of_script.py

should do.

answered Dec 18, 2012 at 12:44

Comments

8

yes there is. add

#!/usr/bin/env python

to the beginning of the file and do

chmod u+rx <file>

assuming your user owns the file, otherwise maybe adjust the group or world permissions.

.py files under windows are associated with python as the program to run when opening them just like MS word is run when opening a .docx for example.

answered Dec 18, 2012 at 12:39

Comments

8

1.save your file name as hey.py with the below given hello world script

#! /usr/bin/python
print('Hello, world!')

2.open the terminal in that directory

$ python hey.py

or if you are using python3 then

$ python3 hey.py

answered Dec 20, 2016 at 4:09

Comments

7

Add to top of the code,

#!/usr/bin/python

Then, run the following command on the terminal,

chmod +x yourScriptFile
answered Dec 18, 2012 at 12:40

Comments

3

Add this at the top of your file:

#!/usr/bin/python

This is a shebang. You can read more about it on Wikipedia.

After that, you must make the file executable via

chmod +x your_script.py
answered Dec 18, 2012 at 12:40

Comments

3

If you have python 3 installed then add this line to the top of the file:

 #!/usr/bin/env python3

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

For more details, follow the official forum:

https://askubuntu.com/questions/761365/how-to-run-a-python-program-directly

chainstair
8379 silver badges20 bronze badges
answered Aug 3, 2019 at 18:05

1 Comment

I think you are missing the # to complete the shebang. Should be #!/usr/bin/env python3