/
usr
/
share
/
doc
/
python3-docs
/
html
/
library
/
/usr/share/doc/python3-docs/html/library
mkdir
upload
Name
Size
Mode
Actions
2to3.html
61709
0644
edit
dl
rm
abc.html
41394
0644
edit
dl
rm
aifc.html
28114
0644
edit
dl
rm
allos.html
32236
0644
edit
dl
rm
archiving.html
11992
0644
edit
dl
rm
argparse.html
274984
0644
edit
dl
rm
array.html
33547
0644
edit
dl
rm
ast.html
39595
0644
edit
dl
rm
asynchat.html
32546
0644
edit
dl
rm
asyncio-dev.html
51869
0644
edit
dl
rm
asyncio-eventloop.html
111215
0644
edit
dl
rm
asyncio-eventloops.html
33026
0644
edit
dl
rm
asyncio-protocol.html
87431
0644
edit
dl
rm
asyncio-queue.html
23957
0644
edit
dl
rm
asyncio-stream.html
61005
0644
edit
dl
rm
asyncio-subprocess.html
58015
0644
edit
dl
rm
asyncio-sync.html
35678
0644
edit
dl
rm
asyncio-task.html
83968
0644
edit
dl
rm
asyncio.html
28026
0644
edit
dl
rm
asyncore.html
42706
0644
edit
dl
rm
atexit.html
18433
0644
edit
dl
rm
audioop.html
35442
0644
edit
dl
rm
base64.html
36454
0644
edit
dl
rm
bdb.html
43423
0644
edit
dl
rm
binary.html
12723
0644
edit
dl
rm
binascii.html
24745
0644
edit
dl
rm
binhex.html
11669
0644
edit
dl
rm
bisect.html
25602
0644
edit
dl
rm
builtins.html
11604
0644
edit
dl
rm
bz2.html
33358
0644
edit
dl
rm
calendar.html
39539
0644
edit
dl
rm
cgi.html
55881
0644
edit
dl
rm
cgitb.html
12765
0644
edit
dl
rm
chunk.html
16872
0644
edit
dl
rm
cmath.html
34171
0644
edit
dl
rm
cmd.html
43811
0644
edit
dl
rm
code.html
27886
0644
edit
dl
rm
codecs.html
134071
0644
edit
dl
rm
codeop.html
14304
0644
edit
dl
rm
collections.abc.html
60912
0644
edit
dl
rm
collections.html
154946
0644
edit
dl
rm
colorsys.html
12627
0644
edit
dl
rm
compileall.html
28263
0644
edit
dl
rm
concurrency.html
21203
0644
edit
dl
rm
concurrent.futures.html
60660
0644
edit
dl
rm
concurrent.html
7959
0644
edit
dl
rm
configparser.html
141052
0644
edit
dl
rm
constants.html
17500
0644
edit
dl
rm
contextlib.html
88801
0644
edit
dl
rm
copy.html
13841
0644
edit
dl
rm
copyreg.html
14725
0644
edit
dl
rm
crypt.html
20331
0644
edit
dl
rm
crypto.html
10914
0644
edit
dl
rm
csv.html
69043
0644
edit
dl
rm
ctypes.html
264529
0644
edit
dl
rm
curses.ascii.html
25197
0644
edit
dl
rm
curses.html
175551
0644
edit
dl
rm
curses.panel.html
16706
0644
edit
dl
rm
custominterp.html
8734
0644
edit
dl
rm
datatypes.html
23950
0644
edit
dl
rm
datetime.html
306435
0644
edit
dl
rm
dbm.html
43119
0644
edit
dl
rm
debug.html
13983
0644
edit
dl
rm
decimal.html
233486
0644
edit
dl
rm
development.html
25123
0644
edit
dl
rm
difflib.html
96156
0644
edit
dl
rm
dis.html
106191
0644
edit
dl
rm
distribution.html
10506
0644
edit
dl
rm
distutils.html
10884
0644
edit
dl
rm
doctest.html
179752
0644
edit
dl
rm
dummy_threading.html
9316
0644
edit
dl
rm
email.charset.html
26431
0644
edit
dl
rm
email.compat32-message.html
86150
0644
edit
dl
rm
email.contentmanager.html
30028
0644
edit
dl
rm
email.encoders.html
14250
0644
edit
dl
rm
email.errors.html
19704
0644
edit
dl
rm
email.examples.html
57872
0644
edit
dl
rm
email.generator.html
41798
0644
edit
dl
rm
email.header.html
31219
0644
edit
dl
rm
email.headerregistry.html
57670
0644
edit
dl
rm
email.html
27741
0644
edit
dl
rm
email.iterators.html
13532
0644
edit
dl
rm
email.message.html
87210
0644
edit
dl
rm
email.mime.html
36081
0644
edit
dl
rm
email.parser.html
47430
0644
edit
dl
rm
email.policy.html
75241
0644
edit
dl
rm
email.util.html
33853
0644
edit
dl
rm
ensurepip.html
18700
0644
edit
dl
rm
enum.html
133949
0644
edit
dl
rm
errno.html
41331
0644
edit
dl
rm
exceptions.html
85151
0644
edit
dl
rm
faulthandler.html
23396
0644
edit
dl
rm
fcntl.html
27321
0644
edit
dl
rm
filecmp.html
25036
0644
edit
dl
rm
fileformats.html
11129
0644
edit
dl
rm
fileinput.html
30030
0644
edit
dl
rm
filesys.html
13418
0644
edit
dl
rm
fnmatch.html
16987
0644
edit
dl
rm
formatter.html
37666
0644
edit
dl
rm
fpectl.html
17538
0644
edit
dl
rm
fractions.html
29918
0644
edit
dl
rm
frameworks.html
13471
0644
edit
dl
rm
ftplib.html
54429
0644
edit
dl
rm
functional.html
9002
0644
edit
dl
rm
functions.html
201486
0644
edit
dl
rm
functools.html
64951
0644
edit
dl
rm
gc.html
29524
0644
edit
dl
rm
getopt.html
25353
0644
edit
dl
rm
getpass.html
12061
0644
edit
dl
rm
gettext.html
76641
0644
edit
dl
rm
glob.html
18432
0644
edit
dl
rm
grp.html
12383
0644
edit
dl
rm
gzip.html
32669
0644
edit
dl
rm
hashlib.html
79554
0644
edit
dl
rm
heapq.html
35529
0644
edit
dl
rm
hmac.html
17827
0644
edit
dl
rm
html.entities.html
11361
0644
edit
dl
rm
html.html
11108
0644
edit
dl
rm
html.parser.html
41842
0644
edit
dl
rm
http.client.html
65250
0644
edit
dl
rm
http.cookiejar.html
96195
0644
edit
dl
rm
http.cookies.html
39329
0644
edit
dl
rm
http.html
37602
0644
edit
dl
rm
http.server.html
58191
0644
edit
dl
rm
i18n.html
10464
0644
edit
dl
rm
idle.html
48412
0644
edit
dl
rm
imaplib.html
67339
0644
edit
dl
rm
imghdr.html
13456
0644
edit
dl
rm
imp.html
48153
0644
edit
dl
rm
importlib.html
173109
0644
edit
dl
rm
index.html
67768
0644
edit
dl
rm
inspect.html
130355
0644
edit
dl
rm
internet.html
31087
0644
edit
dl
rm
intro.html
9904
0644
edit
dl
rm
io.html
122685
0644
edit
dl
rm
ipaddress.html
105873
0644
edit
dl
rm
ipc.html
33743
0644
edit
dl
rm
itertools.html
132824
0644
edit
dl
rm
json.html
81078
0644
edit
dl
rm
keyword.html
8918
0644
edit
dl
rm
language.html
13648
0644
edit
dl
rm
linecache.html
13492
0644
edit
dl
rm
locale.html
61540
0644
edit
dl
rm
logging.config.html
85489
0644
edit
dl
rm
logging.handlers.html
112397
0644
edit
dl
rm
logging.html
134850
0644
edit
dl
rm
lzma.html
56484
0644
edit
dl
rm
macpath.html
9257
0644
edit
dl
rm
mailbox.html
157315
0644
edit
dl
rm
mailcap.html
14676
0644
edit
dl
rm
markup.html
19356
0644
edit
dl
rm
marshal.html
18317
0644
edit
dl
rm
math.html
53661
0644
edit
dl
rm
mimetypes.html
31410
0644
edit
dl
rm
misc.html
8123
0644
edit
dl
rm
mm.html
10022
0644
edit
dl
rm
mmap.html
34294
0644
edit
dl
rm
modulefinder.html
18559
0644
edit
dl
rm
modules.html
10914
0644
edit
dl
rm
msilib.html
57205
0644
edit
dl
rm
msvcrt.html
21989
0644
edit
dl
rm
multiprocessing.html
365487
0644
edit
dl
rm
netdata.html
17470
0644
edit
dl
rm
netrc.html
14568
0644
edit
dl
rm
nis.html
11659
0644
edit
dl
rm
nntplib.html
69601
0644
edit
dl
rm
numbers.html
41432
0644
edit
dl
rm
numeric.html
15535
0644
edit
dl
rm
operator.html
81460
0644
edit
dl
rm
optparse.html
249942
0644
edit
dl
rm
os.html
398039
0644
edit
dl
rm
os.path.html
53818
0644
edit
dl
rm
ossaudiodev.html
47947
0644
edit
dl
rm
othergui.html
10256
0644
edit
dl
rm
parser.html
42408
0644
edit
dl
rm
pathlib.html
116726
0644
edit
dl
rm
pdb.html
52661
0644
edit
dl
rm
persistence.html
15281
0644
edit
dl
rm
pickle.html
115052
0644
edit
dl
rm
pickletools.html
17094
0644
edit
dl
rm
pipes.html
15991
0644
edit
dl
rm
pkgutil.html
31878
0644
edit
dl
rm
platform.html
33728
0644
edit
dl
rm
plistlib.html
29290
0644
edit
dl
rm
poplib.html
30940
0644
edit
dl
rm
posix.html
17608
0644
edit
dl
rm
pprint.html
41969
0644
edit
dl
rm
profile.html
71848
0644
edit
dl
rm
pty.html
17594
0644
edit
dl
rm
pwd.html
13098
0644
edit
dl
rm
pyclbr.html
16520
0644
edit
dl
rm
pydoc.html
15991
0644
edit
dl
rm
pyexpat.html
88938
0644
edit
dl
rm
python.html
17133
0644
edit
dl
rm
py_compile.html
15508
0644
edit
dl
rm
queue.html
28199
0644
edit
dl
rm
quopri.html
13858
0644
edit
dl
rm
random.html
60646
0644
edit
dl
rm
re.html
191176
0644
edit
dl
rm
readline.html
42133
0644
edit
dl
rm
reprlib.html
24964
0644
edit
dl
rm
resource.html
36552
0644
edit
dl
rm
rlcompleter.html
14392
0644
edit
dl
rm
runpy.html
26607
0644
edit
dl
rm
sched.html
20034
0644
edit
dl
rm
secrets.html
24734
0644
edit
dl
rm
select.html
62819
0644
edit
dl
rm
selectors.html
35845
0644
edit
dl
rm
shelve.html
28777
0644
edit
dl
rm
shlex.html
48979
0644
edit
dl
rm
shutil.html
74207
0644
edit
dl
rm
signal.html
55064
0644
edit
dl
rm
site.html
32242
0644
edit
dl
rm
smtpd.html
35892
0644
edit
dl
rm
smtplib.html
68433
0644
edit
dl
rm
sndhdr.html
12439
0644
edit
dl
rm
socket.html
196077
0644
edit
dl
rm
socketserver.html
81918
0644
edit
dl
rm
spwd.html
12328
0644
edit
dl
rm
sqlite3.html
161872
0644
edit
dl
rm
ssl.html
266327
0644
edit
dl
rm
stat.html
44722
0644
edit
dl
rm
statistics.html
50050
0644
edit
dl
rm
stdtypes.html
465658
0644
edit
dl
rm
string.html
95262
0644
edit
dl
rm
stringprep.html
18654
0644
edit
dl
rm
struct.html
51100
0644
edit
dl
rm
subprocess.html
141926
0644
edit
dl
rm
sunau.html
31757
0644
edit
dl
rm
superseded.html
13781
0644
edit
dl
rm
symbol.html
8848
0644
edit
dl
rm
symtable.html
26112
0644
edit
dl
rm
sys.html
141670
0644
edit
dl
rm
sysconfig.html
30259
0644
edit
dl
rm
syslog.html
21026
0644
edit
dl
rm
tabnanny.html
12269
0644
edit
dl
rm
tarfile.html
96758
0644
edit
dl
rm
telnetlib.html
30409
0644
edit
dl
rm
tempfile.html
41394
0644
edit
dl
rm
termios.html
18181
0644
edit
dl
rm
test.html
78555
0644
edit
dl
rm
text.html
13786
0644
edit
dl
rm
textwrap.html
37998
0644
edit
dl
rm
threading.html
103464
0644
edit
dl
rm
time.html
79629
0644
edit
dl
rm
timeit.html
45249
0644
edit
dl
rm
tk.html
23299
0644
edit
dl
rm
tkinter.html
82932
0644
edit
dl
rm
tkinter.scrolledtext.html
9957
0644
edit
dl
rm
tkinter.tix.html
51858
0644
edit
dl
rm
tkinter.ttk.html
110673
0644
edit
dl
rm
token.html
22660
0644
edit
dl
rm
tokenize.html
32149
0644
edit
dl
rm
trace.html
29145
0644
edit
dl
rm
traceback.html
57588
0644
edit
dl
rm
tracemalloc.html
101144
0644
edit
dl
rm
tty.html
10540
0644
edit
dl
rm
turtle.html
250367
0644
edit
dl
rm
types.html
37060
0644
edit
dl
rm
typing.html
139175
0644
edit
dl
rm
undoc.html
9181
0644
edit
dl
rm
unicodedata.html
21108
0644
edit
dl
rm
unittest.html
250689
0644
edit
dl
rm
unittest.mock-examples.html
168144
0644
edit
dl
rm
unittest.mock.html
300089
0644
edit
dl
rm
unix.html
11780
0644
edit
dl
rm
urllib.error.html
14474
0644
edit
dl
rm
urllib.html
9566
0644
edit
dl
rm
urllib.parse.html
76334
0644
edit
dl
rm
urllib.request.html
177199
0644
edit
dl
rm
urllib.robotparser.html
17156
0644
edit
dl
rm
uu.html
12209
0644
edit
dl
rm
uuid.html
31957
0644
edit
dl
rm
venv.html
73909
0644
edit
dl
rm
warnings.html
50320
0644
edit
dl
rm
wave.html
29868
0644
edit
dl
rm
weakref.html
65292
0644
edit
dl
rm
webbrowser.html
26901
0644
edit
dl
rm
windows.html
10626
0644
edit
dl
rm
winreg.html
66730
0644
edit
dl
rm
winsound.html
21268
0644
edit
dl
rm
wsgiref.html
94492
0644
edit
dl
rm
xdrlib.html
33751
0644
edit
dl
rm
xml.dom.html
97570
0644
edit
dl
rm
xml.dom.minidom.html
41481
0644
edit
dl
rm
xml.dom.pulldom.html
24214
0644
edit
dl
rm
xml.etree.elementtree.html
135491
0644
edit
dl
rm
xml.html
19261
0644
edit
dl
rm
xml.sax.handler.html
42659
0644
edit
dl
rm
xml.sax.html
25515
0644
edit
dl
rm
xml.sax.reader.html
45869
0644
edit
dl
rm
xml.sax.utils.html
16928
0644
edit
dl
rm
xmlrpc.client.html
71947
0644
edit
dl
rm
xmlrpc.html
8535
0644
edit
dl
rm
xmlrpc.server.html
49195
0644
edit
dl
rm
zipapp.html
45105
0644
edit
dl
rm
zipfile.html
80039
0644
edit
dl
rm
zipimport.html
23052
0644
edit
dl
rm
zlib.html
34557
0644
edit
dl
rm
_dummy_thread.html
9193
0644
edit
dl
rm
_thread.html
22588
0644
edit
dl
rm
__future__.html
15792
0644
edit
dl
rm
__main__.html
9019
0644
edit
dl
rm
Edit:
/usr/share/doc/python3-docs/html/library/importlib.html
(173109B)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>31.5. importlib — The implementation of import — Python 3.6.7 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.6.7 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="32. Python Language Services" href="language.html" /> <link rel="prev" title="31.4. runpy — Locating and executing Python modules" href="runpy.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <link rel="canonical" href="https://docs.python.org/3/library/importlib.html" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head><body> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="language.html" title="32. Python Language Services" accesskey="N">next</a> |</li> <li class="right" > <a href="runpy.html" title="31.4. runpy — Locating and executing Python modules" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.6.7 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">31. Importing Modules</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="module-importlib"> <span id="importlib-the-implementation-of-import"></span><h1>31.5. <a class="reference internal" href="#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> — The implementation of <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a><a class="headerlink" href="#module-importlib" title="Permalink to this headline">¶</a></h1> <div class="versionadded"> <p><span class="versionmodified">New in version 3.1.</span></p> </div> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/importlib/__init__.py">Lib/importlib/__init__.py</a></p> <hr class="docutils" /> <div class="section" id="introduction"> <h2>31.5.1. Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2> <p>The purpose of the <a class="reference internal" href="#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> package is two-fold. One is to provide the implementation of the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement (and thus, by extension, the <a class="reference internal" href="functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function) in Python source code. This provides an implementation of <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.</p> <p>Two, the components to implement <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> are exposed in this package, making it easier for users to create their own custom objects (known generically as an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>) to participate in the import process.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference internal" href="../reference/simple_stmts.html#import"><span class="std std-ref">The import statement</span></a></dt> <dd>The language reference for the <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement.</dd> <dt><a class="reference external" href="https://www.python.org/doc/essays/packages/">Packages specification</a></dt> <dd>Original specification of packages. Some semantics have changed since the writing of this document (e.g. redirecting based on <code class="docutils literal notranslate"><span class="pre">None</span></code> in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>).</dd> <dt>The <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function</dt> <dd>The <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> statement is syntactic sugar for this function.</dd> <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0235"><strong>PEP 235</strong></a></dt> <dd>Import on Case-Insensitive Platforms</dd> <dt><span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0263"><strong>PEP 263</strong></a></dt> <dd>Defining Python Source Code Encodings</dd> <dt><span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a></dt> <dd>New Import Hooks</dd> <dt><span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a></dt> <dd>Imports: Multi-Line and Absolute/Relative</dd> <dt><span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0366"><strong>PEP 366</strong></a></dt> <dd>Main module explicit relative imports</dd> <dt><span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0420"><strong>PEP 420</strong></a></dt> <dd>Implicit namespace packages</dd> <dt><span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0451"><strong>PEP 451</strong></a></dt> <dd>A ModuleSpec Type for the Import System</dd> <dt><span class="target" id="index-7"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a></dt> <dd>Elimination of PYO files</dd> <dt><span class="target" id="index-8"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a></dt> <dd>Multi-phase extension module initialization</dd> <dt><span class="target" id="index-9"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3120"><strong>PEP 3120</strong></a></dt> <dd>Using UTF-8 as the Default Source Encoding</dd> <dt><span class="target" id="index-10"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a></dt> <dd>PYC Repository Directories</dd> </dl> </div> </div> <div class="section" id="functions"> <h2>31.5.2. Functions<a class="headerlink" href="#functions" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="importlib.__import__"> <code class="descclassname">importlib.</code><code class="descname">__import__</code><span class="sig-paren">(</span><em>name</em>, <em>globals=None</em>, <em>locals=None</em>, <em>fromlist=()</em>, <em>level=0</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.__import__" title="Permalink to this definition">¶</a></dt> <dd><p>An implementation of the built-in <a class="reference internal" href="functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> function.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Programmatic importing of modules should use <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a> instead of this function.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.import_module"> <code class="descclassname">importlib.</code><code class="descname">import_module</code><span class="sig-paren">(</span><em>name</em>, <em>package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.import_module" title="Permalink to this definition">¶</a></dt> <dd><p>Import a module. The <em>name</em> argument specifies what module to import in absolute or relative terms (e.g. either <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code> or <code class="docutils literal notranslate"><span class="pre">..mod</span></code>). If the name is specified in relative terms, then the <em>package</em> argument must be set to the name of the package which is to act as the anchor for resolving the package name (e.g. <code class="docutils literal notranslate"><span class="pre">import_module('..mod',</span> <span class="pre">'pkg.subpkg')</span></code> will import <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code>).</p> <p>The <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a> function acts as a simplifying wrapper around <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.__import__()</span></code></a>. This means all semantics of the function are derived from <a class="reference internal" href="#importlib.__import__" title="importlib.__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.__import__()</span></code></a>. The most important difference between these two functions is that <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code></a> returns the specified package or module (e.g. <code class="docutils literal notranslate"><span class="pre">pkg.mod</span></code>), while <a class="reference internal" href="functions.html#__import__" title="__import__"><code class="xref py py-func docutils literal notranslate"><span class="pre">__import__()</span></code></a> returns the top-level package or module (e.g. <code class="docutils literal notranslate"><span class="pre">pkg</span></code>).</p> <p>If you are dynamically importing a module that was created since the interpreter began execution (e.g., created a Python source file), you may need to call <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">invalidate_caches()</span></code></a> in order for the new module to be noticed by the import system.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span>Parent packages are automatically imported.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.find_loader"> <code class="descclassname">importlib.</code><code class="descname">find_loader</code><span class="sig-paren">(</span><em>name</em>, <em>path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.find_loader" title="Permalink to this definition">¶</a></dt> <dd><p>Find the loader for a module, optionally within the specified <em>path</em>. If the module is in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, then <code class="docutils literal notranslate"><span class="pre">sys.modules[name].__loader__</span></code> is returned (unless the loader would be <code class="docutils literal notranslate"><span class="pre">None</span></code> or is not set, in which case <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised). Otherwise a search using <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> is done. <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned if no loader is found.</p> <p>A dotted name does not have its parents implicitly imported as that requires loading them and that may not be desired. To properly import a submodule you will need to import all parent packages of the submodule and use the correct argument to <em>path</em>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>If <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> is not set, raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>, just like when the attribute is set to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a> instead.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.invalidate_caches"> <code class="descclassname">importlib.</code><code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.invalidate_caches" title="Permalink to this definition">¶</a></dt> <dd><p>Invalidate the internal caches of finders stored at <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a>. If a finder implements <code class="docutils literal notranslate"><span class="pre">invalidate_caches()</span></code> then it will be called to perform the invalidation. This function should be called if any modules are created/installed while your program is running to guarantee all finders will notice the new module’s existence.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.reload"> <code class="descclassname">importlib.</code><code class="descname">reload</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.reload" title="Permalink to this definition">¶</a></dt> <dd><p>Reload a previously imported <em>module</em>. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (which can be different if re-importing causes a different object to be placed in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>).</p> <p>When <a class="reference internal" href="#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></a> is executed:</p> <ul class="simple"> <li>Python module’s code is recompiled and the module-level code re-executed, defining a new set of objects which are bound to names in the module’s dictionary by reusing the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which originally loaded the module. The <code class="docutils literal notranslate"><span class="pre">init</span></code> function of extension modules is not called a second time.</li> <li>As with all other objects in Python the old objects are only reclaimed after their reference counts drop to zero.</li> <li>The names in the module namespace are updated to point to any new or changed objects.</li> <li>Other references to the old objects (such as names external to the module) are not rebound to refer to the new objects and must be updated in each namespace where they occur if that is desired.</li> </ul> <p>There are a number of other caveats:</p> <p>When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains. This feature can be used to the module’s advantage if it maintains a global table or cache of objects — with a <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> statement it can test for the table’s presence and skip its initialization if desired:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span> <span class="n">cache</span> <span class="k">except</span> <span class="ne">NameError</span><span class="p">:</span> <span class="n">cache</span> <span class="o">=</span> <span class="p">{}</span> </pre></div> </div> <p>It is generally not very useful to reload built-in or dynamically loaded modules. Reloading <a class="reference internal" href="sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a>, <a class="reference internal" href="__main__.html#module-__main__" title="__main__: The environment where the top-level script is run."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>, <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> and other key modules is not recommended. In many cases extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded.</p> <p>If a module imports objects from another module using <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> … <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> …, calling <a class="reference internal" href="#importlib.reload" title="importlib.reload"><code class="xref py py-func docutils literal notranslate"><span class="pre">reload()</span></code></a> for the other module does not redefine the objects imported from it — one way around this is to re-execute the <a class="reference internal" href="../reference/simple_stmts.html#from"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">from</span></code></a> statement, another is to use <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> and qualified names (<em>module.name</em>) instead.</p> <p>If a module instantiates instances of a class, reloading the module that defines the class does not affect the method definitions of the instances — they continue to use the old class definition. The same is true for derived classes.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> </div> <div class="section" id="module-importlib.abc"> <span id="importlib-abc-abstract-base-classes-related-to-import"></span><h2>31.5.3. <a class="reference internal" href="#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> – Abstract base classes related to import<a class="headerlink" href="#module-importlib.abc" title="Permalink to this headline">¶</a></h2> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/importlib/abc.py">Lib/importlib/abc.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-importlib.abc" title="importlib.abc: Abstract base classes related to import"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.abc</span></code></a> module contains all of the core abstract base classes used by <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a>. Some subclasses of the core abstract base classes are also provided to help in implementing the core ABCs.</p> <p>ABC hierarchy:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">object</span> <span class="o">+--</span> <span class="n">Finder</span> <span class="p">(</span><span class="n">deprecated</span><span class="p">)</span> <span class="o">|</span> <span class="o">+--</span> <span class="n">MetaPathFinder</span> <span class="o">|</span> <span class="o">+--</span> <span class="n">PathEntryFinder</span> <span class="o">+--</span> <span class="n">Loader</span> <span class="o">+--</span> <span class="n">ResourceLoader</span> <span class="o">--------+</span> <span class="o">+--</span> <span class="n">InspectLoader</span> <span class="o">|</span> <span class="o">+--</span> <span class="n">ExecutionLoader</span> <span class="o">--+</span> <span class="o">+--</span> <span class="n">FileLoader</span> <span class="o">+--</span> <span class="n">SourceLoader</span> </pre></div> </div> <dl class="class"> <dt id="importlib.abc.Finder"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">Finder</code><a class="headerlink" href="#importlib.abc.Finder" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.3: </span>Use <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaPathFinder</span></code></a> or <a class="reference internal" href="#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">PathEntryFinder</span></code></a> instead.</p> </div> <dl class="method"> <dt id="importlib.abc.Finder.find_module"> <em class="property">abstractmethod </em><code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em>, <em>path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Finder.find_module" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the specified module. Originally specified in <span class="target" id="index-11"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>, this method was meant for use in <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> and in the path-based import subsystem.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> when called instead of raising <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.MetaPathFinder"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">MetaPathFinder</code><a class="headerlink" href="#importlib.abc.MetaPathFinder" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a>. For compatibility, this is a subclass of <a class="reference internal" href="#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal notranslate"><span class="pre">Finder</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="method"> <dt id="importlib.abc.MetaPathFinder.find_spec"> <code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.find_spec" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for the specified module. If this is a top-level import, <em>path</em> will be <code class="docutils literal notranslate"><span class="pre">None</span></code>. Otherwise, this is a search for a subpackage or module and <em>path</em> will be the value of <a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a> from the parent package. If a spec cannot be found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. When passed in, <code class="docutils literal notranslate"><span class="pre">target</span></code> is a module object that the finder may use to make a more educated guess about what spec to return.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.MetaPathFinder.find_module"> <code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.find_module" title="Permalink to this definition">¶</a></dt> <dd><p>A legacy method for finding a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the specified module. If this is a top-level import, <em>path</em> will be <code class="docutils literal notranslate"><span class="pre">None</span></code>. Otherwise, this is a search for a subpackage or module and <em>path</em> will be the value of <a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a> from the parent package. If a loader cannot be found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p> <p>If <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> is defined, backwards-compatible functionality is provided.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> when called instead of raising <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>. Can use <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> to provide functionality.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.MetaPathFinder.find_spec" title="importlib.abc.MetaPathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.MetaPathFinder.invalidate_caches"> <code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.MetaPathFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt> <dd><p>An optional method which, when called, should invalidate any internal cache used by the finder. Used by <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.invalidate_caches()</span></code></a> when invalidating the caches of all finders on <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> when called instead of <code class="docutils literal notranslate"><span class="pre">NotImplemented</span></code>.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.PathEntryFinder"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">PathEntryFinder</code><a class="headerlink" href="#importlib.abc.PathEntryFinder" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class representing a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>. Though it bears some similarities to <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaPathFinder</span></code></a>, <code class="docutils literal notranslate"><span class="pre">PathEntryFinder</span></code> is meant for use only within the path-based import subsystem provided by <code class="xref py py-class docutils literal notranslate"><span class="pre">PathFinder</span></code>. This ABC is a subclass of <a class="reference internal" href="#importlib.abc.Finder" title="importlib.abc.Finder"><code class="xref py py-class docutils literal notranslate"><span class="pre">Finder</span></code></a> for compatibility reasons only.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="method"> <dt id="importlib.abc.PathEntryFinder.find_spec"> <code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.find_spec" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method for finding a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for the specified module. The finder will search for the module only within the <a class="reference internal" href="../glossary.html#term-path-entry"><span class="xref std std-term">path entry</span></a> to which it is assigned. If a spec cannot be found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. When passed in, <code class="docutils literal notranslate"><span class="pre">target</span></code> is a module object that the finder may use to make a more educated guess about what spec to return.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.PathEntryFinder.find_loader"> <code class="descname">find_loader</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.find_loader" title="Permalink to this definition">¶</a></dt> <dd><p>A legacy method for finding a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> for the specified module. Returns a 2-tuple of <code class="docutils literal notranslate"><span class="pre">(loader,</span> <span class="pre">portion)</span></code> where <code class="docutils literal notranslate"><span class="pre">portion</span></code> is a sequence of file system locations contributing to part of a namespace package. The loader may be <code class="docutils literal notranslate"><span class="pre">None</span></code> while specifying <code class="docutils literal notranslate"><span class="pre">portion</span></code> to signify the contribution of the file system locations to a namespace package. An empty list can be used for <code class="docutils literal notranslate"><span class="pre">portion</span></code> to signify the loader is not part of a namespace package. If <code class="docutils literal notranslate"><span class="pre">loader</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and <code class="docutils literal notranslate"><span class="pre">portion</span></code> is the empty list then no loader or location for a namespace package were found (i.e. failure to find anything for the module).</p> <p>If <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> is defined then backwards-compatible functionality is provided.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Returns <code class="docutils literal notranslate"><span class="pre">(None,</span> <span class="pre">[])</span></code> instead of raising <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>. Uses <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> when available to provide functionality.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.PathEntryFinder.find_module"> <code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.find_module" title="Permalink to this definition">¶</a></dt> <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.Finder.find_module" title="importlib.abc.Finder.find_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Finder.find_module()</span></code></a> which is equivalent to <code class="docutils literal notranslate"><span class="pre">self.find_loader(fullname)[0]</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.PathEntryFinder.find_spec" title="importlib.abc.PathEntryFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.PathEntryFinder.invalidate_caches"> <code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.PathEntryFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt> <dd><p>An optional method which, when called, should invalidate any internal cache used by the finder. Used by <code class="xref py py-meth docutils literal notranslate"><span class="pre">PathFinder.invalidate_caches()</span></code> when invalidating the caches of all cached finders.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.Loader"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">Loader</code><a class="headerlink" href="#importlib.abc.Loader" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>. See <span class="target" id="index-12"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> for the exact definition for a loader.</p> <dl class="method"> <dt id="importlib.abc.Loader.create_module"> <code class="descname">create_module</code><span class="sig-paren">(</span><em>spec</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.create_module" title="Permalink to this definition">¶</a></dt> <dd><p>A method that returns the module object to use when importing a module. This method may return <code class="docutils literal notranslate"><span class="pre">None</span></code>, indicating that default module creation semantics should take place.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Starting in Python 3.6, this method will not be optional when <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is defined.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.Loader.exec_module"> <code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.exec_module" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method that executes the module in its own namespace when a module is imported or reloaded. The module should already be initialized when <code class="docutils literal notranslate"><span class="pre">exec_module()</span></code> is called. When this method exists, <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> must be defined.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span><a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> must also be defined.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.Loader.load_module"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.load_module" title="Permalink to this definition">¶</a></dt> <dd><p>A legacy method for loading a module. If the module cannot be loaded, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised, otherwise the loaded module is returned.</p> <p>If the requested module already exists in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, that module should be used and reloaded. Otherwise the loader should create a new module and insert it into <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> before any loading begins, to prevent recursion from the import. If the loader inserted a module and the load fails, it must be removed by the loader from <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>; modules already in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> before the loader began execution should be left alone (see <a class="reference internal" href="#importlib.util.module_for_loader" title="importlib.util.module_for_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.module_for_loader()</span></code></a>).</p> <p>The loader should set several attributes on the module. (Note that some of these attributes can change when a module is reloaded):</p> <ul class="simple"> <li><dl class="first docutils"> <dt><a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a></dt> <dd>The name of the module.</dd> </dl> </li> <li><dl class="first docutils"> <dt><a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a></dt> <dd>The path to where the module data is stored (not set for built-in modules).</dd> </dl> </li> <li><dl class="first docutils"> <dt><a class="reference internal" href="../reference/import.html#__cached__" title="__cached__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__cached__</span></code></a></dt> <dd>The path to where a compiled version of the module is/should be stored (not set when the attribute would be inappropriate).</dd> </dl> </li> <li><dl class="first docutils"> <dt><a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a></dt> <dd>A list of strings specifying the search path within a package. This attribute is not set on modules.</dd> </dl> </li> <li><dl class="first docutils"> <dt><a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a></dt> <dd>The parent package for the module/package. If the module is top-level then it has a value of the empty string. The <a class="reference internal" href="#importlib.util.module_for_loader" title="importlib.util.module_for_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.module_for_loader()</span></code></a> decorator can handle the details for <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a>.</dd> </dl> </li> <li><dl class="first docutils"> <dt><a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a></dt> <dd>The loader used to load the module. The <a class="reference internal" href="#importlib.util.module_for_loader" title="importlib.util.module_for_loader"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.module_for_loader()</span></code></a> decorator can handle the details for <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a>.</dd> </dl> </li> </ul> <p>When <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is available then backwards-compatible functionality is provided.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> when called instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>. Functionality provided when <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> is available.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>The recommended API for loading a module is <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> (and <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a>). Loaders should implement it instead of load_module(). The import machinery takes care of all the other responsibilities of load_module() when exec_module() is implemented.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.Loader.module_repr"> <code class="descname">module_repr</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.Loader.module_repr" title="Permalink to this definition">¶</a></dt> <dd><p>A legacy method which when implemented calculates and returns the given module’s repr, as a string. The module type’s default repr() will use the result of this method as appropriate.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Made optional instead of an abstractmethod.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery now takes care of this automatically.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.ResourceLoader"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">ResourceLoader</code><a class="headerlink" href="#importlib.abc.ResourceLoader" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which implements the optional <span class="target" id="index-13"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol for loading arbitrary resources from the storage back-end.</p> <dl class="method"> <dt id="importlib.abc.ResourceLoader.get_data"> <em class="property">abstractmethod </em><code class="descname">get_data</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ResourceLoader.get_data" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method to return the bytes for the data located at <em>path</em>. Loaders that have a file-like storage back-end that allows storing arbitrary data can implement this abstract method to give direct access to the data stored. <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is to be raised if the <em>path</em> cannot be found. The <em>path</em> is expected to be constructed using a module’s <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> attribute or an item from a package’s <a class="reference internal" href="../reference/import.html#__path__" title="__path__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__path__</span></code></a>.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.InspectLoader"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">InspectLoader</code><a class="headerlink" href="#importlib.abc.InspectLoader" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> which implements the optional <span class="target" id="index-14"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol for loaders that inspect modules.</p> <dl class="method"> <dt id="importlib.abc.InspectLoader.get_code"> <code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.get_code" title="Permalink to this definition">¶</a></dt> <dd><p>Return the code object for a module, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if the module does not have a code object (as would be the case, for example, for a built-in module). Raise an <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> if loader cannot find the requested module.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">While the method has a default implementation, it is suggested that it be overridden if possible for performance.</p> </div> <div class="versionchanged" id="index-15"> <p><span class="versionmodified">Changed in version 3.4: </span>No longer abstract and a concrete implementation is provided.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.InspectLoader.get_source"> <em class="property">abstractmethod </em><code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.get_source" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method to return the source of a module. It is returned as a text string using <a class="reference internal" href="../glossary.html#term-universal-newlines"><span class="xref std std-term">universal newlines</span></a>, translating all recognized line separators into <code class="docutils literal notranslate"><span class="pre">'\n'</span></code> characters. Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> if no source is available (e.g. a built-in module). Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> if the loader cannot find the module specified.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.InspectLoader.is_package"> <code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.is_package" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method to return a true value if the module is a package, a false value otherwise. <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised if the <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> cannot find the module.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> <dl class="staticmethod"> <dt id="importlib.abc.InspectLoader.source_to_code"> <em class="property">static </em><code class="descname">source_to_code</code><span class="sig-paren">(</span><em>data</em>, <em>path='<string>'</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.source_to_code" title="Permalink to this definition">¶</a></dt> <dd><p>Create a code object from Python source.</p> <p>The <em>data</em> argument can be whatever the <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> function supports (i.e. string or bytes). The <em>path</em> argument should be the “path” to where the source code originated from, which can be an abstract concept (e.g. location in a zip file).</p> <p>With the subsequent code object one can execute it in a module by running <code class="docutils literal notranslate"><span class="pre">exec(code,</span> <span class="pre">module.__dict__)</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>Made the method static.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.InspectLoader.exec_module"> <code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.exec_module" title="Permalink to this definition">¶</a></dt> <dd><p>Implementation of <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.InspectLoader.load_module"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.InspectLoader.load_module" title="Permalink to this definition">¶</a></dt> <dd><p>Implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.load_module()</span></code></a>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>use <a class="reference internal" href="#importlib.abc.InspectLoader.exec_module" title="importlib.abc.InspectLoader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> instead.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.ExecutionLoader"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">ExecutionLoader</code><a class="headerlink" href="#importlib.abc.ExecutionLoader" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class which inherits from <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">InspectLoader</span></code></a> that, when implemented, helps a module to be executed as a script. The ABC represents an optional <span class="target" id="index-16"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a> protocol.</p> <dl class="method"> <dt id="importlib.abc.ExecutionLoader.get_filename"> <em class="property">abstractmethod </em><code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.ExecutionLoader.get_filename" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract method that is to return the value of <a class="reference internal" href="../reference/import.html#__file__" title="__file__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__file__</span></code></a> for the specified module. If no path is available, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised.</p> <p>If source code is available, then the method should return the path to the source file, regardless of whether a bytecode was used to load the module.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raises <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.FileLoader"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">FileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class which inherits from <a class="reference internal" href="#importlib.abc.ResourceLoader" title="importlib.abc.ResourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResourceLoader</span></code></a> and <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExecutionLoader</span></code></a>, providing concrete implementations of <a class="reference internal" href="#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ResourceLoader.get_data()</span></code></a> and <a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExecutionLoader.get_filename()</span></code></a>.</p> <p>The <em>fullname</em> argument is a fully resolved name of the module the loader is to handle. The <em>path</em> argument is the path to the file for the module.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="attribute"> <dt id="importlib.abc.FileLoader.name"> <code class="descname">name</code><a class="headerlink" href="#importlib.abc.FileLoader.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the module the loader can handle.</p> </dd></dl> <dl class="attribute"> <dt id="importlib.abc.FileLoader.path"> <code class="descname">path</code><a class="headerlink" href="#importlib.abc.FileLoader.path" title="Permalink to this definition">¶</a></dt> <dd><p>Path to the file of the module.</p> </dd></dl> <dl class="method"> <dt id="importlib.abc.FileLoader.load_module"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.load_module" title="Permalink to this definition">¶</a></dt> <dd><p>Calls super’s <code class="docutils literal notranslate"><span class="pre">load_module()</span></code>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.FileLoader.get_filename"> <em class="property">abstractmethod </em><code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.get_filename" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <a class="reference internal" href="#importlib.abc.FileLoader.path" title="importlib.abc.FileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.abc.FileLoader.get_data"> <em class="property">abstractmethod </em><code class="descname">get_data</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.FileLoader.get_data" title="Permalink to this definition">¶</a></dt> <dd><p>Reads <em>path</em> as a binary file and returns the bytes from it.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.abc.SourceLoader"> <em class="property">class </em><code class="descclassname">importlib.abc.</code><code class="descname">SourceLoader</code><a class="headerlink" href="#importlib.abc.SourceLoader" title="Permalink to this definition">¶</a></dt> <dd><p>An abstract base class for implementing source (and optionally bytecode) file loading. The class inherits from both <a class="reference internal" href="#importlib.abc.ResourceLoader" title="importlib.abc.ResourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ResourceLoader</span></code></a> and <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExecutionLoader</span></code></a>, requiring the implementation of:</p> <ul class="simple"> <li><a class="reference internal" href="#importlib.abc.ResourceLoader.get_data" title="importlib.abc.ResourceLoader.get_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ResourceLoader.get_data()</span></code></a></li> <li><dl class="first docutils"> <dt><a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExecutionLoader.get_filename()</span></code></a></dt> <dd>Should only return the path to the source file; sourceless loading is not supported.</dd> </dl> </li> </ul> <p>The abstract methods defined by this class are to add optional bytecode file support. Not implementing these optional methods (or causing them to raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>) causes the loader to only work with source code. Implementing the methods allows the loader to work with source <em>and</em> bytecode files; it does not allow for <em>sourceless</em> loading where only bytecode is provided. Bytecode files are an optimization to speed up loading by removing the parsing step of Python’s compiler, and so no bytecode-specific API is exposed.</p> <dl class="method"> <dt id="importlib.abc.SourceLoader.path_stats"> <code class="descname">path_stats</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.path_stats" title="Permalink to this definition">¶</a></dt> <dd><p>Optional abstract method which returns a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> containing metadata about the specified path. Supported dictionary keys are:</p> <ul class="simple"> <li><code class="docutils literal notranslate"><span class="pre">'mtime'</span></code> (mandatory): an integer or floating-point number representing the modification time of the source code;</li> <li><code class="docutils literal notranslate"><span class="pre">'size'</span></code> (optional): the size in bytes of the source code.</li> </ul> <p>Any other keys in the dictionary are ignored, to allow for future extensions. If the path cannot be handled, <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.path_mtime"> <code class="descname">path_mtime</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.path_mtime" title="Permalink to this definition">¶</a></dt> <dd><p>Optional abstract method which returns the modification time for the specified path.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.3: </span>This method is deprecated in favour of <a class="reference internal" href="#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal notranslate"><span class="pre">path_stats()</span></code></a>. You don’t have to implement it, but it is still available for compatibility purposes. Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if the path cannot be handled.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> instead of <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a>.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.set_data"> <code class="descname">set_data</code><span class="sig-paren">(</span><em>path</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.set_data" title="Permalink to this definition">¶</a></dt> <dd><p>Optional abstract method which writes the specified bytes to a file path. Any intermediate directories which do not exist are to be created automatically.</p> <p>When writing to the path fails because the path is read-only (<a class="reference internal" href="errno.html#errno.EACCES" title="errno.EACCES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">errno.EACCES</span></code></a>/<a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>), do not propagate the exception.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>No longer raises <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> when called.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.get_code"> <code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.get_code" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.get_code" title="importlib.abc.InspectLoader.get_code"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.get_code()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.exec_module"> <code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.exec_module" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.load_module"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.load_module" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.load_module()</span></code></a>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.abc.SourceLoader.exec_module" title="importlib.abc.SourceLoader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> instead.</p> </div> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.get_source"> <code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.get_source" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.get_source" title="importlib.abc.InspectLoader.get_source"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.get_source()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.abc.SourceLoader.is_package"> <code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.abc.SourceLoader.is_package" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.InspectLoader.is_package" title="importlib.abc.InspectLoader.is_package"><code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.is_package()</span></code></a>. A module is determined to be a package if its file path (as provided by <a class="reference internal" href="#importlib.abc.ExecutionLoader.get_filename" title="importlib.abc.ExecutionLoader.get_filename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ExecutionLoader.get_filename()</span></code></a>) is a file named <code class="docutils literal notranslate"><span class="pre">__init__</span></code> when the file extension is removed <strong>and</strong> the module name itself does not end in <code class="docutils literal notranslate"><span class="pre">__init__</span></code>.</p> </dd></dl> </dd></dl> </div> <div class="section" id="module-importlib.machinery"> <span id="importlib-machinery-importers-and-path-hooks"></span><h2>31.5.4. <a class="reference internal" href="#module-importlib.machinery" title="importlib.machinery: Importers and path hooks"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.machinery</span></code></a> – Importers and path hooks<a class="headerlink" href="#module-importlib.machinery" title="Permalink to this headline">¶</a></h2> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/importlib/machinery.py">Lib/importlib/machinery.py</a></p> <hr class="docutils" /> <p>This module contains the various objects that help <a class="reference internal" href="../reference/simple_stmts.html#import"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a> find and load modules.</p> <dl class="attribute"> <dt id="importlib.machinery.SOURCE_SUFFIXES"> <code class="descclassname">importlib.machinery.</code><code class="descname">SOURCE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.SOURCE_SUFFIXES" title="Permalink to this definition">¶</a></dt> <dd><p>A list of strings representing the recognized file suffixes for source modules.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.DEBUG_BYTECODE_SUFFIXES"> <code class="descclassname">importlib.machinery.</code><code class="descname">DEBUG_BYTECODE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.DEBUG_BYTECODE_SUFFIXES" title="Permalink to this definition">¶</a></dt> <dd><p>A list of strings representing the file suffixes for non-optimized bytecode modules.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#importlib.machinery.BYTECODE_SUFFIXES" title="importlib.machinery.BYTECODE_SUFFIXES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p> </div> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES"> <code class="descclassname">importlib.machinery.</code><code class="descname">OPTIMIZED_BYTECODE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES" title="Permalink to this definition">¶</a></dt> <dd><p>A list of strings representing the file suffixes for optimized bytecode modules.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#importlib.machinery.BYTECODE_SUFFIXES" title="importlib.machinery.BYTECODE_SUFFIXES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">BYTECODE_SUFFIXES</span></code></a> instead.</p> </div> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.BYTECODE_SUFFIXES"> <code class="descclassname">importlib.machinery.</code><code class="descname">BYTECODE_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.BYTECODE_SUFFIXES" title="Permalink to this definition">¶</a></dt> <dd><p>A list of strings representing the recognized file suffixes for bytecode modules (including the leading dot).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>The value is no longer dependent on <code class="docutils literal notranslate"><span class="pre">__debug__</span></code>.</p> </div> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.EXTENSION_SUFFIXES"> <code class="descclassname">importlib.machinery.</code><code class="descname">EXTENSION_SUFFIXES</code><a class="headerlink" href="#importlib.machinery.EXTENSION_SUFFIXES" title="Permalink to this definition">¶</a></dt> <dd><p>A list of strings representing the recognized file suffixes for extension modules.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.machinery.all_suffixes"> <code class="descclassname">importlib.machinery.</code><code class="descname">all_suffixes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.all_suffixes" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a combined list of strings representing all file suffixes for modules recognized by the standard import machinery. This is a helper for code which simply needs to know if a filesystem path potentially refers to a module without needing any details on the kind of module (for example, <a class="reference internal" href="inspect.html#inspect.getmodulename" title="inspect.getmodulename"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.getmodulename()</span></code></a>).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> </dd></dl> <dl class="class"> <dt id="importlib.machinery.BuiltinImporter"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">BuiltinImporter</code><a class="headerlink" href="#importlib.machinery.BuiltinImporter" title="Permalink to this definition">¶</a></dt> <dd><p>An <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> for built-in modules. All known built-in modules are listed in <a class="reference internal" href="sys.html#sys.builtin_module_names" title="sys.builtin_module_names"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.builtin_module_names</span></code></a>. This class implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader</span></code></a> ABCs.</p> <p>Only class methods are defined by this class to alleviate the need for instantiation.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>As part of <span class="target" id="index-17"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a>, the builtin importer now implements <code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.create_module()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">Loader.exec_module()</span></code></p> </div> </dd></dl> <dl class="class"> <dt id="importlib.machinery.FrozenImporter"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">FrozenImporter</code><a class="headerlink" href="#importlib.machinery.FrozenImporter" title="Permalink to this definition">¶</a></dt> <dd><p>An <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a> for frozen modules. This class implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> and <a class="reference internal" href="#importlib.abc.InspectLoader" title="importlib.abc.InspectLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader</span></code></a> ABCs.</p> <p>Only class methods are defined by this class to alleviate the need for instantiation.</p> </dd></dl> <dl class="class"> <dt id="importlib.machinery.WindowsRegistryFinder"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">WindowsRegistryFinder</code><a class="headerlink" href="#importlib.machinery.WindowsRegistryFinder" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">Finder</span></a> for modules declared in the Windows registry. This class implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> ABC.</p> <p>Only class methods are defined by this class to alleviate the need for instantiation.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.6: </span>Use <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> configuration instead. Future versions of Python may not enable this finder by default.</p> </div> </dd></dl> <dl class="class"> <dt id="importlib.machinery.PathFinder"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">PathFinder</code><a class="headerlink" href="#importlib.machinery.PathFinder" title="Permalink to this definition">¶</a></dt> <dd><p>A <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">Finder</span></a> for <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> and package <code class="docutils literal notranslate"><span class="pre">__path__</span></code> attributes. This class implements the <a class="reference internal" href="#importlib.abc.MetaPathFinder" title="importlib.abc.MetaPathFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.MetaPathFinder</span></code></a> ABC.</p> <p>Only class methods are defined by this class to alleviate the need for instantiation.</p> <dl class="classmethod"> <dt id="importlib.machinery.PathFinder.find_spec"> <em class="property">classmethod </em><code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>path=None</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.find_spec" title="Permalink to this definition">¶</a></dt> <dd><p>Class method that attempts to find a <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for the module specified by <em>fullname</em> on <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> or, if defined, on <em>path</em>. For each path entry that is searched, <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> is checked. If a non-false object is found then it is used as the <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a> to look for the module being searched for. If no entry is found in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>, then <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> is searched for a finder for the path entry and, if found, is stored in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a> along with being queried about the module. If no finder is ever found then <code class="docutils literal notranslate"><span class="pre">None</span></code> is both stored in the cache and returned.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>If the current working directory – represented by an empty string – is no longer valid then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned but no value is cached in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>.</p> </div> </dd></dl> <dl class="classmethod"> <dt id="importlib.machinery.PathFinder.find_module"> <em class="property">classmethod </em><code class="descname">find_module</code><span class="sig-paren">(</span><em>fullname</em>, <em>path=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.find_module" title="Permalink to this definition">¶</a></dt> <dd><p>A legacy wrapper around <a class="reference internal" href="#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a>.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>Use <a class="reference internal" href="#importlib.machinery.PathFinder.find_spec" title="importlib.machinery.PathFinder.find_spec"><code class="xref py py-meth docutils literal notranslate"><span class="pre">find_spec()</span></code></a> instead.</p> </div> </dd></dl> <dl class="classmethod"> <dt id="importlib.machinery.PathFinder.invalidate_caches"> <em class="property">classmethod </em><code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.PathFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt> <dd><p>Calls <a class="reference internal" href="#importlib.abc.PathEntryFinder.invalidate_caches" title="importlib.abc.PathEntryFinder.invalidate_caches"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder.invalidate_caches()</span></code></a> on all finders stored in <a class="reference internal" href="sys.html#sys.path_importer_cache" title="sys.path_importer_cache"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_importer_cache</span></code></a>.</p> </dd></dl> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Calls objects in <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> with the current working directory for <code class="docutils literal notranslate"><span class="pre">''</span></code> (i.e. the empty string).</p> </div> </dd></dl> <dl class="class"> <dt id="importlib.machinery.FileFinder"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">FileFinder</code><span class="sig-paren">(</span><em>path</em>, <em>*loader_details</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder" title="Permalink to this definition">¶</a></dt> <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.PathEntryFinder" title="importlib.abc.PathEntryFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.PathEntryFinder</span></code></a> which caches results from the file system.</p> <p>The <em>path</em> argument is the directory for which the finder is in charge of searching.</p> <p>The <em>loader_details</em> argument is a variable number of 2-item tuples each containing a loader and a sequence of file suffixes the loader recognizes. The loaders are expected to be callables which accept two arguments of the module’s name and the path to the file found.</p> <p>The finder will cache the directory contents as necessary, making stat calls for each module search to verify the cache is not outdated. Because cache staleness relies upon the granularity of the operating system’s state information of the file system, there is a potential race condition of searching for a module, creating a new file, and then searching for the module the new file represents. If the operations happen fast enough to fit within the granularity of stat calls, then the module search will fail. To prevent this from happening, when you create a module dynamically, make sure to call <a class="reference internal" href="#importlib.invalidate_caches" title="importlib.invalidate_caches"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.invalidate_caches()</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="attribute"> <dt id="importlib.machinery.FileFinder.path"> <code class="descname">path</code><a class="headerlink" href="#importlib.machinery.FileFinder.path" title="Permalink to this definition">¶</a></dt> <dd><p>The path the finder will search in.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.FileFinder.find_spec"> <code class="descname">find_spec</code><span class="sig-paren">(</span><em>fullname</em>, <em>target=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.find_spec" title="Permalink to this definition">¶</a></dt> <dd><p>Attempt to find the spec to handle <em>fullname</em> within <a class="reference internal" href="#importlib.machinery.FileFinder.path" title="importlib.machinery.FileFinder.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.machinery.FileFinder.find_loader"> <code class="descname">find_loader</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.find_loader" title="Permalink to this definition">¶</a></dt> <dd><p>Attempt to find the loader to handle <em>fullname</em> within <a class="reference internal" href="#importlib.machinery.FileFinder.path" title="importlib.machinery.FileFinder.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.FileFinder.invalidate_caches"> <code class="descname">invalidate_caches</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.invalidate_caches" title="Permalink to this definition">¶</a></dt> <dd><p>Clear out the internal cache.</p> </dd></dl> <dl class="classmethod"> <dt id="importlib.machinery.FileFinder.path_hook"> <em class="property">classmethod </em><code class="descname">path_hook</code><span class="sig-paren">(</span><em>*loader_details</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.FileFinder.path_hook" title="Permalink to this definition">¶</a></dt> <dd><p>A class method which returns a closure for use on <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a>. An instance of <a class="reference internal" href="#importlib.machinery.FileFinder" title="importlib.machinery.FileFinder"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileFinder</span></code></a> is returned by the closure using the path argument given to the closure directly and <em>loader_details</em> indirectly.</p> <p>If the argument to the closure is not an existing directory, <a class="reference internal" href="exceptions.html#ImportError" title="ImportError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImportError</span></code></a> is raised.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.machinery.SourceFileLoader"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">SourceFileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader" title="Permalink to this definition">¶</a></dt> <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader" title="importlib.abc.SourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader</span></code></a> by subclassing <a class="reference internal" href="#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.FileLoader</span></code></a> and providing some concrete implementations of other methods.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="attribute"> <dt id="importlib.machinery.SourceFileLoader.name"> <code class="descname">name</code><a class="headerlink" href="#importlib.machinery.SourceFileLoader.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the module that this loader will handle.</p> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.SourceFileLoader.path"> <code class="descname">path</code><a class="headerlink" href="#importlib.machinery.SourceFileLoader.path" title="Permalink to this definition">¶</a></dt> <dd><p>The path to the source file.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourceFileLoader.is_package"> <code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.is_package" title="Permalink to this definition">¶</a></dt> <dd><p>Return true if <a class="reference internal" href="#importlib.machinery.SourceFileLoader.path" title="importlib.machinery.SourceFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a> appears to be for a package.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourceFileLoader.path_stats"> <code class="descname">path_stats</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.path_stats" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader.path_stats" title="importlib.abc.SourceLoader.path_stats"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader.path_stats()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourceFileLoader.set_data"> <code class="descname">set_data</code><span class="sig-paren">(</span><em>path</em>, <em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.set_data" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.SourceLoader.set_data" title="importlib.abc.SourceLoader.set_data"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader.set_data()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourceFileLoader.load_module"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourceFileLoader.load_module" title="Permalink to this definition">¶</a></dt> <dd><p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> where specifying the name of the module to load is optional.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.6: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.exec_module()</span></code></a> instead.</p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.machinery.SourcelessFileLoader"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">SourcelessFileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader" title="Permalink to this definition">¶</a></dt> <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.FileLoader" title="importlib.abc.FileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.FileLoader</span></code></a> which can import bytecode files (i.e. no source code files exist).</p> <p>Please note that direct use of bytecode files (and thus not source code files) inhibits your modules from being usable by all Python implementations or new versions of Python which change the bytecode format.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="attribute"> <dt id="importlib.machinery.SourcelessFileLoader.name"> <code class="descname">name</code><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the module the loader will handle.</p> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.SourcelessFileLoader.path"> <code class="descname">path</code><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.path" title="Permalink to this definition">¶</a></dt> <dd><p>The path to the bytecode file.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourcelessFileLoader.is_package"> <code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.is_package" title="Permalink to this definition">¶</a></dt> <dd><p>Determines if the module is a package based on <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.path" title="importlib.machinery.SourcelessFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourcelessFileLoader.get_code"> <code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.get_code" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the code object for <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.name" title="importlib.machinery.SourcelessFileLoader.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a> created from <a class="reference internal" href="#importlib.machinery.SourcelessFileLoader.path" title="importlib.machinery.SourcelessFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourcelessFileLoader.get_source"> <code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.get_source" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> as bytecode files have no source when this loader is used.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.SourcelessFileLoader.load_module"> <code class="descname">load_module</code><span class="sig-paren">(</span><em>name=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.SourcelessFileLoader.load_module" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Concrete implementation of <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> where specifying the name of the module to load is optional.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.6: </span>Use <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.exec_module()</span></code></a> instead.</p> </div> </dd></dl> <dl class="class"> <dt id="importlib.machinery.ExtensionFileLoader"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">ExtensionFileLoader</code><span class="sig-paren">(</span><em>fullname</em>, <em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader" title="Permalink to this definition">¶</a></dt> <dd><p>A concrete implementation of <a class="reference internal" href="#importlib.abc.ExecutionLoader" title="importlib.abc.ExecutionLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.ExecutionLoader</span></code></a> for extension modules.</p> <p>The <em>fullname</em> argument specifies the name of the module the loader is to support. The <em>path</em> argument is the path to the extension module’s file.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> <dl class="attribute"> <dt id="importlib.machinery.ExtensionFileLoader.name"> <code class="descname">name</code><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.name" title="Permalink to this definition">¶</a></dt> <dd><p>Name of the module the loader supports.</p> </dd></dl> <dl class="attribute"> <dt id="importlib.machinery.ExtensionFileLoader.path"> <code class="descname">path</code><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.path" title="Permalink to this definition">¶</a></dt> <dd><p>Path to the extension module.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.ExtensionFileLoader.create_module"> <code class="descname">create_module</code><span class="sig-paren">(</span><em>spec</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.create_module" title="Permalink to this definition">¶</a></dt> <dd><p>Creates the module object from the given specification in accordance with <span class="target" id="index-18"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.machinery.ExtensionFileLoader.exec_module"> <code class="descname">exec_module</code><span class="sig-paren">(</span><em>module</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.exec_module" title="Permalink to this definition">¶</a></dt> <dd><p>Initializes the given module object in accordance with <span class="target" id="index-19"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0489"><strong>PEP 489</strong></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> </dd></dl> <dl class="method"> <dt id="importlib.machinery.ExtensionFileLoader.is_package"> <code class="descname">is_package</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.is_package" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if the file path points to a package’s <code class="docutils literal notranslate"><span class="pre">__init__</span></code> module based on <a class="reference internal" href="#importlib.machinery.EXTENSION_SUFFIXES" title="importlib.machinery.EXTENSION_SUFFIXES"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EXTENSION_SUFFIXES</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.ExtensionFileLoader.get_code"> <code class="descname">get_code</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_code" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> as extension modules lack a code object.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.ExtensionFileLoader.get_source"> <code class="descname">get_source</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_source" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <code class="docutils literal notranslate"><span class="pre">None</span></code> as extension modules do not have source code.</p> </dd></dl> <dl class="method"> <dt id="importlib.machinery.ExtensionFileLoader.get_filename"> <code class="descname">get_filename</code><span class="sig-paren">(</span><em>fullname</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ExtensionFileLoader.get_filename" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <a class="reference internal" href="#importlib.machinery.ExtensionFileLoader.path" title="importlib.machinery.ExtensionFileLoader.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">path</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> </dd></dl> <dl class="class"> <dt id="importlib.machinery.ModuleSpec"> <em class="property">class </em><code class="descclassname">importlib.machinery.</code><code class="descname">ModuleSpec</code><span class="sig-paren">(</span><em>name</em>, <em>loader</em>, <em>*</em>, <em>origin=None</em>, <em>loader_state=None</em>, <em>is_package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.machinery.ModuleSpec" title="Permalink to this definition">¶</a></dt> <dd><p>A specification for a module’s import-system-related state. This is typically exposed as the module’s <code class="docutils literal notranslate"><span class="pre">__spec__</span></code> attribute. In the descriptions below, the names in parentheses give the corresponding attribute available directly on the module object. E.g. <code class="docutils literal notranslate"><span class="pre">module.__spec__.origin</span> <span class="pre">==</span> <span class="pre">module.__file__</span></code>. Note however that while the <em>values</em> are usually equivalent, they can differ since there is no synchronization between the two objects. Thus it is possible to update the module’s <code class="docutils literal notranslate"><span class="pre">__path__</span></code> at runtime, and this will not be automatically reflected in <code class="docutils literal notranslate"><span class="pre">__spec__.submodule_search_locations</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.name"> <code class="descname">name</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.name" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>(<code class="docutils literal notranslate"><span class="pre">__name__</span></code>)</p> <p>A string for the fully-qualified name of the module.</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.loader"> <code class="descname">loader</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.loader" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>(<code class="docutils literal notranslate"><span class="pre">__loader__</span></code>)</p> <p>The loader to use for loading. For namespace packages this should be set to <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.origin"> <code class="descname">origin</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.origin" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>(<code class="docutils literal notranslate"><span class="pre">__file__</span></code>)</p> <p>Name of the place from which the module is loaded, e.g. “builtin” for built-in modules and the filename for modules loaded from source. Normally “origin” should be set, but it may be <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) which indicates it is unspecified.</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.submodule_search_locations"> <code class="descname">submodule_search_locations</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.submodule_search_locations" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>(<code class="docutils literal notranslate"><span class="pre">__path__</span></code>)</p> <p>List of strings for where to find submodules, if a package (<code class="docutils literal notranslate"><span class="pre">None</span></code> otherwise).</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.loader_state"> <code class="descname">loader_state</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.loader_state" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Container of extra module-specific data for use during loading (or <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.cached"> <code class="descname">cached</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.cached" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>(<code class="docutils literal notranslate"><span class="pre">__cached__</span></code>)</p> <p>String for where the compiled module should be stored (or <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.parent"> <code class="descname">parent</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.parent" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>(<code class="docutils literal notranslate"><span class="pre">__package__</span></code>)</p> <p>(Read-only) Fully-qualified name of the package to which the module belongs as a submodule (or <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p> <dl class="attribute"> <dt id="importlib.machinery.ModuleSpec.has_location"> <code class="descname">has_location</code><a class="headerlink" href="#importlib.machinery.ModuleSpec.has_location" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <p>Boolean indicating whether or not the module’s “origin” attribute refers to a loadable location.</p> </dd></dl> </div> <div class="section" id="module-importlib.util"> <span id="importlib-util-utility-code-for-importers"></span><h2>31.5.5. <a class="reference internal" href="#module-importlib.util" title="importlib.util: Utility code for importers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.util</span></code></a> – Utility code for importers<a class="headerlink" href="#module-importlib.util" title="Permalink to this headline">¶</a></h2> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.6/Lib/importlib/util.py">Lib/importlib/util.py</a></p> <hr class="docutils" /> <p>This module contains the various objects that help in the construction of an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>.</p> <dl class="attribute"> <dt id="importlib.util.MAGIC_NUMBER"> <code class="descclassname">importlib.util.</code><code class="descname">MAGIC_NUMBER</code><a class="headerlink" href="#importlib.util.MAGIC_NUMBER" title="Permalink to this definition">¶</a></dt> <dd><p>The bytes which represent the bytecode version number. If you need help with loading/writing bytecode then consider <a class="reference internal" href="#importlib.abc.SourceLoader" title="importlib.abc.SourceLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.abc.SourceLoader</span></code></a>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.cache_from_source"> <code class="descclassname">importlib.util.</code><code class="descname">cache_from_source</code><span class="sig-paren">(</span><em>path</em>, <em>debug_override=None</em>, <em>*</em>, <em>optimization=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.cache_from_source" title="Permalink to this definition">¶</a></dt> <dd><p>Return the <span class="target" id="index-20"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a>/<span class="target" id="index-21"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> path to the byte-compiled file associated with the source <em>path</em>. For example, if <em>path</em> is <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code> the return value would be <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> for Python 3.2. The <code class="docutils literal notranslate"><span class="pre">cpython-32</span></code> string comes from the current magic tag (see <code class="xref py py-func docutils literal notranslate"><span class="pre">get_tag()</span></code>; if <code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is not defined then <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> will be raised).</p> <p>The <em>optimization</em> parameter is used to specify the optimization level of the bytecode file. An empty string represents no optimization, so <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code> with an <em>optimization</em> of <code class="docutils literal notranslate"><span class="pre">''</span></code> will result in a bytecode path of <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code>. <code class="docutils literal notranslate"><span class="pre">None</span></code> causes the interpter’s optimization level to be used. Any other value’s string representation being used, so <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code> with an <em>optimization</em> of <code class="docutils literal notranslate"><span class="pre">2</span></code> will lead to the bytecode path of <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.opt-2.pyc</span></code>. The string representation of <em>optimization</em> can only be alphanumeric, else <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p> <p>The <em>debug_override</em> parameter is deprecated and can be used to override the system’s value for <code class="docutils literal notranslate"><span class="pre">__debug__</span></code>. A <code class="docutils literal notranslate"><span class="pre">True</span></code> value is the equivalent of setting <em>optimization</em> to the empty string. A <code class="docutils literal notranslate"><span class="pre">False</span></code> value is the same as setting <em>optimization</em> to <code class="docutils literal notranslate"><span class="pre">1</span></code>. If both <em>debug_override</em> an <em>optimization</em> are not <code class="docutils literal notranslate"><span class="pre">None</span></code> then <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.5: </span>The <em>optimization</em> parameter was added and the <em>debug_override</em> parameter was deprecated.</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.source_from_cache"> <code class="descclassname">importlib.util.</code><code class="descname">source_from_cache</code><span class="sig-paren">(</span><em>path</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.source_from_cache" title="Permalink to this definition">¶</a></dt> <dd><p>Given the <em>path</em> to a <span class="target" id="index-22"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> file name, return the associated source code file path. For example, if <em>path</em> is <code class="docutils literal notranslate"><span class="pre">/foo/bar/__pycache__/baz.cpython-32.pyc</span></code> the returned path would be <code class="docutils literal notranslate"><span class="pre">/foo/bar/baz.py</span></code>. <em>path</em> need not exist, however if it does not conform to <span class="target" id="index-23"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3147"><strong>PEP 3147</strong></a> or <span class="target" id="index-24"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0488"><strong>PEP 488</strong></a> format, a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is raised. If <code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.implementation.cache_tag</span></code> is not defined, <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotImplementedError</span></code></a> is raised.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.decode_source"> <code class="descclassname">importlib.util.</code><code class="descname">decode_source</code><span class="sig-paren">(</span><em>source_bytes</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.decode_source" title="Permalink to this definition">¶</a></dt> <dd><p>Decode the given bytes representing source code and return it as a string with universal newlines (as required by <a class="reference internal" href="#importlib.abc.InspectLoader.get_source" title="importlib.abc.InspectLoader.get_source"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader.get_source()</span></code></a>).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.resolve_name"> <code class="descclassname">importlib.util.</code><code class="descname">resolve_name</code><span class="sig-paren">(</span><em>name</em>, <em>package</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.resolve_name" title="Permalink to this definition">¶</a></dt> <dd><p>Resolve a relative module name to an absolute one.</p> <p>If <strong>name</strong> has no leading dots, then <strong>name</strong> is simply returned. This allows for usage such as <code class="docutils literal notranslate"><span class="pre">importlib.util.resolve_name('sys',</span> <span class="pre">__package__)</span></code> without doing a check to see if the <strong>package</strong> argument is needed.</p> <p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if <strong>name</strong> is a relative module name but package is a false value (e.g. <code class="docutils literal notranslate"><span class="pre">None</span></code> or the empty string). <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is also raised a relative name would escape its containing package (e.g. requesting <code class="docutils literal notranslate"><span class="pre">..bacon</span></code> from within the <code class="docutils literal notranslate"><span class="pre">spam</span></code> package).</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.3.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.find_spec"> <code class="descclassname">importlib.util.</code><code class="descname">find_spec</code><span class="sig-paren">(</span><em>name</em>, <em>package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.find_spec" title="Permalink to this definition">¶</a></dt> <dd><p>Find the <a class="reference internal" href="../glossary.html#term-module-spec"><span class="xref std std-term">spec</span></a> for a module, optionally relative to the specified <strong>package</strong> name. If the module is in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, then <code class="docutils literal notranslate"><span class="pre">sys.modules[name].__spec__</span></code> is returned (unless the spec would be <code class="docutils literal notranslate"><span class="pre">None</span></code> or is not set, in which case <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised). Otherwise a search using <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> is done. <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned if no spec is found.</p> <p>If <strong>name</strong> is for a submodule (contains a dot), the parent module is automatically imported.</p> <p><strong>name</strong> and <strong>package</strong> work the same as for <code class="xref py py-func docutils literal notranslate"><span class="pre">import_module()</span></code>.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.module_from_spec"> <code class="descclassname">importlib.util.</code><code class="descname">module_from_spec</code><span class="sig-paren">(</span><em>spec</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.module_from_spec" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new module based on <strong>spec</strong> and <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">spec.loader.create_module</span></code></a>.</p> <p>If <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">spec.loader.create_module</span></code></a> does not return <code class="docutils literal notranslate"><span class="pre">None</span></code>, then any pre-existing attributes will not be reset. Also, no <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> will be raised if triggered while accessing <strong>spec</strong> or setting an attribute on the module.</p> <p>This function is preferred over using <a class="reference internal" href="types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal notranslate"><span class="pre">types.ModuleType</span></code></a> to create a new module as <strong>spec</strong> is used to set as many import-controlled attributes on the module as possible.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.module_for_loader"> <code class="descclassname">@</code><code class="descclassname">importlib.util.</code><code class="descname">module_for_loader</code><a class="headerlink" href="#importlib.util.module_for_loader" title="Permalink to this definition">¶</a></dt> <dd><p>A <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> for <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> to handle selecting the proper module object to load with. The decorated method is expected to have a call signature taking two positional arguments (e.g. <code class="docutils literal notranslate"><span class="pre">load_module(self,</span> <span class="pre">module)</span></code>) for which the second argument will be the module <strong>object</strong> to be used by the loader. Note that the decorator will not work on static methods because of the assumption of two arguments.</p> <p>The decorated method will take in the <strong>name</strong> of the module to be loaded as expected for a <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a>. If the module is not found in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> then a new one is constructed. Regardless of where the module came from, <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> set to <strong>self</strong> and <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a> is set based on what <a class="reference internal" href="#importlib.abc.InspectLoader.is_package" title="importlib.abc.InspectLoader.is_package"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.InspectLoader.is_package()</span></code></a> returns (if available). These attributes are set unconditionally to support reloading.</p> <p>If an exception is raised by the decorated method and a module was added to <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>, then the module will be removed to prevent a partially initialized module from being in left in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a>. If the module was already in <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.modules</span></code></a> then it is left alone.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.3: </span><a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> and <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a> are automatically set (when possible).</p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Set <a class="reference internal" href="../reference/import.html#__name__" title="__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a>, <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a> unconditionally to support reloading.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery now directly performs all the functionality provided by this function.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.set_loader"> <code class="descclassname">@</code><code class="descclassname">importlib.util.</code><code class="descname">set_loader</code><a class="headerlink" href="#importlib.util.set_loader" title="Permalink to this definition">¶</a></dt> <dd><p>A <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> for <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> to set the <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> attribute on the returned module. If the attribute is already set the decorator does nothing. It is assumed that the first positional argument to the wrapped method (i.e. <code class="docutils literal notranslate"><span class="pre">self</span></code>) is what <a class="reference internal" href="../reference/import.html#__loader__" title="__loader__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__loader__</span></code></a> should be set to.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.4: </span>Set <code class="docutils literal notranslate"><span class="pre">__loader__</span></code> if set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, as if the attribute does not exist.</p> </div> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery takes care of this automatically.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.set_package"> <code class="descclassname">@</code><code class="descclassname">importlib.util.</code><code class="descname">set_package</code><a class="headerlink" href="#importlib.util.set_package" title="Permalink to this definition">¶</a></dt> <dd><p>A <a class="reference internal" href="../glossary.html#term-decorator"><span class="xref std std-term">decorator</span></a> for <a class="reference internal" href="#importlib.abc.Loader.load_module" title="importlib.abc.Loader.load_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">importlib.abc.Loader.load_module()</span></code></a> to set the <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a> attribute on the returned module. If <a class="reference internal" href="../reference/import.html#__package__" title="__package__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__package__</span></code></a> is set and has a value other than <code class="docutils literal notranslate"><span class="pre">None</span></code> it will not be changed.</p> <div class="deprecated"> <p><span class="versionmodified">Deprecated since version 3.4: </span>The import machinery takes care of this automatically.</p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.spec_from_loader"> <code class="descclassname">importlib.util.</code><code class="descname">spec_from_loader</code><span class="sig-paren">(</span><em>name</em>, <em>loader</em>, <em>*</em>, <em>origin=None</em>, <em>is_package=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.spec_from_loader" title="Permalink to this definition">¶</a></dt> <dd><p>A factory function for creating a <code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code> instance based on a loader. The parameters have the same meaning as they do for ModuleSpec. The function uses available <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> APIs, such as <code class="xref py py-meth docutils literal notranslate"><span class="pre">InspectLoader.is_package()</span></code>, to fill in any missing information on the spec.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> </dd></dl> <dl class="function"> <dt id="importlib.util.spec_from_file_location"> <code class="descclassname">importlib.util.</code><code class="descname">spec_from_file_location</code><span class="sig-paren">(</span><em>name</em>, <em>location</em>, <em>*</em>, <em>loader=None</em>, <em>submodule_search_locations=None</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.spec_from_file_location" title="Permalink to this definition">¶</a></dt> <dd><p>A factory function for creating a <code class="xref py py-class docutils literal notranslate"><span class="pre">ModuleSpec</span></code> instance based on the path to a file. Missing information will be filled in on the spec by making use of loader APIs and by the implication that the module will be file-based.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 3.4.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd></dl> <dl class="class"> <dt id="importlib.util.LazyLoader"> <em class="property">class </em><code class="descclassname">importlib.util.</code><code class="descname">LazyLoader</code><span class="sig-paren">(</span><em>loader</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.LazyLoader" title="Permalink to this definition">¶</a></dt> <dd><p>A class which postpones the execution of the loader of a module until the module has an attribute accessed.</p> <p>This class <strong>only</strong> works with loaders that define <a class="reference internal" href="#importlib.abc.Loader.exec_module" title="importlib.abc.Loader.exec_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">exec_module()</span></code></a> as control over what module type is used for the module is required. For those same reasons, the loader’s <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a> method must return <code class="docutils literal notranslate"><span class="pre">None</span></code> or a type for which its <code class="docutils literal notranslate"><span class="pre">__class__</span></code> attribute can be mutated along with not using <a class="reference internal" href="../glossary.html#term-slots"><span class="xref std std-term">slots</span></a>. Finally, modules which substitute the object placed into <a class="reference internal" href="sys.html#sys.modules" title="sys.modules"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.modules</span></code></a> will not work as there is no way to properly replace the module references throughout the interpreter safely; <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if such a substitution is detected.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">For projects where startup time is critical, this class allows for potentially minimizing the cost of loading a module if it is never used. For projects where startup time is not essential then use of this class is <strong>heavily</strong> discouraged due to error messages created during loading being postponed and thus occurring out of context.</p> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 3.5.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 3.6: </span>Began calling <a class="reference internal" href="#importlib.abc.Loader.create_module" title="importlib.abc.Loader.create_module"><code class="xref py py-meth docutils literal notranslate"><span class="pre">create_module()</span></code></a>, removing the compatibility warning for <a class="reference internal" href="#importlib.machinery.BuiltinImporter" title="importlib.machinery.BuiltinImporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.BuiltinImporter</span></code></a> and <a class="reference internal" href="#importlib.machinery.ExtensionFileLoader" title="importlib.machinery.ExtensionFileLoader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.machinery.ExtensionFileLoader</span></code></a>.</p> </div> <dl class="classmethod"> <dt id="importlib.util.LazyLoader.factory"> <em class="property">classmethod </em><code class="descname">factory</code><span class="sig-paren">(</span><em>loader</em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.util.LazyLoader.factory" title="Permalink to this definition">¶</a></dt> <dd><p>A static method which returns a callable that creates a lazy loader. This is meant to be used in situations where the loader is passed by class instead of by instance.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">suffixes</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SOURCE_SUFFIXES</span> <span class="n">loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SourceFileLoader</span> <span class="n">lazy_loader</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">LazyLoader</span><span class="o">.</span><span class="n">factory</span><span class="p">(</span><span class="n">loader</span><span class="p">)</span> <span class="n">finder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">FileFinder</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="p">(</span><span class="n">lazy_loader</span><span class="p">,</span> <span class="n">suffixes</span><span class="p">))</span> </pre></div> </div> </dd></dl> </dd></dl> </div> <div class="section" id="examples"> <span id="importlib-examples"></span><h2>31.5.6. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2> <div class="section" id="importing-programmatically"> <h3>31.5.6.1. Importing programmatically<a class="headerlink" href="#importing-programmatically" title="Permalink to this headline">¶</a></h3> <p>To programmatically import a module, use <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a>.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib</span> <span class="n">itertools</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">import_module</span><span class="p">(</span><span class="s1">'itertools'</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="checking-if-a-module-can-be-imported"> <h3>31.5.6.2. Checking if a module can be imported<a class="headerlink" href="#checking-if-a-module-can-be-imported" title="Permalink to this headline">¶</a></h3> <p>If you need to find out if a module can be imported without actually doing the import, then you should use <a class="reference internal" href="#importlib.util.find_spec" title="importlib.util.find_spec"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.util.find_spec()</span></code></a>.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.util</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="c1"># For illustrative purposes.</span> <span class="n">name</span> <span class="o">=</span> <span class="s1">'itertools'</span> <span class="n">spec</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">find_spec</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="k">if</span> <span class="n">spec</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s2">"can't find the itertools module"</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="c1"># If you chose to perform the actual import ...</span> <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span> <span class="c1"># Adding the module to sys.modules is optional.</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span> </pre></div> </div> </div> <div class="section" id="importing-a-source-file-directly"> <h3>31.5.6.3. Importing a source file directly<a class="headerlink" href="#importing-a-source-file-directly" title="Permalink to this headline">¶</a></h3> <p>To import a Python source file directly, use the following recipe (Python 3.4 and newer only):</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.util</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="c1"># For illustrative purposes.</span> <span class="kn">import</span> <span class="nn">tokenize</span> <span class="n">file_path</span> <span class="o">=</span> <span class="n">tokenize</span><span class="o">.</span><span class="vm">__file__</span> <span class="n">module_name</span> <span class="o">=</span> <span class="n">tokenize</span><span class="o">.</span><span class="vm">__name__</span> <span class="n">spec</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">spec_from_file_location</span><span class="p">(</span><span class="n">module_name</span><span class="p">,</span> <span class="n">file_path</span><span class="p">)</span> <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span> <span class="c1"># Optional; only necessary if you want to be able to import the module</span> <span class="c1"># by name later.</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">module_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span> </pre></div> </div> </div> <div class="section" id="setting-up-an-importer"> <h3>31.5.6.4. Setting up an importer<a class="headerlink" href="#setting-up-an-importer" title="Permalink to this headline">¶</a></h3> <p>For deep customizations of import, you typically want to implement an <a class="reference internal" href="../glossary.html#term-importer"><span class="xref std std-term">importer</span></a>. This means managing both the <a class="reference internal" href="../glossary.html#term-finder"><span class="xref std std-term">finder</span></a> and <a class="reference internal" href="../glossary.html#term-loader"><span class="xref std std-term">loader</span></a> side of things. For finders there are two flavours to choose from depending on your needs: a <a class="reference internal" href="../glossary.html#term-meta-path-finder"><span class="xref std std-term">meta path finder</span></a> or a <a class="reference internal" href="../glossary.html#term-path-entry-finder"><span class="xref std std-term">path entry finder</span></a>. The former is what you would put on <a class="reference internal" href="sys.html#sys.meta_path" title="sys.meta_path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.meta_path</span></code></a> while the latter is what you create using a <a class="reference internal" href="../glossary.html#term-path-entry-hook"><span class="xref std std-term">path entry hook</span></a> on <a class="reference internal" href="sys.html#sys.path_hooks" title="sys.path_hooks"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path_hooks</span></code></a> which works with <a class="reference internal" href="sys.html#sys.path" title="sys.path"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.path</span></code></a> entries to potentially create a finder. This example will show you how to register your own importers so that import will use them (for creating an importer for yourself, read the documentation for the appropriate classes defined within this package):</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.machinery</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="c1"># For illustrative purposes only.</span> <span class="n">SpamMetaPathFinder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">PathFinder</span> <span class="n">SpamPathEntryFinder</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">FileFinder</span> <span class="n">loader_details</span> <span class="o">=</span> <span class="p">(</span><span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SourceFileLoader</span><span class="p">,</span> <span class="n">importlib</span><span class="o">.</span><span class="n">machinery</span><span class="o">.</span><span class="n">SOURCE_SUFFIXES</span><span class="p">)</span> <span class="c1"># Setting up a meta path finder.</span> <span class="c1"># Make sure to put the finder in the proper location in the list in terms of</span> <span class="c1"># priority.</span> <span class="n">sys</span><span class="o">.</span><span class="n">meta_path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SpamMetaPathFinder</span><span class="p">)</span> <span class="c1"># Setting up a path entry finder.</span> <span class="c1"># Make sure to put the path hook in the proper location in the list in terms</span> <span class="c1"># of priority.</span> <span class="n">sys</span><span class="o">.</span><span class="n">path_hooks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">SpamPathEntryFinder</span><span class="o">.</span><span class="n">path_hook</span><span class="p">(</span><span class="n">loader_details</span><span class="p">))</span> </pre></div> </div> </div> <div class="section" id="approximating-importlib-import-module"> <h3>31.5.6.5. Approximating <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a><a class="headerlink" href="#approximating-importlib-import-module" title="Permalink to this headline">¶</a></h3> <p>Import itself is implemented in Python code, making it possible to expose most of the import machinery through importlib. The following helps illustrate the various APIs that importlib exposes by providing an approximate implementation of <a class="reference internal" href="#importlib.import_module" title="importlib.import_module"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a> (Python 3.4 and newer for the importlib usage, Python 3.6 and newer for other parts of the code).</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">importlib.util</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="k">def</span> <span class="nf">import_module</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">package</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span> <span class="sd">"""An approximate implementation of import."""</span> <span class="n">absolute_name</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">resolve_name</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">package</span><span class="p">)</span> <span class="k">try</span><span class="p">:</span> <span class="k">return</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">absolute_name</span><span class="p">]</span> <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span> <span class="k">pass</span> <span class="n">path</span> <span class="o">=</span> <span class="kc">None</span> <span class="k">if</span> <span class="s1">'.'</span> <span class="ow">in</span> <span class="n">absolute_name</span><span class="p">:</span> <span class="n">parent_name</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">child_name</span> <span class="o">=</span> <span class="n">absolute_name</span><span class="o">.</span><span class="n">rpartition</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)</span> <span class="n">parent_module</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">parent_name</span><span class="p">)</span> <span class="n">path</span> <span class="o">=</span> <span class="n">parent_module</span><span class="o">.</span><span class="n">spec</span><span class="o">.</span><span class="n">submodule_search_locations</span> <span class="k">for</span> <span class="n">finder</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">meta_path</span><span class="p">:</span> <span class="n">spec</span> <span class="o">=</span> <span class="n">finder</span><span class="o">.</span><span class="n">find_spec</span><span class="p">(</span><span class="n">absolute_name</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span> <span class="k">if</span> <span class="n">spec</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="k">break</span> <span class="k">else</span><span class="p">:</span> <span class="k">raise</span> <span class="ne">ImportError</span><span class="p">(</span><span class="n">f</span><span class="s1">'No module named </span><span class="si">{absolute_name!r}</span><span class="s1">'</span><span class="p">)</span> <span class="n">module</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span> <span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">module</span><span class="p">)</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">absolute_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">module</span> <span class="k">if</span> <span class="n">path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> <span class="nb">setattr</span><span class="p">(</span><span class="n">parent_module</span><span class="p">,</span> <span class="n">child_name</span><span class="p">,</span> <span class="n">module</span><span class="p">)</span> <span class="k">return</span> <span class="n">module</span> </pre></div> </div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">31.5. <code class="docutils literal notranslate"><span class="pre">importlib</span></code> — The implementation of <code class="docutils literal notranslate"><span class="pre">import</span></code></a><ul> <li><a class="reference internal" href="#introduction">31.5.1. Introduction</a></li> <li><a class="reference internal" href="#functions">31.5.2. Functions</a></li> <li><a class="reference internal" href="#module-importlib.abc">31.5.3. <code class="docutils literal notranslate"><span class="pre">importlib.abc</span></code> – Abstract base classes related to import</a></li> <li><a class="reference internal" href="#module-importlib.machinery">31.5.4. <code class="docutils literal notranslate"><span class="pre">importlib.machinery</span></code> – Importers and path hooks</a></li> <li><a class="reference internal" href="#module-importlib.util">31.5.5. <code class="docutils literal notranslate"><span class="pre">importlib.util</span></code> – Utility code for importers</a></li> <li><a class="reference internal" href="#examples">31.5.6. Examples</a><ul> <li><a class="reference internal" href="#importing-programmatically">31.5.6.1. Importing programmatically</a></li> <li><a class="reference internal" href="#checking-if-a-module-can-be-imported">31.5.6.2. Checking if a module can be imported</a></li> <li><a class="reference internal" href="#importing-a-source-file-directly">31.5.6.3. Importing a source file directly</a></li> <li><a class="reference internal" href="#setting-up-an-importer">31.5.6.4. Setting up an importer</a></li> <li><a class="reference internal" href="#approximating-importlib-import-module">31.5.6.5. Approximating <code class="docutils literal notranslate"><span class="pre">importlib.import_module()</span></code></a></li> </ul> </li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="runpy.html" title="previous chapter">31.4. <code class="docutils literal notranslate"><span class="pre">runpy</span></code> — Locating and executing Python modules</a></p> <h4>Next topic</h4> <p class="topless"><a href="language.html" title="next chapter">32. Python Language Services</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.6/Doc/library/importlib.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="language.html" title="32. Python Language Services" >next</a> |</li> <li class="right" > <a href="runpy.html" title="31.4. runpy — Locating and executing Python modules" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.6.7 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="modules.html" >31. Importing Modules</a> »</li> <li class="right"> <div class="inline-search" style="display: none" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('.inline-search').show(0);</script> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Dec 18, 2023. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.6. </div> </body> </html>
Save
cmd:
run