I've named a module blah.tty
within a project of mine. Now I have a need to access the stdlib top-level tty
module from inside it. Is this possible without doing magic tricks?
Asked
Active
Viewed 44 times
0

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343

d11wtq
- 34,788
- 19
- 120
- 195
-
possible duplicate of [Using absolute\_import and handling relative module name confilcts in python](http://stackoverflow.com/questions/12011327/using-absolute-import-and-handling-relative-module-name-confilcts-in-python) – Christian Berendt Jun 23 '14 at 11:01
1 Answers
1
Use absolute imports:
from __future__ import absolute_import
Now import tty
will refer to the top-level stdlib module. Refer to package-relative names either by using the full path (import blah.tty
) or use dots to demark relative paths (from . import tty
, etc.) See PEP 328.

Martijn Pieters
- 1,048,767
- 296
- 4,058
- 3,343
-
Thanks! Why is the `__future__` package named as such, out of curiosity? – d11wtq Jun 23 '14 at 10:54
-
@d11wtq: because it is not a module, really. It is a hint to the parser instead. Well, it is *is* a module, to avoid confusing tools that'll look for that module, but its purpose is still to set parser options. – Martijn Pieters Jun 23 '14 at 10:55
-
@d11wtq: See https://docs.python.org/2/reference/simple_stmts.html#future and https://docs.python.org/2/library/__future__.html for more info. – Martijn Pieters Jun 23 '14 at 10:57