473,494 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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

Jul 18 '05 #1
2 1273
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: ga*****@geochemsource.com

Python forever!
Jul 18 '05 #2
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5191
by: Felix Wiemann | last post by:
Hi! I want to create a module myproject.ui.curses, which needs to import the curses library. However, if I just write ``import curses``, the module imports *itself* instead of the standard...
5
7693
by: sophie_newbie | last post by:
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do...
10
3740
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
4
2183
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
40
3427
by: rjcarr | last post by:
Sorry if this is a completely newbie question ... I was trying to get information about the logging.handlers module, so I imported logging, and tried dir(logging.handlers), but got: ...
4
1648
by: Peter J. Bismuti | last post by:
I'm having trouble understanding how namespaces work in modules. I want to execute a module within the interpreter and then have values that are calculated persist so that other modules that get...
3
1407
by: Peter J. Bismuti | last post by:
How do you define a "module data member" (I want to understand out how this works before making converting to a Class)? Right now I'm defining variables in a module that get put into the global...
3
1622
by: Matias Surdi | last post by:
Hi, Suppose I've a module named "urllib" and from it I need to import the urllib module from the python standart library. ¿how can I do this? The problem I found is that when I do:
1
1672
by: Joe Strout | last post by:
I've been using docstring to exercise each of my modules, with code like this: def _test(): import doctest doctest.testmod() if __name__ == "__main__": _test()
0
7119
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7367
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.