[Tutor] MySQL and Python

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 29 Oct 2001 12:13:11 -0800 (PST)

On Mon, 29 Oct 2001, Jackson wrote:

> Can the MySQLdb module be installed into the cgi-bin dir, and called
> with import /path_to_cgi-bin/MySQLdb ?

We can do something similar to what you're thinking: we can append() the
cgi-bin path to the sys.path variable.  Once you do this, subsequent
imports will also look the cgi-bin directory for modules.

Here's an example: Let's say that I have the following files in a personal
directory, like "/home/dyoo/src/python":

[dyoo@tesuque python]$ ls /home/dyoo/src/python
dannytest.py  test.py

We can import these modules if we add them to the sys.path:

>>> import sys     
>>> print sys.path
['', '/opt/Python-2.1.1/lib/python2.1',
>>> import dannytest
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named dannytest
>>> sys.path.append("/home/dyoo/src/python")
>>> import dannytest
>>> print dannytest.__file__
>>> import test 
>>> print test.__file__

Here, we can see the list of directories that Python scans when we ask it
to 'import' things.

Note, though, that Python scans the sys.path in order, so if there's a
module that's named the same in the standard Python library, that's the
one Python will use.  That's what's happening above with the "test"
module, so be careful.

Hope this helps!