How to install Python with Wampserver
Solution 1:
Python support can be added to WampServer fairly easily, similar to adding any Apache module that doesn't ship with the base package. You need to take a few extra steps to make sure you can continue to use WampServer console to manage your application stack.
Download mod_wsgi Apache Module
You'll need to get an appropriate binary version of mod_wsgi. WSGI is the preferred method for writing web applications in Python. The binary you use has to match up with the versions of Windows, Apache, and Python you are using. This site provides binaries for current versions: mod_wsgi binaries. NOTE: If you are at all concerned about security, you should probably compile your own version of mod_wsgi from the source code, or only download from a trusted source.
The binary goes into the Apache modules directory. In my case, I downloaded mod_wsgi-3.4.ap22.win-amd64-py2.7.zip (Windows 7 64-bit, Python 2.7, Apache 2.2). On my laptop, the correct directory was c:\wamp\bin\apache\Apache2.4.4\modules.
Update httpd.conf and refresh WampServer
Next, you need to update httpd.conf. This can be done from the WampServer control panel by Selecting Apache->httpd.conf from the menu. Notepad (or your default editor) will launch.
Find the section where there is a bunch of LoadModule statements. At the bottom of this section, add a LoadModule statement for mod_wsgi:
LoadModule wsgi_module modules/mod_wsgi.so
Save the httpd.conf file and exit Notepad.
To let WampServer know that you've made a change, you'll need to refresh it. Right-click the WampServer icon in the system tray, and select Refresh. Now, when you look at the list of modules in the control panel (Apache->Apache Modules) you should see mod_wsgi in the list. If it isn't already checked, go ahead and check it. If Apache doesn't restart automatically, do so now from the control panel.
If Apache doesn't start up, and you are sure you didn't mistype the LoadModule statement above, then most likely you have a version mis-match between what your WampServer has installed, and the mod_wsgi binary you downloaded.
Hook up Apache to your Python Application
This step will vary depending on what application framework you are using (cherrypy, Django, etc). I'll provide a really basic example to make sure everything is working correctly; this example closely follows the official WSGI documentation.
Create a directory to hold your WSGI application. I created a directory called C:\code\wsgi. In there, create a Python module that implements a function called 'application'. This will be the entry point for your application whenever your application URL is called.
I called my module wsgi.py:
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]
Next, you'll need to update your Apache httpd.conf file to point WSGI at your application. In my use case, I was maintaining a PHP site while doing some prototyping of a new site using Python. To keep the two separate, I defined a virtual server in Apache, listening on a different port number. I also added the IfModule directive, so that if I disable mod_wsgi using the WampServer control panel, then these statements are ignored.
<IfModule wsgi_module>
<VirtualHost *:8090>
WSGIScriptAlias /myapp /code/wsgi/wsgi.py
<Directory /code/wsgi>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
</IfModule>
Test It
Restart Apache using the WampServer control panel. If all is well, you should be able to type
http://localhost:8090/myapp
into your browser, and see the Hello, World! message displayed.
Updated May 2015
Newer releases of the Windows binaries for mod_wsgi are packaged using the whl file format. The whl file is a Python PIP "wheel" file. It's compatible with ZIP, so you can rename the file using a .zip extension to extract the mod_wsgi.so file (from the data directory).
Alternately, you can run 'pip install (packagename).whl' to install mod_wsgi.so as a Python package. You would have to find out where Python extracted the mod_wsgi.so file and copy it to the right place (if necessary).
I used the former approach for the latest version of WAMP Server. The correct file was mod_wsgi-4.4.11+ap24vc10-cp34-none-win32.whl. The 'cpNN' part of the name should match up with the version of Python you have installed.
Solution 2:
You can run python application on localhost. At first, install Python and then you have 2 methods:
Method 1: Native Python Server
1) Open CMD
2) run: cd path/to/project/
3) run: python -m http.server 8080
4) Open http://127.0.0.1/
and you will be in your project ! That's simple
__
Method 2 (worse method, not recommended): Wamp, Xamp, etc..
1) open ...wamp\bin\apache\apacheXXXX\conf\httpd.conf
, then search for Options Indexes FollowSymLinks
and add in the end: ExecCGI
(or Includes ExecCGI
)
2) Find & ensure that LoadModule cgi_module is NOT commented.
3) Search #AddHandler cgi-script .cgi
and remove #
. Then on next line add this:
AddHandler cgi-script .py
4) Find the line:
DirectoryIndex index.php index.php3 index.html index.htm
and add in the end of them: index.cgi index.py
Now, Restart Apache.
5) create a sample.py, with content (just change C:\Python34 path with your installation path):
#!C:\Python34\python\python.exe
print("Content-type: text/html\n\n");
print("helloooooo")
#####---> for python Version 2.x, use: print "helloooooo"
p.s. NOTES: (A) To avoid problems, dont install PYTHON in a path, wherein any "folder name" contains a space. (B) There should not be a space between the start of line and print(..
6) Restart WAMP.
THAT's all!! Open http://localhost/sample.py
!!
p.s.note, in come cases, while using .htaccess [inside .py directory], you might need to insert these lines in .htaccess:
Options +ExecCGI
AddHandler cgi-script .py
Solution 3:
Here is my answer:
- First you need to install python version from the python official website.
- Now install it on yuor hard disk as i installed it in my C drive. It will be installed like (C:/Python27)
-
Now make any python file (for example lets make a file python.py in which we write)
#!C:/Python27/python from os import * from cgi import * print "content-type: text/html\n\n" print "<br><B>hello python</B>"
Save it as python.py in the directory cgi-bin in my case the wamp is installed on my C drive so the file python.py It should be saved in C:\wamp\bin\apache\Apache2.2.11\cgi-bin
Make sure that cgi_module is installed in your apache you can check by clicking on the WAMP icon n the tray menu and look for the tick mark on cgi_module if not tick marked. then do it by left clicking an restart the wampserver
Open your local host and and check for the url (
http://localhost/cgi-bin/python.py
)