Page 1 of 1

[How-To] Running Python through/with XAMPP

PostPosted: 10. April 2009 07:23
by Kairu
Please note: If anything in this How-To is unclear, doesn't seem to work, or have a suggestion on what could be done better, let me know with as much detail as possible.
Also note: At the time of this writing, I'm using XAMPP 1.6.6a, and the recommended build of Python (version 2.5.*).

This How-To will enable you to run Python files through your XAMPP installation, on a USB-Drive. (It's a partially-portable version).

Step 1: Download Python
If you haven't already done so, go to the Python website (http://python.org) and download (preferably) the recommended build of Python (2.5.* at the time of this writing). Be sure to download the .msi installer, not the source code itself (which comes in ".tgz" and ".tar.bz2" archives).

Step 2: Install Python
Once you've downloaded the .msi installer, run it. Just go through the installation process (install it to wherever it recommends you to; usually it'll be something along the lines "C:\python25\"). After the installation has finished, navigate to where Python was installed. Copy the folder/directory from the installation over to the folder/directory where XAMPP is located or the root-of-the-drive (For example copy 'C:\python25\' over to 'E:\XAMPP\' where 'E:' would be your USB-Drive).

Step 3: Configure Apache
In the directory/folder where XAMPP is located, navigate to your httpd.conf file (usually located in /apache/conf/) and open it with a text-editing program. Scroll to the bottom, add the following code, and save it.
Code: Select all
#
# For Python
#
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict


Step 4: Tweaking
Uninstallation is optional - Assuming you copied the Python Binaries into your USB-Drive and didn't cut/move them, then unistall the Python installation that you ran earlier in this How-To. The files you copied won't be affected by this unistallation process, only the files that were actually created by the .msi installer will be removed. I unistalled Python from Windows so it would be easier to tell if the Python-Binaries on my USB-Drive could run with Python being installed on the Host-Computer.

Next, add the following code into a text-file (.txt), save the file, and change the extension to .reg. I called this file modpy1.reg, but you can name it whatever you want (be sure it has .reg extension!). Also, make sure to edit the this so it matches the location of where your Python directory is.
Code: Select all
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Python.File]
@="Python File"

[HKEY_CLASSES_ROOT\Python.File\DefaultIcon]
@="C:\\Python25\\DLLs\\py.ico"

[HKEY_CLASSES_ROOT\Python.File\shell]

[HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE]

[HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command]
@="\"C:\\Python25\\pythonw.exe\" \"C:\\Python25\\Lib\\idlelib\\idle.pyw\" -n -e \"%1\""

[HKEY_CLASSES_ROOT\Python.File\shell\ExecCGI]

[HKEY_CLASSES_ROOT\Python.File\shell\ExecCGI\command]
@="\"E:\\Python\\python.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\Python.File\shell\open]

[HKEY_CLASSES_ROOT\Python.File\shell\open\command]
@="\"C:\\Python25\\python.exe\" \"%1\" %*"

[HKEY_CLASSES_ROOT\.py]
@="Python.File"
"Content Type"="text/plain"


This code is the path to the Python folder/directory that you copied onto your USB-Drive. So, wherever you placed that folder/directory, just replace my path (as is shown in the provided code) with the path pointing to your Python folder/directory. Add the following code into another text-file, save the file, and change the extension to .reg (exactly as you did with the code above). I called this file modpy2.reg but once again, you can call it whatever you want. Make sure 'python.exe' is included in the path and that you escape the back-slashes in the path, as you see is done in the code provided.
Code: Select all
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Python.File\shell\ExecCGI\command]
@="\"E:\\Python\\python.exe\" \"%1\" %*"


Note: You actually don't have to make two separate files, you can combine both bits of code together into one file. I separated them for convenience reasons. You'll have to edit the drive-letter (that appears in the second code-snippet) if you're using your USB-Drive on a computer that assigns it a different drive-letter than what it was before.

Step 5: Test it
Now that you've got everything done, it should work flawlessly. Here's what you should've done up to this point:
Code: Select all
1.0 Downloaded a recommended build of Python as a .msi installer
2.0 Installed Python onto your computer using the .msi installer
3.0 Copied the folder that the .msi installer created onto your USB-Drive
4.0 Configured your httpd.conf file to allow apache to handle Python-files as CGI-scripts.
5.0 (Optional) Uninstalled the Python installation you ran earlier
5.1 Created the two registry-files (.reg files). (Or just one file. I only made two for convenience reasons).

