Use Python on MAMP
Asked Answered
D

3

6

I'm slowly migrating from PHP to Python. In particular, as I work in webdev/webdesign I would like to display a basic HTML page using Python, using the following code:

#!/usr/bin/python
   
print('<html><head></head><body>This is a test</body></html>')

Sending the file online on my host as index.cgi, I've had no problem displaying the content of the file.

The problems start when I try to install the WSGI module on MAMP, or just to make Python work in general with it.

When it go to localhost/index.cgi the content of the file is displayed instead of its results.

I've followed half a dozen tutorials and none seems to work, I always encounter a problem at one point or another. It seems to come from the fact that Apache that comes with MAMP isn't built in a way that lets you add modules to it (such as wsgi).

This is also comes from the fact that I can't find any recent article on how to install Python on MAMP, they all either date from 2008 or 2009, with old versions of MAMP, Python and Macports.

Can somebody points me to the current procedure to make this work ?

EDIT : Ok after finding this article I gathered that MAMP by default don't process CGI scripts outside of the cgi-bin/ folder in MAMP/. So I modified the Apache conf file as explained, it now apparently reads the .cgi file but throws an error 500 with the content shown above. Is the code the culprit or is it MAMP's ?

Dessiatine answered 2/7, 2012 at 15:9 Comment(1)
it's a 404 for the link you found.Endoenzyme
D
2

Got it to work, the problem were the missing CGI interpretation of MAMP outside of the cgi-bin/ folder (see original post) and the missing headers :

print 'Content-type: text/html\n\n'

Dessiatine answered 2/7, 2012 at 16:4 Comment(0)
J
2

I've just gone through this process on OSX Catalina with Mamp V5.5

For me I had to follow the following steps:

  1. Make sure your file has the first line:
#!/usr/bin/python

or a path to any valid Python installation or environment. Make sure your python is working correctly.

  1. The file must have the extension cgi e.g.
blah.cgi  (not .py)
  

Then it will work from any folder.

  1. The file must have execute permissions. In terminal:
chmod 755 blah.cgi
  1. The file must send a content type near the beginning ( no brackets for Python versions < 3 ):
print('Content-type: text/html \n\n')
  1. An additional step I would recommend is adding this at the beginning of your page:
import sys
sys.stderr = open("err.log",'w')

Which will route all your error messages to the file err.log in the same directory which is insanely useful for debugging. If your page comes back with 500 Internal Server Error, you should see some errors in err.log file (unless the problem was in initial imports before this statement).

There are other config changes you can make to keep the .py extension but I won't go into that here.

Jonson answered 23/8, 2020 at 0:45 Comment(0)
U
1

This is just standard CGI, nothing special here, no need for WSGI. You do need to install Python. You can install it wherever you like, as long as your script can find it. You see the line:

#! /usr/bin/python

that is where the script will try to find Python, so change it to your Python installation, or fix your Python installation to be there.

Untaught answered 2/7, 2012 at 15:20 Comment(2)
Well my Python installation is there, when I type which python in Terminal it indeed tells me /usr/bin/python but nonetheless when I visit the .cgi file, be it in localhost/ or file:// I only get the file's content. This is weird.Dessiatine
@MaximeFabre Look for Python in your MAMP installation directory. (Mine's /Applications/MAMP/bin/apache2/bin/python, which is an alias for /Applications/MAMP/Library/bin/python2.7)Puling

© 2022 - 2024 — McMap. All rights reserved.