views:

35

answers:

3

How do I access a module named x that I masked with a variable named x?

+1  A: 

use sys.modules[module_name] ... and you should avoid masking module names: use wisely the import statement e.g. import XYZ as ABC.

You can also rely on using a more complete namespace "path" e.g. os.path.xxx

jldupont
But what if it's `sys` that's masked? Also, using a namespace path won't help if e.g. `os` is no longer a module.
Ignacio Vazquez-Abrams
... and also if XYZ is masked and ... the point is: there are different ways to address the problem.
jldupont
+4  A: 

don't name your variable x or use import ... as style.

>>> sys = 2
>>> import sys as s
>>> s
<module 'sys' (built-in)>
>>> sys
2
SilentGhost
+1  A: 
import x as someotherx
Ignacio Vazquez-Abrams