module path?

Discussion in 'Python' started by akonsu, Sep 29, 2009.

  1. akonsu

    akonsu Guest

    hello,

    is there a way to determine the file location of a loaded module?
    assuming it is not built in.

    import settings
    print settings

    produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>

    i would like to get to this path somehow other than by parsing the
    string representation of the module. is there a property on
    types.ModuleType? i could not find anything...

    thanks
    konstantin
     
    akonsu, Sep 29, 2009
    #1
    1. Advertising

  2. On Mon, 28 Sep 2009 16:41:36 -0700, akonsu wrote:

    > hello,
    >
    > is there a way to determine the file location of a loaded module?
    > assuming it is not built in.
    >
    > import settings
    > print settings
    >
    > produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>
    >
    > i would like to get to this path somehow other than by parsing the
    > string representation of the module. is there a property on
    > types.ModuleType? i could not find anything...


    Did you look at dir(settings) for a list of method and attribute names?

    Look at module.__file__. But be careful because some modules are "built
    in", that is, they don't actually exist as a separate file and are part
    of the Python compiler. E.g.:

    >>> import sys
    >>> sys.__file__

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    AttributeError: 'module' object has no attribute '__file__'
    >>> sys

    <module 'sys' (built-in)>



    --
    Steven
     
    Steven D'Aprano, Sep 29, 2009
    #2
    1. Advertising

  3. akonsu

    akonsu Guest

    On Sep 28, 7:51 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Mon, 28 Sep 2009 16:41:36 -0700, akonsu wrote:
    > > hello,

    >
    > > is there a way to determine the file location of a loaded module?
    > > assuming it is not built in.

    >
    > > import settings
    > > print settings

    >
    > > produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>

    >
    > > i would like to get to this path somehow other than by parsing the
    > > string representation of the module. is there a property on
    > > types.ModuleType? i could not find anything...

    >
    > Did you look at dir(settings) for a list of method and attribute names?
    >
    > Look at module.__file__. But be careful because some modules are "built
    > in", that is, they don't actually exist as a separate file and are part
    > of the Python compiler. E.g.:
    >
    > >>> import sys
    > >>> sys.__file__

    >
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    > AttributeError: 'module' object has no attribute '__file__'>>> sys
    >
    > <module 'sys' (built-in)>
    >
    > --
    > Steven


    thanks! i did not know about dir() method.
    konstantin
     
    akonsu, Sep 29, 2009
    #3
  4. akonsu

    Dave Angel Guest

    akonsu wrote:
    > hello,
    >
    > is there a way to determine the file location of a loaded module?
    > assuming it is not built in.
    >
    > import settings
    > print settings
    >
    > produces: <module 'settings' from 'd:\ko\mysite\settings.pyc'>
    >
    > i would like to get to this path somehow other than by parsing the
    > string representation of the module. is there a property on
    > types.ModuleType? i could not find anything...
    >
    > thanks
    > konstantin
    >
    >

    The property is called __file__

    So in this case, filename = settings.__file__
     
    Dave Angel, Sep 29, 2009
    #4
  5. akonsu

    Aahz Guest

    In article <>,
    akonsu <> wrote:
    >
    >thanks! i did not know about dir() method.


    dir() is a function, not a method.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "....Normal is what cuts off your sixth finger and your tail..." --Siobhan
     
    Aahz, Oct 6, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    2,027
    Peter O'Reilly
    May 3, 2004
  2. Replies:
    0
    Views:
    2,301
  3. Mupota Muchelemba
    Replies:
    1
    Views:
    951
    Tony Morris
    Feb 4, 2004
  4. Ron Adam
    Replies:
    3
    Views:
    465
    Ron Adam
    Aug 2, 2005
  5. Maric Michaud
    Replies:
    0
    Views:
    7,222
    Maric Michaud
    Jun 24, 2006
Loading...

Share This Page