Python & XAMPP on Windows: how to?

49,812

Solution 1

Yes you are right, mod_python won't work with Python 2.7. So mod_wsgi is the best option for you.

I would recommend AMPPS as python environment is by default enabled with mod_python and python 2.5. AMPPS Website

if you still want to continue,

Add this line in httpd.conf

LoadModule wsgi_module modules/mod_wsgi.so

Uncomment the line in httpd.conf

Include conf/extra/httpd-vhosts.conf

Open vhost file httpd-vhosts.conf and add

NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
    <Directory "path/to/directory/in/which/wsgi_test.wsgi/is/present">
        Options FollowSymLinks Indexes
        AllowOverride All
        Order deny,allow
        allow from All
    </Directory>
    ServerName 127.0.0.1
    ServerAlias 127.0.0.1
    WSGIScriptAlias /wsgi "path/to/wsgi_test.wsgi"
    DocumentRoot "path/to/htdocs"
    ErrorLog "path/to/log.err"
    CustomLog "path/to/log.log" combined
</VirtualHost>

Add the following lines in wsgi_test.wsgi

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Note : Don't make the test directory in htdocs. Because I haven't tried that yet. These steps worked for me in AMPPS. :)

Then access 127.0.0.1/wsgi in your favorite browser. You will see Hello World!.

If you don't see, follow QuickConfigurationGuide

OR

You can add these lines in httpd.conf

<IfModule wsgi_module>
<Directory path/to/directory>
    Options FollowSymLinks Indexes
    AllowOverride All
    Order deny,allow
    allow from All
</Directory>
WSGIScriptAlias /wsgi path/to/wsgi_test.wsgi
</IfModule>

Solution 2

WSGI is a lot better, but at least I googled and tried to set it up for days without success. CGI is less efficient, but as most people use windows for development only, it makes little/no difference. It's super easy to set up!

CGI method:

  1. In xampp\apache\conf\httpd.conf look for this line: AddHandler cgi-script .cgi .pl .asp. Modify it so it looks like this: AddHandler cgi-script .cgi .pl .asp .py
  2. At the top of each Python script you create, set the path to your version of Python. For instance, if yours is in C:\Python27 write: #!/Python27/python
  3. Put test sample test code in xampp\cgi-bin and access localhost/cgi-bin/your-file.py

sample test code(change commented python path according where you have it installed):

#!C:/Python27/python

print "Content-type: text/html\n\n"
print "<html><head><title>Hello World from Python</title></head><body>Hello World from a Python CGI Script</body></html>"

I've tested this in xampp 1.8.1 if something doesn't work read tihs:

source: http://elvenware.com/charlie/development/web/Python/Xampp.html

Share:
49,812
Bonny1992
Author by

Bonny1992

Updated on July 09, 2022

Comments

  • Bonny1992
    Bonny1992 almost 2 years

    I have installed on my Win7x64 Xampp and Python 2.7.

    Now I'm trying to get the "power" of Python language... how can I do it?

    I've tried with mod_python and mod_wsgi but the first one does not exist for my version of Python, and when I try to start Apache after installing wsgi it gives me an error

    < Directory "\x93C:/wsgi_app\x94"> path is invalid
    

    I added a space between < and 'directory' to make the string visible here.

    So... Anyone knows if there is a little tutorial to install these features?

    Or is anyone is kind enough to explain me step by step what shall I do?

    Thanks and sorry if i'm not so able to explain me.

    If you need something, please ask me.

  • Bonny1992
    Bonny1992 over 12 years
    Is the virtual host necessary? :) Thanks for the reply anyway :):)
  • John D
    John D over 12 years
    No it isn't necessary. You can add these lines to httpd.conf too. <IfModule wsgi_module> <Directory path/to/directory> Options FollowSymLinks Indexes AllowOverride All Order deny,allow allow from All </Directory> WSGIScriptAlias /wsgi path/to/wsgi_test.wsgi </IfModule>
  • Bonny1992
    Bonny1992 over 12 years
    Ok with AMPPS works now :) But now can i start python applications from my webserver? :)
  • Bonny1992
    Bonny1992 over 12 years
    If I try to start one of my python script the server gives me error 500 :(
  • Bonny1992
    Bonny1992 over 12 years
    But if I start a script, it works only 1 time :( and the wsgi_test.wsgi works everytime ... how can i make the scripts start?? :(
  • Bonny1992
    Bonny1992 over 12 years
    What kind of files can I start from my webserver? There is some specific language? Or some Python modules? :)
  • John D
    John D over 12 years
    Well for Python scripts, you will have to read that scripts document to start also we must know the python language. you can find the tutorials to learn python from google.
  • Bonny1992
    Bonny1992 over 12 years
    I know python language but my scripts doesn't start on my webserver :)
  • Bonny1992
    Bonny1992 over 12 years
    On local with python idle these scripts starts :) if write l, for example, 'print "hello world"' it gives me error 500 :)
  • John D
    John D over 12 years
    Hi, put the path of your script here. WSGIScriptAlias /wsgi path/to/your_script.wsgi
  • Bonny1992
    Bonny1992 over 12 years
    but can I add only one script at a time? Cannot I select an entire folder? :)
  • John D
    John D over 12 years
    You can select entire directory. Follow link
  • KalenGi
    KalenGi over 10 years
    it's important to note that "WSGIScriptAlias" defines what you type into the browser in order to navigate to the script.