So if you've done all of what was just mentioned above, then restart XAMPP, and point your browser to a .py-file that's inside your htdocs folder/directory. If you don't have a python-script to test, just use this (extremely simple) one. Just create a new text-file, paste the following code into it, and change the extension to .py.
Code: Select all
#!/usr/bin/python
import cgitb
cgitb.enable()
print "Content-Type: text/html"
print ""
print "If you can read this inside a Web-Browser window, you can successfully run Python through XAMPP!'"


If the file executed successfully, all you should see is, "If you can read this inside a Web-Browser window, you can successfully run Python through XAMPP!" If you got an error, or it blatantly displays the code while trying to view it inside a Web-Browser, then something went wrong along the way. While I can't say everything you could've done wrong, I can suggest checking your script paths (making sure the path you provided match up with where they actually are on your USB-Drive).

Le Fin; Dekimashita; The End!

P.S. Just a note: Remember to change the drive-letter of the path in the .reg file(s) you made. You only have to do this for the second snippet I provided for the .reg files. You won't (or shouldn't, atleast) have to edit anything in the first snippet for the .reg files. For example, you set this up on your computer (which it assigns your USB-Drive the letter of 'E:'). You then take that USB-Drive to another computer that assigns your USB-Drive to be 'J:'. Well now you have to edit the drive-letter in the script-path of the second .reg file to say 'J:' instead of 'E:'

ANNOTATION 1
I forgot to mention one other thing you must do: you must set a system variable for Python to run correctly (so you don't get a "500 Server Error"). To do this, Open My Computer, right-click and select Properties, select the tab titled Advanced, select Environment Variables (which is a button) at the bottom, select Path in the System Variables box, select Edit, and add the path to your python directory at the end of the Variable Value line[/u]. Ex: For the sake of this tutorial, I would add ";C:\Python25" to the end of the line (the semi-colon ";" acts as a separator between system-variables).

Feel free to contact me (for anything regarding this How-To): member.realm@gmail.com

Re: Running Python through XAMPP

PostPosted: 10. April 2009 07:50
by Sharley
You may find some clues in these forum search results even though they are old.

Not sure if using an .msi version is the way to install to a USB drive because if a drive letter is nominated and a registry entry is created then your USB becomes unportable - best use a zip archive if one is available then extract to a folder in the xampp tree of your USB.

Re: Running Python through XAMPP

PostPosted: 10. April 2009 08:07
by Kairu
I found a GZip-File, so hopefully, it'll work. Excuse my ignorance; but what exactly would I do after I placed the files into the XAMPP directory?

Re: Running Python through XAMPP

PostPosted: 11. April 2009 00:13
by Kairu
Does anyone know what to do? (I'm still at a road-block with this.)

Re: Running Python through XAMPP

PostPosted: 11. April 2009 00:28
by Sharley
Kairu wrote:Does anyone know what to do? (I'm still at a road-block with this.)
What do you want to do?

Why did you want to install Python in the first place?

Did you scan the search results I provided above?

Python is a programming language, have you read the documentation on the Python web site?
http://docs.python.org/tutorial/

These Google search results may help with the log jam.

Re: Running Python through XAMPP

PostPosted: 11. April 2009 03:28
by Kairu
Now I feel ignorant - I completely overlooked the fact that you provided search-results. :oops:

What I want to do is install Python in the same fashion that the other components of XAMPP are installed. I want to keep it relatively portable (I keep XAMPP on a USB-Drive as I'm usually using it on several different computers). I'm installing Python because I want to start using it as my Programming Language-of-choice (I currently use PHP, which dissatisfies me) and I noticed that XAMPP doesn't support it natively.

I did find a recent version of Python (version 2.4) for XAMPP in the form of a XAMPP-Addon, but there seem to be quite a few problems with it. (It seems it was originally meant for LAMPP but no-one bothered to change the paths or anything like that).

Edit...
Nevermind, I have Python successfully installed.

Re: Running Python through XAMPP

PostPosted: 11. April 2009 05:38
by Sharley
Kairu wrote:Nevermind, I have Python successfully installed.
Would you mind telling others reading this topic and looking for a solution, how you eventually managed to successfully install Python?

Re: Running Python through XAMPP

PostPosted: 11. April 2009 06:02
by Kairu
I was just working on that actually; I'll add it to the original post.

Re: [How-To] Running Python through/with XAMPP

PostPosted: 11. April 2009 08:41
by Sharley
Thank you for the comprehensive and detailed How-To, I am sure it will be most helpful for others who want to use Python and have searched the forum for this topic.

Well done!