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