Running Python scripts with Xampp

Run Python in XAMPP for Windows

Step 1: Download and Install Python

Download and install the latest version of Python from https://www.python.org/downloads.

Step 2: Configure XAMPP for Python

Open the Apache httpd.conf configuration file located at .../xampp/apache/conf/httpd.conf using a text editor of your choice.

The XAMPP GUI can also quickly access the httpd.conf file:

enter image description here

Copy and paste the following code at the end of the file:

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

Step 2.5: Add Python extension to default page locations (Optional)

Inside the httpd.conf file search for <IfModule dir_module> to add index.py among others to the list of default page locations.

<IfModule dir_module>
    DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm index.py \
    default.php default.pl default.cgi default.asp default.shtml default.html default.htm default.py \
    home.php home.pl home.cgi home.asp home.shtml home.html home.htm home.py
</IfModule>

Step 3: Restart Apache / XAMPP

If Apache was running while editing, now is the time to restart it.

Step 4: Run Python from XAMPP

Create a folder and Python file in the XAMPP htdocs directory; e.g. .../xampp/htdocs/PythonProject/test.py.

At the beginning of your script, you first need to specify the directory of your Python executable. The default location of Python 3.10.0 is C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe, but in your case, it may be different, depending on the version and directory in which you’ve installed Python.

#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe

After that, you can create your Python script.

#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe

print("Content-Type: text/html\n")
print("Hello, World!")

Save the file and open localhost/PythonProject/test.py in your web browser. Your Python script should be running.

Leave a Comment