orphan:
Appendix: Import Syntax Under the Hood¶
Appendix: An Extended Timeline of Importing in Python¶
orphan:
Some Context¶
(origins) http://www.python.org/community/sigs/retired/import-sig/ http://www.python.org/dev/peps/pep-3121/#id11 (1.5) http://www.python.org/doc/essays/packages.html #http://www.python.org/doc/essays/packages/ Modula-3 influence: http://python-history.blogspot.com/2009/02/adding-support-for-user-defined-classes.html http://python-history.blogspot.com/2009/01/brief-timeline-of-python.html http://python-history.blogspot.com/2009/03/dynamically-loaded-modules.html http://docs.python.org/dev/whatsnew/index.html http://python.org/download/releases/src/ http://hg.python.org/cpython-fullhistory/tags http://hg.python.org/cpython-fullhistory/graph/3cd033e6b530?revcount=800 http://hg.python.org/cpython-fullhistory/log/62bdb1cbe0f5/Python/import.c?revcount=120 initial: http://hg.python.org/cpython-fullhistory/file/fc6fcd7df4f7/Python/import.c 0.9.8: http://hg.python.org/cpython-fullhistory/file/17eff686be30/Python/import.c builtin___import__(), importdl.c: http://hg.python.org/cpython-fullhistory/rev/d7e91437f0a2 PyImport_Import: http://hg.python.org/cpython-fullhistory/rev/292193170da1 highlights of “What’s New”: http://nedbatchelder.com/blog/201109/whats_in_which_python.html code_swarm: http://vimeo.com/1093745
(ni) introduced (1.3): http://hg.python.org/cpython-fullhistory/rev/ec0b42889243 deprecated (1.5): http://docs.python.org/release/1.5/lib/node40.html still lives: http://docs.python.org/library/imputil.html#examples
(ihooks) introduced (1.3): http://hg.python.org/cpython-fullhistory/rev/ec0b42889243 removed (3.0): http://docs.python.org/release/2.6.2/library/undoc.html#miscellaneous-useful-utilities http://pydoc.org/2.4.1/ihooks.html
—–
The versions and dates are derived from a post on Guido’s “History of Python” blog. I’ve correlated the entries in section B.1 to versions by either explicit reference or by matching their commits to a version. Section B.2 also maps commits to versions. In both cases, I did my best to determine that mapping, but some may be off by a version.
The Extended Timeline¶
- Initial Checkin (1990)
- Checks sys.modules
- Loads modules from sys.path or current dir (if sys.path is empty)
- Supports IMPORT_NAME and IMPORT_FROM opcodes
- No support for .pyc files
- No support for packages
- No support for C extension modules?
- No ImportError
- Python 0.9.1 (Feb. 1991)
- builtin module support (C extention modules)
- Python 1.0 (1994)
- Support for extension modules
- Support for .pyc files
- Python 1.2 (1995)
- (Python/bltinmodule.c) __import__() builtin introduced
- (Python/import.c) dynamic module support factored out into importdl.c
- Python 1.3 (1995)
- “ni” module introduced
- Python 1.4 (1996)
- http://docs.python.org/release/1.4/ref/ *
- Python 1.5 (1998)
- Support for packages
- “site-packages” and “site-python” directories introduced
- “__all__” introduced
- “ni” module deprecated
- (Python/import.c) PyImport_Import() introduced
- Python 2.0 (2000)
- PEP 221 – Import As
- Python 2.1 (2001)
- PEP 235 – Import on Case-Insensitive Platforms
- Python 2.2 (2001)
- Python 2.3 (2003)
- Python 2.4 (2004)
- PEP 328 – Imports: Multi-Line and Absolute/Relative (multi-line portion)
- Python 2.5 (2006)
- Python 2.6/3.0 (2008)
- Python 3.0 (2008)
- Python 3.1 (2009)
- importlib module added
- Python 3.2 (2011)
- PEP 3147 – PYC Repository Directories
- Python 3.3 (2012)
- see appendix D
Appendix: Ongoing Core Efforts to Improve Importing¶
orphan: |
---|
PEPs¶
- PEP 369 – Post import hooks
- PEP 382 – Namespace Packages
- PEP 395 – Module Aliasing
- PEP 402 – Simplified Package Layout and Partitioning
- PEP ??? – import engine
Rejected PEPs:
Projects¶
- importlib.__import__ as the default builtins.__import__
Currently in Python, “builtin___import__()” in Python/bltinmodule.c makes a call to PyImport_ImportModuleLevelObject. Brett Cannon is working on making importlib.__import__ the default import call[1].
- the __experimental__ module
http://mail.python.org/pipermail/python-ideas/2010-June/007357.html http://mail.python.org/pipermail/python-ideas/2011-August/011278.html
like the __future__ module, but for less-stable APIs that are likely to go in focus on stdlib (room for experimental syntax too?) (higher exposure testing)
Appendix: Imports in Alternate Python Implementations¶
Appendix: Easter Eggs¶
The Python devs are a playful lot.
import antigravity¶
http://xkcd.com/353/ http://python-history.blogspot.com/2010/06/import-antigravity.html
from __future__ import flufl¶
http://www.python.org/dev/peps/pep-0401/ http://sayspy.blogspot.com/2009/03/guido-has-retired-as-bdfl.html
from __future__ import braces¶
import __hello__¶
Appendix: Import Examples¶
orphan: |
---|
How It Works¶
- Example: Plain Syntax Handler
- Example: From Name Syntax Handler
- Example: From Star Syntax Handler
- Example: builtins.__import__
Uncategorized¶
- Example: Naively Track Imports
- Example: Import Tracking, Take 2
- Example: Statement Local Namespaces
- Example: Protecting a High-Latency Filesystem
- Example: Customizing Access to a Specific Module Path
- Example: PEPS 382 and 402 as Import Hooks
- Example: Import Engine as an Import Hook
- Example: lazy imports
- Example: “importing” straight from a file
Appendix: Imports in the Python Community¶
Appendix: Troubleshooting Imports¶
orphan:
Causes of ImportError¶
- turn into ImportError subclasses, __cause__
Other Exceptions During Import¶
- SyntaxError
- IOError?
Appendix: Import Tips and Tricks¶
orphan: |
---|
block imports on the current working directory¶
By default Python will look for a module in your current working directory before trying the stdlib. The explicit relative import syntax of 2.7 help with this, but only to an extent.
To completely keep Python from trying the CWD, simply run “sys.path.remove(‘’)” and optionally follow that with “sys.path.append(‘’)”.
So the question remains, when did the empty string get added to (the front of) sys.path, and why?
...
Glossary¶
orphan: |
---|
The terminology surrounding imports can get confusing. This glossary should help.
- import hook
- ...
- finder
- An object with a find() method that conforms to PEP 302. May also refer to the class of such an object.
- loader
- An object with a load() method that conforms to PEP 302. May also refer to the class of such an object.
- importer
- Mostly synonymous with path importer.
- path importer
- An object, class, or other code that may be plugged into the PEP 302 import machinery. Often this term refers specifically to those that are used with sys.path_hooks.
- module
- The object generated at the highest level of the import process. In the normal import statement, it is the object bound to the name.
- module name
- The value bound to the __name__ attribute of the corresponding module object. This will be the full qualified name relative to the sys.path value at import time.
- package
- A module corresponding to a directory. The module is populated with the results of evaluating the __init__.py file in the directory. Other .py files and directories in the directory may be imported as submodules of the package.
- namespace package
- A package, possibly without its own module execution, into which subpackages are aggregated according to a single namespace. The “zope” package is a good example.
- package portion
- ...