Import name conflicts

Tim Johnson tim at akwebsoft.com
Tue Apr 18 19:59:20 EDT 2017


* Ben Finney <ben+python at benfinney.id.au> [170418 14:58]:
> Tim Johnson <tim at akwebsoft.com> writes:
> 
> > Using python 2.7~
> 
> In Python 2, you should turn on the “default import is absolute” by
> issuing the statement::
> 
>     from __future__ import absolute_import
> 
> as one of the first statements in each module.
> 
> When you migrate your code to PYthon 3, that will be the default
> behaviour.
> 
> > The packages is called 'controllers' and has a submodule named 'imp'
> 
> To avoid the module named ‘imp’ in the standard library, your code
> should import ‘imp’ using a relative import. See PEP 328
> <URL:https://www.python.org/dev/peps/pep-0328/#rationale-for-relative-imports>.
> 
> So, you'd do::
> 
>     from . import imp
> 
> to import the ‘imp’ module relative to the current package.
> 
> > I do the following:
> > >>> a = __import__('imp')
> 
> It is best to avoid calling dunder functions directly; they are
> available for overriding the internals of language features, and
> *invoking* one is best done by just using the corresponding language
> feature.
> 
> Instead, use either the ‘import’ statement, or the ‘importlib’ standard
> library module <URL:https://docs.python.org/2/library/importlib.html>,
> specifically the ‘importlib.import_module’ function::
> 
>     import importlib
> 
>     a = importlib.import_module('.imp')
> 
> > Now, suppose a python upgrade provides a package called controllers or
> > there's some great third-party package available called controllers.
> 
> You don't even have to imagine that far: there is already a standard
> library module named ‘imp’, so you already have that problem to contend
> with :-)
> 
> That is the distinction that led to “absolute” import versus “relative”
> import.
> 
> > How do I access the 'global' controllers package?
> 
> * Enable “default import is absolute”, as described above.
> 
> * Issue a normal import statement: the absolute import search path will
>   be used.
> 
> I hope that helps.
  Lots of help Ben ...
  Beware, I started my career coding in ASM, then C, then python, so
  I'm prone to get under the hood and start playing with the wires.

  I made a simple test module called 'controllers' in another
  directory, used __import__ to get the 'local' imp module from the
  local controllers package then changed directories to the location
  of the "global" controllers module (in sys.path). Popped
  "controllers" from sys.modules and then called __import__ again to
  get that module as another variable.

  To reiterate Ben's method with working code and upper-level 
  importlib:
  >>> from __future__ import absolute_import
  >>> import importlib
  >>> i = importlib.import_module('.imp',package='controllers') #
  # package arg needed
  >>> dir(i)
  ['COLORS', 'CONTROLLER', 'DBNAME', 'DBPWD', 'DBUSERNAME',
  'ID_COL', 'JOB', 'MYTABLE', 'RECID', 'TABLEFORMAT',
  '__builtins__', '__doc__', '__file__', '__name__', '__package__',
  'load', 'std']
  # dir(i) verifies that 'i' is my 'local' 'imp' module
  >>> i1 = importlib.import_module('imp')
  >>> dir(i1) # result not shown
   # verifies 'i1' is my "global" imp module
   # Now, to load my alternate controllers module, I have to do
   # this:
   import os, sys
   sys.modules.pop('controllers')
   os.chdir('/alternate/controllers/directory')
   c = importlib.import_module('controllers')
   # now dir(c) verifies that c is my alternate 'controllers' module
   # NOTE: in my scenario, we could expect that the alternate
   # 'controller' module would be in sys.path. Some iteration might
   # be needed

   thanks Ben. :) I guess I have a plan

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


More information about the Python-list mailing list