When is a module imported from the standard library?

Ignacio Vazquez-Abrams ignacio at openservices.net
Wed Aug 22 15:59:15 EDT 2001


On Wed, 22 Aug 2001, Jeff Shannon wrote:

>
> Ignacio Vazquez-Abrams wrote:
>
> > #! /usr/bin/python
> >
> > import os
> > import string
> >
> > print os.path.dirname(string.split(`os`, "'")[3])
> > ---
> >
> > I tested it under 1.5.2. Does it also work under 2.x?
>
> It seems to (though I used `os`.split() instead of importing
> string)--I'm using 2.0
>
> However, I would be worried about this being somewhat fragile, as it's
> depending on the specific format of a module's repr()--it seems to be
> constant across versions, but I'd hate to presume that it will stay so.
>
> Jeff Shannon
> Technician/Programmer
> Credit International

I found one that might be a little more portable:

---
#! /ur/bin/python

import os
import imp

print os.path.dirname(imp.find_module('os')[1])
---

It removes the dependency on repr(), but requires that the imp module hasn't
been sillily masked (I doubt that masking os would happen TOO often...).

-- 
Ignacio Vazquez-Abrams  <ignacio at openservices.net>





More information about the Python-list mailing list