How To Run a Python Script (Python 2/3)

How to Run Python Scripts

This article will show you how to run both Python 2 and Python 3 scripts in Linux and find out what versions of Python are installed.

Running a Script

To run a Python 3 script:

python3 /path/to/script.py

To run a Python 2 script:

python2 /path/to/script.py

Read on to find out how to install Python and find out which versions you have installed.

Installing Python on Linux

You can see what Python packages are installed on your system by running the following commands:

which python

which python3

which python2

… if Python is installed under one of these names, the path to the Python executable will be returned. If not, “not found” will be returned.

Installing Python Version 3

Run the following to install Python 3 on Ubuntu:

sudo apt install python3

Or on RedHat/Fedora based distributions:

sudo yum install python3

Installing Python Version 2

Python 2 went EOL in Jan 2020 – If you are writing new scripts, consider sticking to Python 3

Run the following to install Python 2 on Ubuntu:

sudo apt install python2

Or on RedHat/Fedora based distributions:

sudo yum install python2

Which Version of Python Does Your Script Require?

If you wrote a script, you probably already know what version of Python you wrote it for.

If you’re using someone else’s script, they may have left a note or comment stating which version of Python it was written for.

You can also look at differences in the code – the print command is commonly used in many scripts and has different syntax in Python 2 and Python 3.

Python 3 will use the following print syntax:

print("Hello")

Python 2 will use the following print syntax:

print "Hello"

Finally, you can just try and run the script in Python 3 and, if it doesn’t work, try it in Python 2. This is not preferable as the script may appear to work but behave unexpectedly.

Finding the Default Python Version on your System

It is not recommended to simply run a python script using the python command as it may point to different versions of Python in different environments. Explicitly running python3 or python2 is recommended, so you know that the correct version is being used for the given script.

If you do wish to know the path to the python executable used by default, you can run:

which python

Which will output the path to the executable for that command – something like:

/usr/bin/python

In this case, that doesn’t make it too clear as the version isn’t present in the file name, so we can run that command directly with the -v flag.

/usr/bin/python - v

The version of python will then be printed.

Using !# To Define the Path to the Python Executable for a Script

There is another way to see what version of Python a script was written for or to define which version should be used. Some scripts use the hashbang/shebang line to tell the system which Python executable to use. The hashbang line will always be the first line in a file. It is not required, so it may not be present in all (or many) scripts – it’s up to the author to include it:

#!/usr/bin/python3

This, however, depends on both the script author and your own systems having the same version of python at the same file path. Even if you don’t, the hashbang line can give clues to which version of Python is required to execute the script.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *