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

P

plb

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
 
L

Laszlo Zsolt Nagy

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: (e-mail address removed)

Python forever!
 
P

plb

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top