Repository navigation
Expand file tree
/
Copy pathModules_and_Packages.html
More file actions
693 lines (548 loc) · 62.5 KB
/
Copy pathModules_and_Packages.html
File metadata and controls
693 lines (548 loc) · 62.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta content="Topic: Understanding imports modules and packages, Difficulty: Easy, Category: Section" name="description" />
<meta content="custom package, module, script, import, setup, pip, conda, relative import, absolute import" name="keywords" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Import: Modules and Packages — Python Like You Mean It</title>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<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/language_data.js"></script>
<script type="text/javascript" src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<script type="text/javascript" src="../_static/gtag.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Module 6: Testing Our Code" href="../module_6.html" />
<link rel="prev" title="Working with Files" href="WorkingWithFiles.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<div class="version">
1.4
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Table of Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_1.html">Module 1: Getting Started with Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_2.html">Module 2: The Essentials of Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_2_problems.html">Module 2: Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">Module 3: The Essentials of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3_problems.html">Module 3: Problems</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_4.html">Module 4: Object Oriented Programming</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_5.html">Module 5: Odds and Ends</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="Writing_Good_Code.html">Writing Good Code</a></li>
<li class="toctree-l2"><a class="reference internal" href="Matplotlib.html">Matplotlib</a></li>
<li class="toctree-l2"><a class="reference internal" href="WorkingWithFiles.html">Working with Files</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Import: Modules and Packages</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#Modules">Modules</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Import-Statements">Import Statements</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Packages">Packages</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Intra-Module-Imports">Intra-Module Imports</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#Absolute-Imports">Absolute Imports</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Relative-Imports">Relative Imports</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#Installing-a-Package">Installing a Package</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#PYTHONPATH-and-Site-Packages">PYTHONPATH and Site-Packages</a></li>
<li class="toctree-l4"><a class="reference internal" href="#Installing-Your-Own-Python-Package">Installing Your Own Python Package</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#pip-and-conda:-Package-Managers">pip and conda: Package Managers</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Links-to-Official-Documentation">Links to Official Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#Reading-Comprehension-Exercise-Solutions:">Reading Comprehension Exercise Solutions:</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_6.html">Module 6: Testing Our Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changes.html">Changelog</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> »</li>
<li><a href="../module_5.html">Module 5: Odds and Ends</a> »</li>
<li>Import: Modules and Packages</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module5_OddsAndEnds/Modules_and_Packages.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast,
.nboutput.nblast {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast + .nbinput {
margin-top: -19px;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<div class="section" id="Import:-Modules-and-Packages">
<h1>Import: Modules and Packages<a class="headerlink" href="#Import:-Modules-and-Packages" title="Permalink to this headline">¶</a></h1>
<p>Our tour through the essentials of Python and NumPy required us to regularly make use of <code class="docutils literal notranslate"><span class="pre">import</span></code> statements. This allowed us to access the functions and objects that are provided by the standard library and by NumPy.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># accessing `defaultdict` from the standard library's `collections` package</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
<span class="c1"># import the entire numpy package, giving it the alias 'np'</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</pre></div>
</div>
<p>Despite our regular use of the <code class="docutils literal notranslate"><span class="pre">import</span></code> statement, we have thus far swept its details under the rug. Here, we will finally pay our due diligence and discuss Python’s import system, which entails understanding the way that code can be organized into modules and packages. Modules are individual <code class="docutils literal notranslate"><span class="pre">.py</span></code> files from which we can import functions and objects, and packages are collections of such modules. Detailing this packaging system will not only provide us with insight into the organization of
the standard library and other collections of Python code, but it will permit us to create our own packages of code.</p>
<p>To conclude this section, we will demonstrate the process of installing a Python package on your system; supposing that you have written your own Python package, installing it enables you to import it anywhere on your system. A brief overview will be provided of the two most popular venues for hosting Python packages to the world at large: the Python Package Index (PyPI) and Anaconda.org.</p>
<p>The <a class="reference external" href="https://docs.python.org/3/tutorial/modules.html">official Python tutorial</a> provides a nice overview of this material and dives into details that we will not concern ourselves with here.</p>
<div class="admonition warning">
<p class="admonition-title fa fa-exclamation-circle"><strong>Auto-reload</strong>:</p>
<p>As you follow along with this section in a Jupyter notebook include the following code at the top of you notebook:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="o">%</span><span class="n">load_ext</span> <span class="n">autoreload</span>
<span class="o">%</span><span class="n">autoreload</span> <span class="mi">2</span>
</pre></div>
</div>
<p>Executing these “magic commands” will inform your notebook to actively reload all imported modules and packages as they are modified. If you do not execute these commands, your notebook will not “see” any changes that you have made to a module that has already been imported, unless you restart the kernel for that notebook.</p>
</div>
<div class="section" id="Modules">
<h2>Modules<a class="headerlink" href="#Modules" title="Permalink to this headline">¶</a></h2>
<p>A Python ‘module’ refers to a single <code class="docutils literal notranslate"><span class="pre">.py</span></code> file that contains function definitions and variable-assignment statements. Importing a module will execute these statements, rendering the resulting objects available via the imported module.</p>
<p>Let’s create our own module and import it into an interactive Python session. Open a Jupyter notebook or IPython console in a known directory on your computer. Now, with an <a class="reference external" href="http://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Getting_Started_With_IDEs_and_Notebooks.html#Integrated-Development-Environments">IDE</a> or simple text editor (not software like Microsoft Word!) create a text file named <code class="docutils literal notranslate"><span class="pre">my_module.py</span></code> in the same directory as you Python session. The contents of
<code class="docutils literal notranslate"><span class="pre">my_module.py</span></code> should be:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""</span>
<span class="sd">Our first Python module. This initial string is a module-level documentation string.</span>
<span class="sd">It is not a necessary component of the module. It is a useful way to describe the</span>
<span class="sd">purpose of your module.</span>
<span class="sd">"""</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"I am being executed!"</span><span class="p">)</span>
<span class="n">some_list</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">None</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">square</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span>
<span class="k">def</span> <span class="nf">cube</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">3</span>
</pre></div>
</div>
<p>Returning to our interactive Python session we can import this module into our session. Python is able to “find” this module because it is in the present directory - more on this later. Importing <code class="docutils literal notranslate"><span class="pre">my_module.py</span></code> will execute all of its code in order from top to bottom, and will produce a Python object named <code class="docutils literal notranslate"><span class="pre">my_module</span></code>; this is an instance of the built-in <code class="docutils literal notranslate"><span class="pre">module</span></code> type. Note that we do not include the <code class="docutils literal notranslate"><span class="pre">.py</span></code> suffix in our import statement.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span># importing my_module in our interactive session
>>> import my_module
I am being executed!
# produced is a object that is an instance of the module-type
>>> my_module
<module 'my_module' from 'usr/my_dir/my_module.py'>
>>> type(my_module)
module
</pre></div>
</div>
<p>As expected, importing our module causes our <code class="docutils literal notranslate"><span class="pre">print</span></code> statement to be executed, which explains why <code class="docutils literal notranslate"><span class="pre">'I</span> <span class="pre">am</span> <span class="pre">being</span> <span class="pre">executed!'</span></code> is printed to the console. Next, the objects <code class="docutils literal notranslate"><span class="pre">some_list</span></code>, <code class="docutils literal notranslate"><span class="pre">square</span></code>, and <code class="docutils literal notranslate"><span class="pre">cube</span></code> are defined as the remaining code is executed. <em>These are made available as attributes of our module object</em>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># all of the variables assigned in the module are made</span>
<span class="c1"># available as attributes of the module object</span>
<span class="o">>>></span> <span class="n">my_module</span><span class="o">.</span><span class="n">some_list</span>
<span class="p">[</span><span class="s1">'a'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">None</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">my_module</span><span class="o">.</span><span class="n">square</span>
<span class="o"><</span><span class="n">function</span> <span class="n">my_module</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">></span>
<span class="o">>>></span> <span class="n">my_module</span><span class="o">.</span><span class="n">square</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">4</span>
<span class="o">>>></span> <span class="n">my_module</span><span class="o">.</span><span class="n">cube</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">8</span>
</pre></div>
</div>
<p>It is critical to understand that this is the means by which the contents of a module is made available to the environment in which it was imported. In this vein, a good way to get to know the contents of a module is to make use of the <a class="reference external" href="http://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Informal_Intro_Python.html#Dabbling-with-Numbers">auto-completion</a> feature provided by IDEs and interactive consoles to list all of the attributes of a module object. The built-in <code class="docutils literal notranslate"><span class="pre">help</span></code>
function can also be used to summarize a module’s contents:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">help</span><span class="p">(</span><span class="n">my_module</span><span class="p">)</span>
<span class="go">Help on module my_module:</span>
<span class="go">NAME</span>
<span class="go"> my_module</span>
<span class="go">DESCRIPTION</span>
<span class="go"> Our first Python module. This initial string is a module-level documentation string.</span>
<span class="go"> It is not a necessary component of the module.</span>
<span class="go">FUNCTIONS</span>
<span class="go"> cube(x)</span>
<span class="go"> square(x)</span>
<span class="go">DATA</span>
<span class="go"> some_list = ['a', 1, None]</span>
<span class="go">FILE</span>
<span class="go"> c:\users\ryan soklaski\desktop\learning_python\python\module5_oddsandends\my_module.py</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Takeaway</strong>:</p>
<p>A module is simply a text file named with a .py suffix, whose contents consist of Python code. A module can be imported into an interactive console environment (e.g. a Jupyter notebook) or into another module. Importing a module executes that module’s code and produces a module-type object instance. Any variables that were assigned during the import are bound as attributes to that object.</p>
</div>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Creating a simple module</strong></p>
<p>Create a simple math module named <code class="docutils literal notranslate"><span class="pre">basic_math.py</span></code>. It should make available the irrational numbers <span class="math notranslate nohighlight">\(\pi\)</span> and <span class="math notranslate nohighlight">\(e\)</span>, and the function <code class="docutils literal notranslate"><span class="pre">deg_to_rad</span></code>, which converts an angle from degrees to radians.</p>
<p>Next, import this module and compute <span class="math notranslate nohighlight">\(e^{i\pi}\)</span> and compute 45 degrees in radians.</p>
</div>
</div>
<div class="section" id="Import-Statements">
<h2>Import Statements<a class="headerlink" href="#Import-Statements" title="Permalink to this headline">¶</a></h2>
<p>Python provides a flexible framework for importing modules and specific members of a module. We have already seen in our work with NumPy that we can specify an <em>alias</em> in our import statement; this can be especially convenient for import modules with long names:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># the module object for numpy is returned with the variable name `np`</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">])</span>
<span class="n">array</span><span class="p">([</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">])</span>
</pre></div>
</div>
<p>In general we can perform an alias for an import via: <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre"><module_name></span> <span class="pre">as</span> <span class="pre"><alias_name></span></code>.</p>
<p>Next, the pattern <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre"><module_name></span> <span class="pre">import</span> <span class="pre"><thing1>,</span> <span class="pre"><thing2>,</span> <span class="pre">...</span></code> permits us to import specific objects from the module instead of the importing the entire module as a whole. Let’s import <code class="docutils literal notranslate"><span class="pre">square</span></code> and <code class="docutils literal notranslate"><span class="pre">some_list</span></code> from basic_module.py:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">my_module</span> <span class="kn">import</span> <span class="n">square</span><span class="p">,</span> <span class="n">some_list</span>
<span class="go">I am being executed!</span>
<span class="gp">>>> </span><span class="n">some_list</span>
<span class="go">['a', 1, None]</span>
<span class="gp">>>> </span><span class="n">square</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">4</span>
</pre></div>
</div>
<p>Note that the module is still being executed in full, however instead of producing the module-instance <code class="docutils literal notranslate"><span class="pre">my_module</span></code>, this imort statement instead only returns the specified objects that were defined in the module.</p>
<p>Lastly, you can specify <code class="docutils literal notranslate"><span class="pre">*</span></code> to refer to all of the module’s attributes.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># import all of the contents of `my_module`</span>
<span class="o">>>></span> <span class="kn">from</span> <span class="nn">my_module</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>You can include in your module a list named <code class="docutils literal notranslate"><span class="pre">__all__</span></code>, which stores attribute names as strings, to restrict those attributes that are referred to by <code class="docutils literal notranslate"><span class="pre">*</span></code>. That is, if we included <code class="docutils literal notranslate"><span class="pre">__all__</span> <span class="pre">=</span> <span class="pre">["cube",</span> <span class="pre">"some_list"]</span></code> within <code class="docutils literal notranslate"><span class="pre">my_module.py</span></code>, then <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">my_module</span> <span class="pre">import</span> <span class="pre">*</span></code> will only import <code class="docutils literal notranslate"><span class="pre">cube</span></code> and <code class="docutils literal notranslate"><span class="pre">some_list</span></code>, and not <code class="docutils literal notranslate"><span class="pre">square</span></code>.</p>
<p>Lastly, we can also make use of aliasing for this style of import:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">my_module</span> <span class="kn">import</span> <span class="n">cube</span> <span class="k">as</span> <span class="n">my_cube</span>
<span class="gp">>>> </span><span class="n">my_cube</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">8</span>
</pre></div>
</div>
</div>
<div class="section" id="Packages">
<h2>Packages<a class="headerlink" href="#Packages" title="Permalink to this headline">¶</a></h2>
<p>It is not unusual, when working on larger scale projects, to want to organize one’s code into several modules. For example, suppose that we are writing software for performing facial recognition. We might want to have a camera module for taking pictures, a face-detection module for storing a model-class capable of detecting faces, and a database module that is used to store and update “seen” faces. These modules can be stored in a collective <em>package</em>.</p>
<p>A Python package is a directory containing a file with the name <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code>, along with other Python modules and subpackages (i.e. subdirectories, each with its own <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file and associated modules). The <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file is of special importance - it serves as the indicator that its housing directory is to be treated as a package. For example, let’s build a bare-bones package with the following directory structure:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">-</span> <span class="n">your</span> <span class="n">current</span> <span class="n">Jupyter</span> <span class="n">notebook</span> <span class="o">/</span> <span class="n">console</span> <span class="n">session</span>
<span class="o">-</span> <span class="n">a_dir</span><span class="o">/</span>
<span class="o">|--</span><span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>Note that your interactive Python session (e.g. Jupyter notebook) should be in active in the same directory as <code class="docutils literal notranslate"><span class="pre">a_dir/</span></code>. The name of the directory containing the <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> file is the name of the package, thus the name of this package is <code class="docutils literal notranslate"><span class="pre">a_dir</span></code>. Suppose the contents of <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> is as follows:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">sum_func</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="k">def</span> <span class="nf">divide_func</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">/</span> <span class="n">y</span>
</pre></div>
</div>
<p>As with a module, importing this package will execute the contents of <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> and make available <code class="docutils literal notranslate"><span class="pre">sum_func</span></code> and <code class="docutils literal notranslate"><span class="pre">divide_func</span></code> as attributes of the resulting module object:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># importing a python package</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">a_dir</span>
<span class="o">>>></span> <span class="n">a_dir</span><span class="o">.</span><span class="n">divide_func</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="mf">0.5</span>
</pre></div>
</div>
<p>Let’s graduate to a more fleshed out package, which contains modules and subpackages. The following package, <code class="docutils literal notranslate"><span class="pre">face_detection</span></code>, possesses the modules <code class="docutils literal notranslate"><span class="pre">utils</span></code>, <code class="docutils literal notranslate"><span class="pre">database</span></code>, and <code class="docutils literal notranslate"><span class="pre">model</span></code>. It also contains the subpackage <code class="docutils literal notranslate"><span class="pre">camera</span></code>, which contains the <code class="docutils literal notranslate"><span class="pre">config</span></code> module and the <code class="docutils literal notranslate"><span class="pre">calibration</span></code> module.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">-</span> <span class="n">face_detection</span><span class="o">/</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">utils</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">database</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">model</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">camera</span><span class="o">/</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">calibration</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">config</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>We can access the contents of these modules via <code class="docutils literal notranslate"><span class="pre"><package>.<module></span></code>, <code class="docutils literal notranslate"><span class="pre"><package>.<subpackage>.<module></span></code> and so on. Consider the following examples:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># importing a function from the `database` module</span>
<span class="o">>>></span> <span class="kn">from</span> <span class="nn">face_detection.database</span> <span class="kn">import</span> <span class="n">load_database</span>
<span class="c1"># importing the entire `model` module</span>
<span class="o">>>></span> <span class="kn">from</span> <span class="nn">face_detection</span> <span class="kn">import</span> <span class="n">model</span>
<span class="c1"># importing a function from the `camera` subpackage</span>
<span class="o">>>></span> <span class="kn">from</span> <span class="nn">face_detection.camera.config</span> <span class="kn">import</span> <span class="n">restore_default</span>
</pre></div>
</div>
<p>See that the <code class="docutils literal notranslate"><span class="pre">.</span></code> syntax allows us to drill progressively deeper down into modules and subpackages, relative to our top-level package.</p>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>Reading Comprehension: Packages</strong></p>
<p>Suppose that we are working with a package named <code class="docutils literal notranslate"><span class="pre">mail</span></code>. The <code class="docutils literal notranslate"><span class="pre">mail/</span></code> directory contains an <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> module whose contents are:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">send_mail</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="n">x</span>
<span class="n">phrase_of_the_day</span> <span class="o">=</span> <span class="s2">"get that package delievered!"</span>
</pre></div>
</div>
<p>It also contains the module <code class="docutils literal notranslate"><span class="pre">delivery.py</span></code> with the contents:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_zip</span><span class="p">():</span>
<span class="sd">"""just a dummy function"""</span>
<span class="k">return</span> <span class="mi">871092</span>
</pre></div>
</div>
<ol class="arabic simple">
<li>Create this package.</li>
<li>Import the send_mail function</li>
<li>Import the delivery module and then execute it’s get-zip function</li>
</ol>
</div>
</div>
<div class="section" id="Intra-Module-Imports">
<h2>Intra-Module Imports<a class="headerlink" href="#Intra-Module-Imports" title="Permalink to this headline">¶</a></h2>
<p>Modules within a package can import from one another; suppose, for instance, that both <code class="docutils literal notranslate"><span class="pre">face_detection.database</span></code> and <code class="docutils literal notranslate"><span class="pre">face_detection.camera.calibration</span></code> both want to leverage the <code class="docutils literal notranslate"><span class="pre">face_detection.utils</span></code> module. Recall the layout of this package:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">-</span> <span class="n">face_detection</span><span class="o">/</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">utils</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">database</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">model</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">camera</span><span class="o">/</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">calibration</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">config</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>There are two import styles that can be used to facilitate these inter-module imports: absolute imports and relative imports.</p>
<div class="section" id="Absolute-Imports">
<h3>Absolute Imports<a class="headerlink" href="#Absolute-Imports" title="Permalink to this headline">¶</a></h3>
<p>The absolute import style works by specifying all modules in terms of their absolute position in relation to the topmost package. Suppose that we want to import the <code class="docutils literal notranslate"><span class="pre">utils</span></code> module in our <code class="docutils literal notranslate"><span class="pre">model</span></code> module, stated as an absolute import, this would be:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">face_detection.utils</span>
</pre></div>
</div>
<p>or, using an alias</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">face_detection.utils</span> <span class="kn">as</span> <span class="nn">utils</span>
</pre></div>
</div>
<p>Suppose that we want to import the <code class="docutils literal notranslate"><span class="pre">utils</span></code> module in <code class="docutils literal notranslate"><span class="pre">face_detection.camera.calibration</span></code> as well; the absolute import would look identical since the absolute location of <code class="docutils literal notranslate"><span class="pre">utils</span></code> relative to the top-level package does not change based on where we are using this import statement.</p>
<p>As an additional example, to import the camera configuration module anywhere in the package, the absolute import statement would appear as:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">face_detection.camera.config</span>
</pre></div>
</div>
<p>The absolute import syntax supports all of the variations the we enumerated above, such as aliased imports and <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre"><module></span> <span class="pre">import</span> <span class="pre"><object></span></code>.</p>
</div>
<div class="section" id="Relative-Imports">
<h3>Relative Imports<a class="headerlink" href="#Relative-Imports" title="Permalink to this headline">¶</a></h3>
<p>Relative imports use dots to indicate the relative position of the imported module, based on the location of the module doing the importing. For example, suppose that we want to import the <code class="docutils literal notranslate"><span class="pre">utils</span></code> module in our <code class="docutils literal notranslate"><span class="pre">model</span></code> module using a relative import statement:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">utils</span>
</pre></div>
</div>
<p>See that <code class="docutils literal notranslate"><span class="pre">.</span></code> serves to represent “the current package”. <code class="docutils literal notranslate"><span class="pre">..</span></code> then refers to “one package above the current one”. Thus a relative import of <code class="docutils literal notranslate"><span class="pre">utils</span></code> from <code class="docutils literal notranslate"><span class="pre">face_detection/camera/calibration.py</span></code> would look like:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">..</span> <span class="kn">import</span> <span class="n">utils</span>
</pre></div>
</div>
<p>We use <code class="docutils literal notranslate"><span class="pre">..</span></code> because <code class="docutils literal notranslate"><span class="pre">utils</span></code> is not in the same package as <code class="docutils literal notranslate"><span class="pre">calibration.py</span></code>, but is one package above it. We can also import specific contents from a module in this way:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">..utils</span> <span class="kn">import</span> <span class="n">some_util_func</span>
</pre></div>
</div>
<p>The relative import style is more restricted than the absolute import style. It is only able to embody the <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre"><module></span> <span class="pre">import</span> <span class="pre"><thing1>,</span> <span class="pre"><thing2></span></code> form of import statements. Despite their limited form, relative imports can be nice to use if you are dealing with a project with deeply nested sub-packages.</p>
</div>
</div>
<div class="section" id="Installing-a-Package">
<h2>Installing a Package<a class="headerlink" href="#Installing-a-Package" title="Permalink to this headline">¶</a></h2>
<div class="section" id="PYTHONPATH-and-Site-Packages">
<h3>PYTHONPATH and Site-Packages<a class="headerlink" href="#PYTHONPATH-and-Site-Packages" title="Permalink to this headline">¶</a></h3>
<p>Thus far in our reading of Python packages we have had to take care that all of the modules and packages that we have written reside in the same directory as our interactive Python session. How is it that we are able to import NumPy in any session or module, without any knowledge of where that package is located? This is because we have installed <em>NumPy</em>, which means that the package has been placed in our “Python path”, which is indicated as <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> specifies the directories that the Python interpreter will look in when importing modules. You can check your <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> using <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># looking up `PYTHONPATH`</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">sys</span>
<span class="o">>>></span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span>
<span class="p">[</span><span class="s1">''</span><span class="p">,</span>
<span class="s1">'/home/TerranceWasabi/miniconda3/bin'</span><span class="p">,</span>
<span class="s1">'/home/TerranceWasabi/miniconda3/lib/python36.zip'</span><span class="p">,</span>
<span class="s1">'/home/TerranceWasabi/miniconda3/lib/python3.6'</span><span class="p">,</span>
<span class="s1">'/home/TerranceWasabi/miniconda3/lib/python3.6/lib-dynload'</span><span class="p">,</span>
<span class="s1">'/home/TerranceWasabi/miniconda3/lib/python3.6/site-packages'</span>
<span class="p">]</span>
</pre></div>
</div>
<p>Note that the first entry in <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> is <code class="docutils literal notranslate"><span class="pre">''</span></code>, meaning that the Python interpreter will first look in the current directory when trying to do an import. If it does not find any package or module that satisfies the import statement, then it will proceed to check the next entry in <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>. This is why we took care to create our modules and packages in the same directory as our active Python session.</p>
<p>Now note the last directory in <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>: site-packages. <em>Site-packages is the target directory in which all installed Python packages are placed by default</em>. We can import NumPy wherever we’d like because it was placed in site-packages when it was installed, and the Python interpreter will always look in site-packages when attempting to fulfill an import statement.</p>
<p>In lieu of printing out your <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code>, you can look up the location of your site-packages directly:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># looking up your site-packages</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">site</span>
<span class="o">>>></span> <span class="n">site</span><span class="o">.</span><span class="n">getsitepackages</span><span class="p">()</span>
<span class="p">[</span><span class="s1">'/home/TerranceWasabi/miniconda3/lib/python3.6/site-packages'</span><span class="p">]</span>
</pre></div>
</div>
<p>It must be mentioned that we are sweeping some details under the rug here. Installing NumPy does not merely entail copying its various modules and packages wholesale into site-packages. That being said, we will not dive any deeper into the technical details of package installation beyond understanding where packages are installed and thus where our Python interpreter looks to import them.</p>
</div>
<div class="section" id="Installing-Your-Own-Python-Package">
<h3>Installing Your Own Python Package<a class="headerlink" href="#Installing-Your-Own-Python-Package" title="Permalink to this headline">¶</a></h3>
<p>Suppose that we are happy with the work we have done on our <code class="docutils literal notranslate"><span class="pre">face_detector</span></code> project. We will want to install this package - placing it in our site-packages directory so that we can import it irrespective of our Python interpreter’s working directory. Here we will construct a basic setup script that will allow us to accomplish this. For completeness, we will also indicate how one would include a test suite alongside the source code in this directory structure.</p>
<p>We note outright that the purpose of this section is strictly to provide you with the minimum set of instructions needed to install a package. We will not be diving into what is going on under the hood at all. Please refer to <a class="reference external" href="https://docs.python.org/3/distutils/introduction.html#an-introduction-to-distutils">An Introduction to Distutils</a> and <a class="reference external" href="https://packaging.python.org/tutorials/packaging-projects/#packaging-your-project">Packaging Your Project</a> for a deeper treatment of this topic.</p>
<p>Carrying on, we will want to create a setup-script, <code class="docutils literal notranslate"><span class="pre">setup.py</span></code>, <em>in the same directory as our package</em>. That is, our directory structure should look like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">-</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="c1"># script responsible for installing `face_detection` package</span>
<span class="o">-</span> <span class="n">face_detection</span><span class="o">/</span> <span class="c1"># source code of `face_detection` package</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">utils</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">database</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">model</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">camera</span><span class="o">/</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">calibration</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">config</span><span class="o">.</span><span class="n">py</span>
<span class="o">-</span> <span class="n">tests</span><span class="o">/</span> <span class="c1"># test-suite for `face_detection` package (to be run using pytest)</span>
<span class="o">|--</span> <span class="n">conftest</span><span class="o">.</span><span class="n">py</span> <span class="c1"># optional configuration file for pytest</span>
<span class="o">|--</span> <span class="n">test_utils</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">test_database</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">test_model</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">camera</span><span class="o">/</span>
<span class="o">|--</span> <span class="n">test_calibration</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">test_config</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">tests/</span></code> directory can be included at the same directory level as <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> and <code class="docutils literal notranslate"><span class="pre">face_detection/</span></code>. This is the recommended structure for using <a class="reference external" href="https://docs.pytest.org/en/latest/">pytest</a> as our test-runner.</p>
<p>The bare bones build script for preparing your package for installation, <code class="docutils literal notranslate"><span class="pre">setup.py</span></code>, is as follows:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># contents of setup.py</span>
<span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">find_packages</span><span class="p">,</span> <span class="n">setup</span>
<span class="n">setup</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s2">"face_detection"</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s2">"1.0.0"</span><span class="p">,</span>
<span class="n">packages</span><span class="o">=</span><span class="n">find_packages</span><span class="p">(</span><span class="n">exclude</span><span class="o">=</span><span class="p">[</span><span class="s2">"tests"</span><span class="p">,</span> <span class="s2">"tests.*"</span><span class="p">]),</span>
<span class="n">python_requires</span><span class="o">=</span><span class="s2">">=3.5"</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">exclude</span></code> expression is used to ensure that specific directories or files are not included in the installation of <code class="docutils literal notranslate"><span class="pre">face_detection</span></code>. We use <code class="docutils literal notranslate"><span class="pre">exclude=["tests",</span> <span class="pre">"tests.*"]</span></code> to avoid installing the test-suite alongside <code class="docutils literal notranslate"><span class="pre">face_detection</span></code>.</p>
<p>If you read through the additional materials linked above, you will see that there are many more fields of optional information that can be provided in this setup script, such as the author name, any installation requirements that the package has, and more.</p>
<p>Armed with this script, we are ready to install our package locally on our machine! In your terminal, navigate to the directory containing this setup script and your package that it being installed. Run</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>pip install .
</pre></div>
</div>
<p>and voilà, your package <code class="docutils literal notranslate"><span class="pre">face_detection</span></code> will have been installed to site-packages. You are now free to import this package from any directory on your machine. In order to uninstall this package from your machine execute the following from your terminal:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>pip uninstall face_detection
</pre></div>
</div>
<p>One final but important detail: the installed version of your package will no longer “see” the source code. That is, if you go on to make any changes to your code, you will have to uninstall and reinstall your package before your will see the effects system-wide. Instead, you can install your package in “development mode”, such that a symbolic link to your source code is placed in your site-packages. Thus, any changes that you make to your code will immediately be reflected in your system-wide
installation. You can add the <code class="docutils literal notranslate"><span class="pre">--editable</span></code> flag to pip to install a package in development mode:</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>pip install --editable .
</pre></div>
</div>
</div>
</div>
<div class="section" id="pip-and-conda:-Package-Managers">
<h2>pip and conda: Package Managers<a class="headerlink" href="#pip-and-conda:-Package-Managers" title="Permalink to this headline">¶</a></h2>
<p>Python packages can be shared worldwide. There are two widely-used Python package managers, <code class="docutils literal notranslate"><span class="pre">pip</span></code> and <code class="docutils literal notranslate"><span class="pre">conda</span></code>. <code class="docutils literal notranslate"><span class="pre">pip</span></code> downloads and installs packages from <a class="reference external" href="https://pypi.org/">The Python Package Index (PyPI)</a>, whereas <code class="docutils literal notranslate"><span class="pre">conda</span></code> downloads and installs packages from the Anaconda Cloud. Both <code class="docutils literal notranslate"><span class="pre">conda</span></code> and <code class="docutils literal notranslate"><span class="pre">pip</span></code> are installed as part of the <a class="reference external" href="http://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Installing_Python.html#Installing-Anaconda">Anaconda distribution</a>.</p>
<p>To install a package via <code class="docutils literal notranslate"><span class="pre">pip</span></code>, you execute</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>pip install <package_name>
</pre></div>
</div>
<p>To install a package via <code class="docutils literal notranslate"><span class="pre">conda</span></code>, you execute</p>
<div class="highlight-shell notranslate"><div class="highlight"><pre><span></span>conda install <package_name>
</pre></div>
</div>
<p>Both managers will install packages to your site-packages directory.</p>
<p>There are substantial benefits for using <code class="docutils literal notranslate"><span class="pre">conda</span></code> rather than <code class="docutils literal notranslate"><span class="pre">pip</span></code> to install packages. First and foremost, <code class="docutils literal notranslate"><span class="pre">conda</span></code> has a powerful “environment solver”, which tracks the inter-dependencies of Python packages. Thus it will attempt to install, upgrade, and downgrade packages as needed to accommodate your installations. Additionally, the default list of packages available via <code class="docutils literal notranslate"><span class="pre">conda</span></code> are curated and maintained by Continuum Analytics, the creators of Anaconda. To elucidate one of the
benefits of this, installing NumPy via <code class="docutils literal notranslate"><span class="pre">pip</span></code> will deliver the vanilla version of NumPy to you; <code class="docutils literal notranslate"><span class="pre">conda</span></code> will install an <a class="reference external" href="https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl">mkl-optimized version of NumPy</a>, which can execute routines substantially faster. Finally, <code class="docutils literal notranslate"><span class="pre">conda</span></code> also serves as <a class="reference external" href="https://conda.io/docs/user-guide/tasks/manage-environments.html">an environment manager</a>, which allows you to maintain multiple, non-conflicting environments that can house different
configurations of installed Python packages and even different versions of Python itself.</p>
<p>That being said, there are some benefits to using <code class="docutils literal notranslate"><span class="pre">pip</span></code>. PyPi is accessible and easy to upload packages to; this is likely the easiest means for distributing a Python package worldwide. As such, <code class="docutils literal notranslate"><span class="pre">pip</span></code> provides access to a wider range of Python packages. That being said, <code class="docutils literal notranslate"><span class="pre">conda</span></code> can also be directed to install packages from custom channels - providing access to packages outside of the curated Anaconda distribution. This has become a popular method of installation for machine learning
libraries like PyTorch and TensorFlow.</p>
<p>You are free to install some packages using <code class="docutils literal notranslate"><span class="pre">conda</span></code> and others with <code class="docutils literal notranslate"><span class="pre">pip</span></code>. Just take care not to accidentally install the same package with both - this can lead to a real mess.</p>
</div>
<div class="section" id="Links-to-Official-Documentation">
<h2>Links to Official Documentation<a class="headerlink" href="#Links-to-Official-Documentation" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="https://docs.python.org/3/tutorial/modules.html">Python Tutorial: Modules</a></li>
<li><a class="reference external" href="https://docs.python.org/3/distutils/introduction.html#an-introduction-to-distutils">An Introduction to Distutils</a></li>
<li><a class="reference external" href="https://packaging.python.org/tutorials/packaging-projects/#packaging-your-project">Packaging Your Project</a></li>
<li><a class="reference external" href="https://pypi.org/">PyPi</a></li>
</ul>
</div>
<div class="section" id="Reading-Comprehension-Exercise-Solutions:">
<h2>Reading Comprehension Exercise Solutions:<a class="headerlink" href="#Reading-Comprehension-Exercise-Solutions:" title="Permalink to this headline">¶</a></h2>
<p><strong>Creating a simple module: Solution</strong></p>
<p>Create a simple math module named <code class="docutils literal notranslate"><span class="pre">basic_math.py</span></code>. It should make available the irrational numbers <span class="math notranslate nohighlight">\(\pi\)</span> and <span class="math notranslate nohighlight">\(e\)</span>, and the function <code class="docutils literal notranslate"><span class="pre">deg_to_rad</span></code>, which converts an angle from degrees to radians.</p>
<p>The contents of <code class="docutils literal notranslate"><span class="pre">basic_math.py</span></code> should be:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sd">"""Basic math constants and functions"""</span>
<span class="n">pi</span> <span class="o">=</span> <span class="mf">3.141592653589793</span>
<span class="n">e</span> <span class="o">=</span> <span class="mf">2.718281828459045</span>
<span class="k">def</span> <span class="nf">deg_to_rad</span><span class="p">(</span><span class="n">angle</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">180</span><span class="p">)</span> <span class="o">*</span> <span class="n">angle</span>
</pre></div>
</div>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">basic_math</span>
<span class="go"># Euler's formula: e**(i * pi) = -1</span>
<span class="gp">>>> </span><span class="n">basic_math</span><span class="o">.</span><span class="n">e</span> <span class="o">**</span> <span class="p">(</span><span class="n">basic_math</span><span class="o">.</span><span class="n">pi</span> <span class="o">*</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">))</span>
<span class="go">(-1+1.2246467991473532e-16j)</span>
<span class="gp">>>> </span><span class="n">basic_math</span><span class="o">.</span><span class="n">deg_to_rad</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span>
<span class="go">0.7853981633974483</span>
</pre></div>
</div>
<p><strong>Packages: Solutions</strong></p>
<ol class="arabic simple">
<li>Create this package.</li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">mail</span><span class="o">/</span>
<span class="o">|--</span> <span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="o">|--</span> <span class="n">delivery</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li>Import the send_mail function</li>
</ol>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">mail</span> <span class="kn">import</span> <span class="n">send_mail</span>
</pre></div>
</div>
<ol class="arabic simple" start="3">
<li>Import the delivery module and then execute it’s get-zip function</li>
</ol>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">mail</span> <span class="kn">import</span> <span class="n">delivery</span>
<span class="gp">>>> </span><span class="n">delivery</span><span class="o">.</span><span class="n">get_zip</span><span class="p">()</span>
<span class="go">871092</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../module_6.html" class="btn btn-neutral float-right" title="Module 6: Testing Our Code" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="WorkingWithFiles.html" class="btn btn-neutral float-left" title="Working with Files" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2019, Ryan Soklaski
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>