Skip to content

Instantly share code, notes, and snippets.

@tiran
Last active September 29, 2017 20:08
Show Gist options
  • Save tiran/3502097de4448d4c85ea47a202712ea9 to your computer and use it in GitHub Desktop.
Save tiran/3502097de4448d4c85ea47a202712ea9 to your computer and use it in GitHub Desktop.
Python import blocking
$ mkdir a
$ touch a/__init__.py
$ touch a/b.py
$ python2
Python 2.7.13 (default, Sep 5 2017, 08:53:59)
[GCC 7.1.1 20170622 (Red Hat 7.1.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> a
<module 'a' from 'a/__init__.py'>
>>> sys.modules['a.b'] = None
>>> from a import b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name b
>>>
$ python3
Python 3.6.2 (default, Sep 22 2017, 08:28:09)
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> a
<module 'a' from '/tmp/a/__init__.py'>
>>> import sys
>>> sys.modules['a.b'] = None
>>> from a import b
>>> b is None
True
$ python3.4
Python 3.4.5 (default, Feb 11 2017, 18:09:02)
[GCC 7.0.1 20170209 (Red Hat 7.0.1-0.7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.modules['a.b'] = None
>>> from a import b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: import of 'a.b' halted; None in sys.modules
>>>
$ python3.5
Python 3.5.3 (default, Jun 26 2017, 10:36:28)
[GCC 7.1.1 20170622 (Red Hat 7.1.1-3)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.modules['a.b'] = None
>>> from a import b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: import of 'a.b' halted; None in sys.modules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment