"dynamical" importing

Discussion in 'Python' started by Joerg Schuster, Oct 19, 2005.

  1. Hello,

    I need to import modules from user defined paths. I.e. I want to do
    something
    like:

    module_dir = sys.argv[1]

    my_path = os.path.join(module_dir, 'bin', 'my_module')

    from my_path import my_object

    Obviously, it doesn't work this way. How would it work?

    Jörg Schuster
     
    Joerg Schuster, Oct 19, 2005
    #1
    1. Advertising

  2. Hi,

    I guess you need to look at __import__

    Regards,

    Laurent.

    Joerg Schuster wrote:

    > Hello,
    >
    > I need to import modules from user defined paths. I.e. I want to do
    > something
    > like:
    >
    > module_dir = sys.argv[1]
    >
    > my_path = os.path.join(module_dir, 'bin', 'my_module')
    >
    > from my_path import my_object
    >
    > Obviously, it doesn't work this way. How would it work?
    >
    > Jörg Schuster
     
    Laurent Rahuel, Oct 19, 2005
    #2
    1. Advertising

  3. Joerg Schuster wrote:

    > I need to import modules from user defined paths. I.e. I want to do
    > something like:
    >
    > module_dir = sys.argv[1]
    >
    > my_path = os.path.join(module_dir, 'bin', 'my_module')
    >
    > from my_path import my_object
    >
    > Obviously, it doesn't work this way. How would it work?


    some alternatives:

    - if you want the modules to remain imported:

    try:
    sys.path.insert(0, os.path.join(module_dir, "bin"))
    module = __import__("my_module")
    finally:
    del sys.path[0]
    object = module.my_object

    - if you're only interested in the object:

    namespace = {}
    execfile(os.path.join(module_dir, "bin", "my_module" + ".py"), namespace)
    object = namespace["my_object"]

    </F>
     
    Fredrik Lundh, Oct 19, 2005
    #3
  4. Joerg Schuster

    John Abel Guest

    Try:

    userModule = _importModule( pathToModule )

    def _importModule( moduleName ):
    modName = __import__ ( moduleName )
    modComponents = moduleName.split( '.' )
    for indivComp in modComponents[ 1: ]:
    modName = getattr( modName, indivComp )

    return modName

    HTH,

    J

    Joerg Schuster wrote:

    >Hello,
    >
    >I need to import modules from user defined paths. I.e. I want to do
    >something
    >like:
    >
    >module_dir = sys.argv[1]
    >
    >my_path = os.path.join(module_dir, 'bin', 'my_module')
    >
    >from my_path import my_object
    >
    >Obviously, it doesn't work this way. How would it work?
    >
    >Jörg Schuster
    >
    >
    >
     
    John Abel, Oct 19, 2005
    #4
  5. John Abel wrote:

    > def _importModule( moduleName ):
    > modName = __import__ ( moduleName )
    > modComponents = moduleName.split( '.' )
    > for indivComp in modComponents[ 1: ]:
    > modName = getattr( modName, indivComp )
    >
    > return modName


    __import__ takes a module name, not an arbitrary file name.

    </F>
     
    Fredrik Lundh, Oct 19, 2005
    #5
  6. Joerg Schuster

    John Abel Guest

    Fredrik Lundh wrote:

    >John Abel wrote:
    >
    >
    >
    >>def _importModule( moduleName ):
    >> modName = __import__ ( moduleName )
    >> modComponents = moduleName.split( '.' )
    >> for indivComp in modComponents[ 1: ]:
    >> modName = getattr( modName, indivComp )
    >>
    >> return modName
    >>
    >>

    >
    >__import__ takes a module name, not an arbitrary file name.
    >
    ></F>
    >
    >
    >
    >
    >

    Didn't mean to imply that it did. By pathToModule, I meant spam.ham as
    in
    http://localhost/documentation/Python-Docs-2.4.1/lib/built-in-funcs.html#l2h-6
    ( hopefully, that should explain things to the OP ).

    J
     
    John Abel, Oct 19, 2005
    #6
  7. Thanks a lot to all.
     
    Joerg Schuster, Oct 19, 2005
    #7
    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. Liqun Xu

    Click-Event by dynamical Button

    Liqun Xu, Dec 5, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    385
    Munsifali Rashid
    Dec 5, 2003
  2. MARTIN Herve \(EXT\)

    Dynamical update of the CLASSPATH

    MARTIN Herve \(EXT\), Nov 5, 2003, in forum: Java
    Replies:
    4
    Views:
    353
    MARTIN Herve \(EXT\)
    Nov 6, 2003
  3. blabla

    dynamical dimensions

    blabla, May 26, 2004, in forum: Java
    Replies:
    4
    Views:
    372
    blabla
    Jun 14, 2004
  4. Michael Schrading

    Fonts in dynamical build html

    Michael Schrading, Feb 9, 2004, in forum: HTML
    Replies:
    4
    Views:
    416
  5. FraterQ
    Replies:
    4
    Views:
    835
    Martin Ambuhl
    Jul 3, 2003
Loading...

Share This Page