When is a module imported from the standard library?

Emile van Sebille emile at fenx.com
Wed Aug 22 08:34:15 EDT 2001


Wouldn't the shortest entry in sys.path ending in lib give this to you?  If
you're concerned that the path may have been tweaked maybe:

 o = os.popen('python -c "import sys;print sys.path"').read()

HTH,

--

Emile van Sebille
emile at fenx.com

---------
"Dinu Gherman" <dinu at reportlab.com> wrote in message
news:3b8394bf.3928488 at news.t-online.de...
> Hi,
>
> I wonder if there is a way to tell if a module is
> part of the standard library or not, when you scan
> over Python source code and handle import statement
> "events"?
>
> Note that I'm not interested in the complexities of
> loading a module named "copy" that sits in the cur-
> rent directory, shadowing the one in the standard
> library, though...
>
> I think all I like to know is if there is a cross-
> platform way to (normally) find out from these
> lines:
>
>   >>> import copy
>   >>> copy.__file__
>   'c:\\programs\\python21\\lib\\copy.pyc'
>   >>> import xmlpp
>   >>> xmlpp.__file__
>   'c:\\users\\gherman\\python\\lib\\xmlpp.py'
>   >>>
>
> that 'c:\\programs\\python21\\lib' is the folder
> in which the standard library is installed?
>
> Of course, this should work without having to im-
> port all or even a large fraction of some known
> standard modules and verify if their __file__
> attribute's directory part is the same...
>
> Ideally, one would not need to import anything, but
> could simply check for an import statement event (on
> the code level) if the module name exists in the fol-
> der of the standard library or not (modulo some sha-
> dowing effects).
>
> Or, to put it differently: how do I find the stan-
> dard library's installation folder without importing
> anything?
>
> Regards,
>
> Dinu
>
> --
> Dinu C. Gherman
> dinu at reportlab dot com
> http://www.reportlab.com
> ................................................................
> "The only possible values [for quality] are 'excellent' and 'in-
> sanely excellent', depending on whether lives are at stake or
> not. Otherwise you don't enjoy your work, you don't work well,
> and the project goes down the drain."
>                     (Kent Beck, "Extreme Programming Explained")




More information about the Python-list mailing list