New to Linux or Python? Having Python script to run and feeling lost? See how to run a Python script in Linux.
There are two ways on running Python scripts in Linux.
Method 1. Python3 command
Using your terminal you can run a script by typing python3 YourScript.py command. That’s it. Your script should run.
Method 2. Using a dot slash
The alternative is to run ./YourScript.py It will run Python script as well.
There are a few things you need to remember:
1. Your script needs to start with
#!/usr/bin/env python3
This will allow to run the script under Linux. If this is missing just add it in the first line of the file. Don’t worry. This will not change the logic of the script.
2. Your file needs to be executable. You need to have permission to execute the script to be able to run the Python programm. To change the permission you need to allow script to be executable for your user by
chmod +x YourScript.py
3. Path of your Python needs to be assigned properly. This is how to set it in Linux:
export PATH="$PATH:/usr/local/bin/python"