简体   繁体   中英

Change which python installation my apache local server is using

I have two python installations, one at /usr/bin/env and one through Enthought at /Library/Enthought/User/bin/python. The enthought installation has a library that I would like to use in a website that I've inherited from a former coworker.

I set up a local server through apache on my Mac OSX 10.10.5 (Yosemite). I configured httpd.conf and my user configuration file inside of the website folder to execute cgi and py files.

However, when I try to run my coworker's python scripts through the server, it seems to use the installation without the package I need (/usr/bin/env). I tried changing the shebang at the top of the script from:

'#!/usr/bin/env python' to '#!/Library/Enthought/User/bin/ python'

but that gives me an internal service error (500).

One thing I was able to do was to create a symlink using this command: 'sudo ln -s /Library/Enthought/User/bin/python /usr/bin/python'

Now his script can run and has no trouble finding the right python package. However, I'd like to understand why changing the shebang isn't enough. Why does the server still access the /usr/bin/env python distribution?

A subquestion: Lastly, I created my own test script in the meantime, but I get an Internal Service Error (500) no matter what the shebang is. This script is in the same folder as his script. Why can't this script run?:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cgi
import cgitb; cgitb.enable()  # for troubleshooting
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
print "Hello."
print "</body></html>"

Thanks! I hope this question was clear. This is my first experience with web servers, so please correct me if I made any terminology mistakes!

The shebang life you quote doesn't work because of the space at the end. /usr/bin/env is a program, and python is the argument, the name of the program it's supposed to look for. Remove the space from your new shebang and you will be running the Python interpreter directly. There's no need to provide it with an argument.

As to why the other script isn't working, I can only guess that you haven't made it executable, since I can't find any other reason for its failure (but this is a guess).

Your second print statement does not have an argument. Try changing it from print to print "\\n" .

EDIT

Try the following script with the appropriate shebang line prepended and, as holdenweb says, make sure it has execute permissions for the right user. Another issue I have run into in the past is with line terminations. If you have the "od" utility, run " od -c <filename> " on the command line to do an octal dump for your new file and the existing, working file. That way, you can see if the files end with the same line termination("\\r\\n" or "\\n").

import cgi
print "HTTP/1.0 Status: 200 OK"
print "Content-type: text/html"
print ""
print "<html><head>"
print "</head><body>"
print "Hello."
print "</body></html>"

Note the addition of print "HTTP/1.0 Status: 200 OK" . Other than that...I am out of suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM