How to keep a module with the same name as a module it is importing from importing itself?

Discussion in 'Python' started by plb, Feb 8, 2005.

  1. plb

    plb Guest

    All:

    I am struggling with an import problem...

    In my package, myapp, I have a module, logging.py. That module,
    naturally, imports the library module logging with an 'import logging'
    statement. However, when I use 'import myapp.logging' in my script,
    the myapp.logging module tries to import itself rather than the library
    logging module.

    How can I prevent this from happening other than using a name that
    doesn't conflict?

    --PLB
    plb, Feb 8, 2005
    #1
    1. Advertising

  2. Re: How to keep a module with the same name as a module it isimporting from importing itself?


    >In my package, myapp, I have a module, logging.py. That module,
    >naturally, imports the library module logging with an 'import logging'
    >statement. However, when I use 'import myapp.logging' in my script,
    >the myapp.logging module tries to import itself rather than the library
    >logging module.
    >
    >How can I prevent this from happening other than using a name that
    >doesn't conflict?
    >
    >


    Did you try the built-in 'imp' module?

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

    I believe that 'load_module' is your very best friend. :)

    Another idea: rename your modules so they do not conflict with standard
    modules.
    Best,

    Laci 2.0


    --
    _________________________________________________________________
    Laszlo Nagy web: http://designasign.biz
    IT Consultant mail:

    Python forever!
    Laszlo Zsolt Nagy, Feb 8, 2005
    #2
    1. Advertising

  3. plb

    plb Guest

    Good call! The following snippet solved my problems portably.

    path = sys.path[1:]
    file, filename, description = imp.find_module('logging', path)
    logging = imp.load_module('logging', file, filename, description)

    Thanks!

    --Peter
    plb, Feb 8, 2005
    #3
    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. plb
    Replies:
    0
    Views:
    371
  2. Francisco Borges

    importing two modules with the same name

    Francisco Borges, Mar 19, 2005, in forum: Python
    Replies:
    5
    Views:
    311
    Francisco Borges
    Mar 20, 2005
  3. Pradnyesh Sawant

    doubt with importing module, given module name

    Pradnyesh Sawant, Apr 9, 2007, in forum: Python
    Replies:
    0
    Views:
    271
    Pradnyesh Sawant
    Apr 9, 2007
  4. Jeff Kish
    Replies:
    4
    Views:
    834
    Martin Honnen
    Oct 30, 2008
  5. hisan
    Replies:
    1
    Views:
    1,296
    Dan Stromberg
    Jun 25, 2012
Loading...

Share This Page