piotr
piotr

Reputation: 5787

Access module masked by variable name

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

Upvotes: 0

Views: 759

Answers (3)

jldupont
jldupont

Reputation: 96806

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

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799062

import x as someotherx

Upvotes: 0

SilentGhost
SilentGhost

Reputation: 319821

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

>>> sys = 2
>>> import sys as s
>>> s
<module 'sys' (built-in)>
>>> sys
2

Upvotes: 3

Related Questions