Import name conflicts

Tim Johnson tim at akwebsoft.com
Tue Apr 18 14:28:44 EDT 2017


Using python 2.7~

For testing and edification purposes:

I have a project which has a controllers package at the directory
level just below the root.

>From the top-level (root) of the project, I start my python
interpreter.

The packages is called 'controllers' and has a submodule named 'imp'

I do the following:
>>> a = __import__('imp')
>>> dir(a)
['C_BUILTIN', 'C_EXTENSION', 'IMP_HOOK', 'NullImporter',
'PKG_DIRECTORY', ....]
# object a is the builtin imp module

Now I do 
>>> b = __import__('controllers.imp', fromlist=['controllers'])
>>> dir(b)
['COLORS', 'CONTROLLER', 'DBNAME', 'DBPWD', 'DBUSERNAME', 'ID_COL',
'JOB', 'MYTABLE', 'RECID', 'TABLEFORMAT', ...]

# object b is the imp module in the controllers package.
# I've resolved a potential name collision ...

Now, suppose a python upgrade provides a package called controllers
or there's some great third-party package available called
controllers. 

How do I access the 'global' controllers package?

thanks

-- 
Tim 
http://www.akwebsoft.com, http://www.tj49.com


More information about the Python-list mailing list