Option 1: Call the interpreter
- For Python 2:
python <filename>.py - For Python 3:
python3 <filename>.py
Option 2: Let the script call the interpreter
- Make sure the first line of your file has
#!/usr/bin/env python. - Make it executable -
chmod +x <filename>.py. - And run it as
./<filename>.py
Option 1: Call the interpreter
- For Python 2:
python <filename>.py - For Python 3:
python3 <filename>.py
Option 2: Let the script call the interpreter
- Make sure the first line of your file has
#!/usr/bin/env python. - Make it executable -
chmod +x <filename>.py. - And run it as
./<filename>.py
Just prefix the script's filename with python. E.g.:
python filename.py
How to execute python file in linux - Stack Overflow
How to execute a python script?
what is the simplest way to run a a python script?
Running Python Programs from a Terminal
Videos
Method one: some as Windows (not the best option)
Tell the file-manager to open files ending in .py in python /usr/bin/python3 (note the slash at the start).
Then double click it.
Method two (not the best option)
Open a shell and type python3 «script name»
Method three (the preferred method)
- Add
#!/usr/bin/python3as the first line of the script. - Make the file executable
chmod +x «script name»(or right click in your file-manager).
side note:
With this method it is better to remove the .py, as it is not needed, and leaks the implementation: All callers of the script need to know the programming language.
first get your desktop mint with this command :
Copycd /home/<USER>/Desktop
after enter just enter command:
Copypython3 myscript.py
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 :
Copy#!/usr/bin/env python
or the real path
Copy#!/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)
I suggest that you add
Copy#!/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:
Copychmod u+x name_of_script.py
should do.
Right clicked on "HelloWorld.py" > properties > ticked "allow executing the file as a program" > clicked on it and received the following options -
- run in terminal
- display
- cancel
- run
Tried all that but it doesn't execute.
What am i missing?