How to run Python scripts


Contents:

General information

Please read this first.

This page is meant to help users how to run Python scripts on their system.

Below you can find some basic information about the Python framework and how to execute Python scripts on different operating systems.


Please do not contact me if you have any questions or problems installing Python or to ask how to run scripts on your system that are not a part of any of the projects provided by this website.

If you need any help you may search the internet (e. g. official documentation, how-to pages and forums) or ask someone who is using the same operating system.


Python framework

The framework to get Python scripts running.

Python logoFirst of all, to execute Python scripts, you need the Python framework installed on your system, which is available for various platforms.

Details can be found on the official Python website.


For now, all Python projects on this website are developed using Python 2.7, so they require the appropriate version of the Python framework. Some projects may also run with earlier versions of the framework (further information can be found on the page of each project).

There are also versions of each project for the Python 3.x framework. The code of these versions was simply converted from Python 2.x to 3.x syntax using the 2to3 tool and has only been tested fundamentally.

If you do not know which Python framework you should install, you may have a look here.

Unix-like systems

On some Unix-like systems (such as Linux and BSD derivatives) the Python framework may already be installed by default. If not, it can either be installed using a package manager (if available) or manually by downloading the corresponding package from the download area on the Python website.

To check if Python is already installed on your system, you may run the following command on a shell:


$ python --version

This command will return the version number of the Python framework, if installed. In case multiple Python frameworks are installed, it returns the version of the one that is set as default.

To explicitly get the version of the Python 2.x framework you may type


$ python2 --version

and for the Python 3.x framework the command looks like this:


$ python3 --version

Windows

To run Python scripts on Windows operating systems, you have to download the corresponding installer from the download area on the Python website.



Run scripts on Unix-like systems

How to run Python scripts on Unix-like systems (such as Linux and BSD derivatives).

The following example shows how to run a Python script named foobar.py on the shell of a Unix-like system. In this example, the script file is located in the /home/johndoe directory.


If a Python script requires command-line arguments to operate, simply double-clicking on the script file will not work.

Due to this, such scripts need to be executed either using the shell, a shell script or a shortcut which contains all the required arguments.

Due to the shebang in the first line of the script file, the system should be able to automatically determine the path of the Python interpreter binary.

Absolute path

You can simply run the script by typing:


$ /home/johndoe/foobar.py

If you get some sort of "permission denied" message, you can make the file executable via

$ chmod +x /home/johndoe/foobar.py

and retry. If this also will not work, you can run the script by using the following command. If multiple Python versions are installed, it uses the interpreter of the framework that is set as default.


$ python /home/johndoe/foobar.py

To explicitly use the Python 2.x interpreter you may type


$ python2 /home/johndoe/foobar.py

and for the Python 3.x interpreter the command looks like this:


$ python3 /home/johndoe/foobar.py

Relative path

If you are inside the directory where the script file is located, you can type ./ instead of the full path to the script. This refers to the current path on the shell. For example:


$ ./foobar.py

If you get some sort of "permission denied" message, you can make the file executable via

$ chmod +x foobar.py

and retry. If this also will not work, you can run the script by using the following command. If multiple Python versions are installed, it uses the interpreter of the framework that is set as default.


$ python foobar.py

To explicitly use the Python 2.x interpreter you may type


$ python2 foobar.py

and for the Python 3.x interpreter the command looks like this:


$ python3 foobar.py

Run scripts on Windows

How to run Python scripts on the Windows operating system.

If you are using Windows, you can run the Python scripts using the Windows command prompt.


If a Python script requires command-line arguments to operate, simply double-clicking on the script file will not work.

Due to this, such scripts need to be executed either using the command prompt, a command-line script or a shortcut which contains all the required arguments.

The following example shows how to run a script named foobar.py inside the directory C:\Stuff.


Absolute path

You can simply run the script by typing:


C:\Users\John Doe> C:\Stuff\foobar.py

For example, to explicitly use the Python 2.x interpreter whose executable python.exe is located inside the directory C:\Python27 you may type:


C:\Users\John Doe> C:\Python27\python.exe C:\Stuff\foobar.py

Relative path

If you are inside the directory where the script file is located, you can type the above command without the full path to the script. For example:


C:\Stuff> foobar.py

For example, to explicitly use the Python 2.x interpreter whose executable python.exe is located inside the directory C:\Python27 you may type:


C:\Stuff> C:\Python27\python.exe foobar.py