importing / loading a module / class dynamically

Discussion in 'Python' started by hg, Jan 5, 2007.

  1. hg

    hg Guest

    Hi,

    I have the following problem.

    I find in a directory hierarchy some files following a certain sets of
    rules:

    ..../.../../plugin/name1/name1.py
    .....
    ..../.../../plugin/namen/namen.py

    each file will in turn have a class with the same name as the filename
    (minus .py)


    I fetch those names in a list of string and want to import the files /
    instantiate the classes.


    I block at the beginning and tried this (test.py is a real file)
    >>> s = 'test.py'
    >>> eval ('import ' + s)


    and get

    Traceback (most recent call last):
    File "<pyshell#1>", line 1, in -toplevel-
    eval ('import ' + s)
    File "<string>", line 1
    import test.py

    Any clue ?

    Thanks

    hg
     
    hg, Jan 5, 2007
    #1
    1. Advertising

  2. hg

    hg Guest

    hg wrote:

    > Hi,
    >
    > I have the following problem.
    >
    > I find in a directory hierarchy some files following a certain sets of
    > rules:
    >
    > .../.../../plugin/name1/name1.py
    > ....
    > .../.../../plugin/namen/namen.py
    >
    > each file will in turn have a class with the same name as the filename
    > (minus .py)
    >
    >
    > I fetch those names in a list of string and want to import the files /
    > instantiate the classes.
    >
    >
    > I block at the beginning and tried this (test.py is a real file)
    >>>> s = 'test.py'
    >>>> eval ('import ' + s)

    >
    > and get
    >
    > Traceback (most recent call last):
    > File "<pyshell#1>", line 1, in -toplevel-
    > eval ('import ' + s)
    > File "<string>", line 1
    > import test.py
    >
    > Any clue ?
    >
    > Thanks
    >
    > hg



    OK, from http://mail.python.org/pipermail/python-list/2004-July/272081.html,
    I need to use exec and not eval

    hg
     
    hg, Jan 5, 2007
    #2
    1. Advertising

  3. hg

    hg Guest

    Laszlo Nagy wrote:

    >
    >> .../.../../plugin/name1/name1.py
    >> ....
    >> .../.../../plugin/namen/namen.py
    >>
    >>
    >> I block at the beginning and tried this (test.py is a real file)
    >>
    >>>>> s = 'test.py'
    >>>>> eval ('import ' + s)
    >>>>>

    > import test.py # This is invalid
    > import test # This MAY be valid
    > import name1.name1 # Most probably this is what you want if you have the
    > aforementioned directory stucture
    > from name1 import name1 # Or this?
    >
    > You must also:
    >
    > 1. Have the 'plugin' dir in your sys.path
    > 2. Have at least an empty plugin/name1/__init__.py file
    >
    > Another alternative is to have plugins/__init__.py and do something like:
    >
    > from plugins.name1 import name1
    >
    > You should not overcomplicate things anyway. If you do not need these
    > name1...namen directories for sure, then just drop them.
    >
    > Hint: try this (untested)
    >
    > import os
    > fnames = os.listdir('plugins')
    > for fname in fnames:
    > if os.path.isdir(fname):
    > root,ext = os.path.splitext(fname)
    > cmd = "from plugins.%s import %s" % (root,root)
    > print "I should eval this:",cmd
    >
    > Best,
    >
    > Laszlo
    >
    >
    >> Traceback (most recent call last):
    >> File "<pyshell#1>", line 1, in -toplevel-
    >> eval ('import ' + s)
    >> File "<string>", line 1
    >> import test.py
    >>



    Thanks,

    What I am doing is adding plugin support to PyCrust ... so I'm looking for a
    mechanism where anyone can develop a plugin and have it loaded by pycrust.

    the .py was a typo


    why the "...Have at least an empty plugin/name1/__init__.py file..." ?

    Thanks,

    hg
     
    hg, Jan 5, 2007
    #3
  4. hg

    hg Guest

    Laszlo Nagy wrote:

    >
    >> Thanks,
    >>
    >> What I am doing is adding plugin support to PyCrust ... so I'm looking
    >> for a mechanism where anyone can develop a plugin and have it loaded by
    >> pycrust.
    >>
    >> the .py was a typo
    >>
    >>
    >> why the "...Have at least an empty plugin/name1/__init__.py file..." ?
    >>

    > When you do
    >
    > import plugins.name1.name1
    >
    > then "plugins" and "plugins/name1" should be a package, not a module. A
    > package is a special directory that contains package initialization code
    > in a __init__.py file. If you do not have the file, then the "plugins"
    > directory will be only a directory, and it cannot be imported.
    >
    > For details, see:
    >
    > http://docs.python.org/tut/node8.html#SECTION008400000000000000000
    >
    > Laszlo



    Many thanks Laszlo, it looks like I got it to work ... result will be on
    www.snakecard.com/PY for those interested ... in the next few days

    hg
     
    hg, Jan 5, 2007
    #4
  5. hg

    Laszlo Nagy Guest


    > .../.../../plugin/name1/name1.py
    > ....
    > .../.../../plugin/namen/namen.py
    >
    >
    > I block at the beginning and tried this (test.py is a real file)
    >
    >>>> s = 'test.py'
    >>>> eval ('import ' + s)
    >>>>

    import test.py # This is invalid
    import test # This MAY be valid
    import name1.name1 # Most probably this is what you want if you have the
    aforementioned directory stucture
    from name1 import name1 # Or this?

    You must also:

    1. Have the 'plugin' dir in your sys.path
    2. Have at least an empty plugin/name1/__init__.py file

    Another alternative is to have plugins/__init__.py and do something like:

    from plugins.name1 import name1

    You should not overcomplicate things anyway. If you do not need these
    name1...namen directories for sure, then just drop them.

    Hint: try this (untested)

    import os
    fnames = os.listdir('plugins')
    for fname in fnames:
    if os.path.isdir(fname):
    root,ext = os.path.splitext(fname)
    cmd = "from plugins.%s import %s" % (root,root)
    print "I should eval this:",cmd

    Best,

    Laszlo


    > Traceback (most recent call last):
    > File "<pyshell#1>", line 1, in -toplevel-
    > eval ('import ' + s)
    > File "<string>", line 1
    > import test.py
    >
     
    Laszlo Nagy, Jan 5, 2007
    #5
  6. hg

    Laszlo Nagy Guest

    hg írta:
    > hg wrote:
    >
    >
    >> Hi,
    >>
    >> I have the following problem.
    >>
    >> I find in a directory hierarchy some files following a certain sets of
    >> rules:
    >>
    >> .../.../../plugin/name1/name1.py
    >> ....
    >> .../.../../plugin/namen/namen.py
    >>
    >> each file will in turn have a class with the same name as the filename
    >> (minus .py)
    >>
    >>
    >> I fetch those names in a list of string and want to import the files /
    >> instantiate the classes.
    >>
    >>
    >> I block at the beginning and tried this (test.py is a real file)
    >>
    >>>>> s = 'test.py'
    >>>>> eval ('import ' + s)
    >>>>>

    >> and get
    >>
    >> Traceback (most recent call last):
    >> File "<pyshell#1>", line 1, in -toplevel-
    >> eval ('import ' + s)
    >> File "<string>", line 1
    >> import test.py
    >>
    >> Any clue ?
    >>
    >> Thanks
    >>
    >> hg
    >>

    >
    >
    > OK, from http://mail.python.org/pipermail/python-list/2004-July/272081.html,
    > I need to use exec and not eval
    >

    Well, you can also use the 'imp' module. You should read this:

    http://docs.python.org/lib/module-imp.html

    Best,

    Laszlo
     
    Laszlo Nagy, Jan 5, 2007
    #6
  7. hg

    Laszlo Nagy Guest


    > Thanks,
    >
    > What I am doing is adding plugin support to PyCrust ... so I'm looking for a
    > mechanism where anyone can develop a plugin and have it loaded by pycrust.
    >
    > the .py was a typo
    >
    >
    > why the "...Have at least an empty plugin/name1/__init__.py file..." ?
    >

    When you do

    import plugins.name1.name1

    then "plugins" and "plugins/name1" should be a package, not a module. A
    package is a special directory that contains package initialization code
    in a __init__.py file. If you do not have the file, then the "plugins"
    directory will be only a directory, and it cannot be imported.

    For details, see:

    http://docs.python.org/tut/node8.html#SECTION008400000000000000000

    Laszlo
     
    Laszlo Nagy, Jan 5, 2007
    #7
  8. Hi,

    Using exec or eval ISN'T what should be done ever. When you have
    troubles importing you should :

    - Add some repository to your sys.path
    and/or
    - Use the buildin import method
    and/or
    - Use Mr Eby's Importing module (http://python.org/pypi/Importing)

    Regards,

    Laurent

    hg a écrit :
    > Hi,
    >
    > I have the following problem.
    >
    > I find in a directory hierarchy some files following a certain sets of
    > rules:
    >
    > .../.../../plugin/name1/name1.py
    > ....
    > .../.../../plugin/namen/namen.py
    >
    > each file will in turn have a class with the same name as the filename
    > (minus .py)
    >
    >
    > I fetch those names in a list of string and want to import the files /
    > instantiate the classes.
    >
    >
    > I block at the beginning and tried this (test.py is a real file)
    >>>> s = 'test.py'
    >>>> eval ('import ' + s)

    >
    > and get
    >
    > Traceback (most recent call last):
    > File "<pyshell#1>", line 1, in -toplevel-
    > eval ('import ' + s)
    > File "<string>", line 1
    > import test.py
    >
    > Any clue ?
    >
    > Thanks
    >
    > hg
    >
     
    laurent rahuel, Jan 5, 2007
    #8
    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. Thomas Aanensen

    Using class without importing module

    Thomas Aanensen, Feb 10, 2004, in forum: Python
    Replies:
    1
    Views:
    282
    Josiah Carlson
    Feb 10, 2004
  2. plb
    Replies:
    2
    Views:
    372
  3. Pradnyesh Sawant

    doubt with importing module, given module name

    Pradnyesh Sawant, Apr 9, 2007, in forum: Python
    Replies:
    0
    Views:
    303
    Pradnyesh Sawant
    Apr 9, 2007
  4. -ecosys.com
    Replies:
    4
    Views:
    324
    Calvin Spealman
    Sep 28, 2007
  5. Replies:
    4
    Views:
    336
Loading...

Share This Page