I'm using python 2.7.13
At first the browser was showing the raw code.
what I did:
AddHandler cgi-script .cgi .pl .asp .py
At the top of all my scripts I added this:
#!j:/Installeds/Python/python
print "Content-type: text/html\n\n"
Now it's giving me Internal Server Error (500)
and I have no idea what else to try... First time with python.
Obs: I think this may help> Apache>Error.log
[cgi:error] [pid 6364:tid 1620] (9)Bad file descriptor: [client ::1:51083] AH01222: don't know how to spawn child process: C:/Files and Installs/Xampp/htdocs/Test/main.py
AH02102: C:/Files and Installs/Xampp/htdocs/Test/main.py is not executable; ensure interpreted scripts have "#!" or "'!" first line
Download and install the latest version of Python from https://www.python.org/downloads.
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:
Copy and paste the following code at the end of the file:
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
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>
If Apache was running while editing, now is the time to restart it.
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.
Im running ubuntu 16.04 so my answer might be a little different. I am using a google chrome browser with a python 3 file called test.py in /opt/lampp/htdocs/PythonProject:
#test.py
#!/usr/bin/env python3
print('Content-type: text/html\r\n\r')
print("<p>hello world!</p>")
print("I can view this in my browser yay!!")
I edited my httpd.conf file in /opt/lampp/etc/httpd.conf and I did not add
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
to the end of the file, instead I added .py to the end of the existing line
AddHandler cgi-script .cgi .pl
finally i made the file executable by chmod +x /opt/lampp/htdocs/PythonProject/test.py
and then I just ran it through my browser:
http://localhost/PythonProject/test.py
OUTPUT:
hello world!
I can view this in my browser yay!!
To run a python script: Open any text editor and put this code
#!C:/Users/"Username"/AppData/Local/Programs/Python/Python37-32/python.exe
print("content-type: text/html\n\n" )
print("<br><B>hello python</B>")
In the first line, you have to type the location of the python.exe file after putting a shebang (#!) “username” — username of your PC This will differ from one user to another. You can find the python location from environment variables (see the screenshot below)
py environment variables
output
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With