472,354 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

package module import name clash with global package

I've run into a strange package related namespace problem.

Follow these steps (on UNIX system) to illustrate:

-------------------------
mkdir /tmp/mypkg
cd /tmp/mypkg
touch __init__.py
echo 'import os\ndef test():\n print os.getcwd()' > os.py
cd /tmp

echo '#\!/usr/bin/env python\nimport mypkg.os\nmypkg.os.test()' >
test_1.py
echo '#\!/usr/bin/env python\nimport os\nprint os.getcwd()' >
test_2.py
chmod a+x test_*.py
-------------------------

This sets up a minimal package mypkg in /tmp that contains a
sub-module named os. It also sets up a test_1.py and test_2.py script
to illustrate the problem. They do equivalent things (print the cwd)
except test_1.py tries and fails to do it from within the mypkg.os
module.

The problem is then that I can't import the global package "os" into
my mypkg.os packaged module. I've done lots of digging around into
this problem and I think I understand the reasons why this happens.

My question is if anyone can think of an alternative way of
fixing/avoiding this?

The best workarounds I have are:
a) Not use os as my submodule name
b) First import global os in test_1.py and then in mypkg/os.py run
the function as sys.modules["os"].__dict__["getcwd"]()

a) is not a perfect solution because, well, in my example I've used
"os" to illustrate the problem, but really, who's to predict what
global modules might be installed on a system? And besides, isn't this
something that namespaces were supposed to solve?

b) well, that's just plain ugly.

Any other ideas?

thanks,
George
Jul 18 '05 #1
3 3679
George P <ge********@yahoo.com> wrote:
...
a) Not use os as my submodule name
Best.
a) is not a perfect solution because, well, in my example I've used
"os" to illustrate the problem, but really, who's to predict what
global modules might be installed on a system? And besides, isn't this


As long as your package does not use global modulenames coinciding with
the ones it contains, you're fine. You don't need to predict anything:
if your package needs global modules os, sys, and re, don't have modules
named that way in your package. Simple, innit?
Alex
Jul 18 '05 #2
al*****@yahoo.com (Alex Martelli) wrote in message news:<1gjx9y3.85025t5mpy3hN%al*****@yahoo.com>...
George P <ge********@yahoo.com> wrote:
...
a) Not use os as my submodule name


Best.
a) is not a perfect solution because, well, in my example I've used
"os" to illustrate the problem, but really, who's to predict what
global modules might be installed on a system? And besides, isn't this


As long as your package does not use global modulenames coinciding with
the ones it contains, you're fine. You don't need to predict anything:
if your package needs global modules os, sys, and re, don't have modules
named that way in your package. Simple, innit?
Alex


Not all that simple. Let's say I give my submodule some fairly generic
name like utils. I would think that was fine because my module is
really mypkg.utils. But then someone out in the rest of the python
universe creates a package called utils and that eventually becomes
standard and gets installed on all systems. Then if I wanted to access
it within my module, I would have a problem.

The problem is easily overcome in my code - all I'd have to do is to
rename my module to myutils or something, but if any other code has
been written that relied on my package and imported mypkg.util, they'd
suddenly stop working with the new release that has the module renamed
to mypkg.myutil.

I'm not saying that these aren't insurmountable problems, I'm just
looking for a better solution. In C++, I would be able to refer to the
global utils in my code as ::utils, and there would be no more
problems. Does anyone know of a similar workaround in python?

George
Jul 18 '05 #3
George P <ge********@yahoo.com> wrote:
...
Not all that simple. Let's say I give my submodule some fairly generic
name like utils. I would think that was fine because my module is
really mypkg.utils. But then someone out in the rest of the python
universe creates a package called utils and that eventually becomes
standard and gets installed on all systems. Then if I wanted to access
it within my module, I would have a problem.
Right: in this case, in the release of your module where you suddenly
want to use a module of the same name from elsewhere, you have to take a
little more care than just adding an 'import utils'. But there's no
guessing involved: it was your "who could guess" that made no sense.

Your module mypkg.utils which wants to import a module named simply
utils must carefully do something like:

import sys, imp

utils = sys.modules.get('utils')
if not utils: utils = imp.load_module('utils',*imp.find_module('utils'))
The problem is easily overcome in my code - all I'd have to do is to
Sure, see above.
rename my module to myutils or something, but if any other code has
been written that relied on my package and imported mypkg.util, they'd
suddenly stop working with the new release that has the module renamed
to mypkg.myutil.
If you change the published interface of your package, yup, you'll break
client code relying on the previous version of the interface. If
everybody used the preferred form "from mypkg import utils" you'd have
no trouble (just add 'import myutils as utils' to mypkg/__init__.py) but
if they use the popular form "import mypkg.utils" you'd break their
code. Which is why, to avoid risking that, I'd suggest the imp solution
sketched above, rather than running the risk of changing your own
published interface.

I'm not saying that these aren't insurmountable problems, I'm just
looking for a better solution. In C++, I would be able to refer to the
global utils in my code as ::utils, and there would be no more
problems. Does anyone know of a similar workaround in python?


Yep, http://www.python.org/peps/pep-0328.html -- but it looks like only
the multiline-import part of PEP 328 is going to make it into Python
2.4, unless I've missed something, not the absolute and relative imports
which are presumably what you're looking for.
Alex

Jul 18 '05 #4

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

Similar topics

5
by: simo | last post by:
I'm writing a module, and it's getting too big to cope with as it has a lot of configuration in it - constants, dictionaries etc; it'd be nice to externalise this and keep my module down to just...
10
by: Steven Reddie | last post by:
Hi, I want to do something like the following, which doesn't work: modulename = 'module' import modulename The error is that there is no module named 'modulename'. Is there a way to get...
1
by: chuck | last post by:
Every once in awhile I run across a python module that might have statements like: for c in sys.modules.__dict__.values(): or import __builtin__ __builtin__.__dict__ = lambda x: x
3
by: Mudcat | last post by:
I have a directory structure that contains different modules that run depending on what the user selects. They are identical in name and structure, but what varies is the content of the functions....
2
by: Keith Jackson | last post by:
Does anybody know of a tool that will take a module as input, look for any wildcard imports, and then identify what symbols in the module come from which wildcard import? It could then expand out...
3
by: Joost | last post by:
Hi guys, I have couple of simple python based active server pages that make use of httplib2 which uses gzip.py. IIS, however, also has a gzip.dll located at the iis/inetsrv path. When using ASP...
1
by: Adam Hupp | last post by:
I've noticed some unexpected behavior with __builtins__ during module import. It seems that during module import __builtins__ is a dict but at all other times it is a module. For example, if...
12
by: Chris Allen | last post by:
Hello fellow pythoneers. I'm stumped on something, and I was hoping maybe someone in here would have an elegant solution to my problem. This is the first time I've played around with packages, so...
2
by: patrimith | last post by:
Hi List, I am used to the following with Java: import some.package.MyClass; name = MyClass.class.getName(); The value for name will be "some.package.MyClass". For Python, I find:
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.