Python module imports & paths

Status
Not open for further replies.

GohanIYIan

Ars Legatus Legionis
10,196
My hosting provider somehow messed up their python config and broke my website. I have SSH access but not root access. I'm looking for ideas on what might be going wrong.

The problem, in a nutshell, is this line works when running python from the command line but fails when run as CGI:
Code:
import MySQLdb
The error is "No module named MySQLdb"

So far I've been able to ascertain:
  • sys.path on the command line includes the MySQLdb egg, sys.path on CGI does not
  • manually updating the CGI sys.path at runtime to match the CLI sys.path does not help
  • platform.python_build reports different dates, although version and compiler are the same
  • the CGI python script can see the egg file, per os.path

My initial thought was something chroot related, but the file visibility seems to rule that out.
 

Alamout

Ars Legatus Legionis
27,285
manually updating the CGI sys.path at runtime to match the CLI sys.path does not help
platform.python_build reports different dates, although version and compiler are the same
These bits are the red flags, I think. Are you sure that CGI is using the same Python executable that you're using from the CLI? e.g. Is sys.executable the same?

What is the date for the CGI version, maybe they built it themselves and did something weird?
 

GohanIYIan

Ars Legatus Legionis
10,196
Alamout":172svstf said:
manually updating the CGI sys.path at runtime to match the CLI sys.path does not help
platform.python_build reports different dates, although version and compiler are the same
These bits are the red flags, I think. Are you sure that CGI is using the same Python executable that you're using from the CLI? e.g. Is sys.executable the same?

What is the date for the CGI version, maybe they built it themselves and did something weird?

Both return /usr/bin/python for sys.executable. With the different build dates, I figured that meant a chroot environment for CGI.

CGI says python_build is "Sep 3 2009 15:37:37". CLI says "Jun 18 2012 08:55:23". Both report 2.4.3 for version and "GCC 4.1.2 20080704 (Red Hat 4.1.2-52)" for compiler.

EDIT:

LordHunter's answer seems close. If I append the egg and it's directory to sys.path (/usr/lib64/python2.4/site-packages), I at least get a new error: No module named pkg_resources
 

GohanIYIan

Ars Legatus Legionis
10,196
Correct. Adding the setuptools egg yields no progress.

I still can't tell for sure if there's one python installation or two. CGI python sys.path references Numeric and has the same behavior in reverse. CGI can import Numeric, CLI cannot.

EDIT: the setuptools egg doesn't exist per CGI os.path.exists(). I think there are two separate environments, the mysql egg just coincidentally has the same filename in both.
 

Alamout

Ars Legatus Legionis
27,285
Both return /usr/bin/python for sys.executable. With the different build dates, I figured that meant a chroot environment for CGI.
Yeah, that's weird. I didn't realize sys.executable would be that opaque. There might be some gross way to follow the link and figure out where it actually is--maybe try to execute "readlink /usr/bin/python" from within the CGI script, using subprocess?
 

LordHunter317

Ars Legatus Legionis
22,394
Subscriptor
Alamout":2hpyvy8r said:
There might be some gross way to follow the link and figure out where it actually is--maybe try to execute "readlink /usr/bin/python" from within the CGI script, using subprocess?
I don't see how this is going to be informative.

It might be worth seeing if /usr/bin/python is a script instead of a real executable. If it's the latter, you're almost certainly dealing with a chroot.
 

GohanIYIan

Ars Legatus Legionis
10,196
Alamout":1wj4etx9 said:
Both return /usr/bin/python for sys.executable. With the different build dates, I figured that meant a chroot environment for CGI.
Yeah, that's weird. I didn't realize sys.executable would be that opaque. There might be some gross way to follow the link and figure out where it actually is--maybe try to execute "readlink /usr/bin/python" from within the CGI script, using subprocess?

The built in os.readlink seems simpler.

The whole link chain appears to be: /usr/bin/python => /bin/python => /bin/python2.4 => /chroot/python2.4/bin/python2.4

Support did finally fix the problem though.
 
Status
Not open for further replies.