-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPackaging.html
More file actions
1104 lines (1075 loc) · 84.7 KB
/
Packaging.html
File metadata and controls
1104 lines (1075 loc) · 84.7 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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Packages and Packaging — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="A Small Example Package" href="../exercises/packaging/package_lab.html" />
<link rel="prev" title="Documentation" href="Documentation.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" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<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="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="NamingThings.html">Style and Naming</a></li>
<li class="toctree-l2"><a class="reference internal" href="Modules.html">Code Structure, Modules, and Namespaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="Documentation.html">Documentation</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Packages and Packaging</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/packaging/package_lab.html">A Small Example Package</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-pkg.html">Mailroom – as a Python Package</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/10-modules_packages/index.html"><span class="section-number">10. </span>Modules and Packages</a> »</li>
<li>Packages and Packaging</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/Packaging.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="Documentation.html" class="btn btn-neutral float-left" title="Documentation" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/packaging/package_lab.html" class="btn btn-neutral float-right" title="A Small Example Package" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="packages-and-packaging">
<span id="packaging"></span><h1>Packages and Packaging<a class="headerlink" href="#packages-and-packaging" title="Permalink to this headline"></a></h1>
<div class="section" id="packages-modules-imports-oh-my">
<h2>Packages, Modules, Imports, Oh My!<a class="headerlink" href="#packages-modules-imports-oh-my" title="Permalink to this headline"></a></h2>
<p>Before we get started on making your own package – let’s remind
ourselves about packages and modules, and importing….</p>
<p class="rubric">Modules</p>
<p>A python “module” is a single namespace, with a collection of values:</p>
<blockquote>
<div><ul class="simple">
<li><p>functions</p></li>
<li><p>constants</p></li>
<li><p>class definitions</p></li>
<li><p>really any old value.</p></li>
</ul>
</div></blockquote>
<p>A module usually corresponds to a single file: <code class="docutils literal notranslate"><span class="pre">something.py</span></code></p>
<p class="rubric">Packages</p>
<p>A “package” is essentially a module, except it can have other modules (and indeed other packages) inside it.</p>
<p>A package usually corresponds to a directory with a file in it called <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> and any number
of python files or other package directories:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a_package</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">module_a</span><span class="o">.</span><span class="n">py</span>
<span class="n">a_sub_package</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">module_b</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> can be totally empty – or it can have arbitrary python code in it.
The code will be run when the package is imported – just like a module.</p>
<p>Modules inside packages are <em>not</em> automatically imported. So, with the above structure:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">a_package</span>
</pre></div>
</div>
<p>will run the code in <code class="docutils literal notranslate"><span class="pre">a_package/__init__.py</span></code>. Any names defined in the <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> will be available in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a_package</span><span class="o">.</span><span class="n">a_name</span>
</pre></div>
</div>
<p>But:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a_package</span><span class="o">.</span><span class="n">module_a</span>
</pre></div>
</div>
<p>will not exist. To get submodules, you need to explicitly import them like so:</p>
<blockquote>
<div><p>import a_package.module_a</p>
</div></blockquote>
<div class="section" id="more-on-importing">
<h3>More on Importing<a class="headerlink" href="#more-on-importing" title="Permalink to this headline"></a></h3>
<p>You usually import a module like this:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">something</span>
</pre></div>
</div>
<p>or:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">something</span> <span class="kn">import</span> <span class="n">something_else</span>
</pre></div>
</div>
<p>or a few names from a package:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">something</span> <span class="kn">import</span> <span class="p">(</span><span class="n">name_1</span><span class="p">,</span>
<span class="n">name_2</span><span class="p">,</span>
<span class="n">name_3</span><span class="p">,</span>
<span class="n">x</span><span class="p">,</span>
<span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<p>You also can optionally rename stuff as you import it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</pre></div>
</div>
<p>This is a common pattern for using large packages (maybe with long names) and not having to type a lot.</p>
</div>
<div class="section" id="import">
<h3><code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">*</span></code><a class="headerlink" href="#import" title="Permalink to this headline"></a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">something</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<p>Means: “import all the names in the module, “something”.</p>
<p>You really don’t want to do that! It is an old pattern that is now an anti-pattern.</p>
<p>But if you do encounter it, it doesn’t actually import all the names – it imports the ones defined in the module’s <code class="docutils literal notranslate"><span class="pre">__all__</span></code> variable.</p>
<p><code class="docutils literal notranslate"><span class="pre">__all__</span></code> is a list of names that you want <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">*</span></code> to import.
So the module author can control it, and not accidentally override builtins or bring a lot of extraneous names into your namespace.</p>
<p>But really,</p>
<p class="centered">
<strong><strong>Don’t Use ``import *``</strong></strong></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 were added with PEP 328:</p>
<p><a class="reference external" href="https://www.python.org/dev/peps/pep-0328/">https://www.python.org/dev/peps/pep-0328/</a></p>
<p>The final version is described here:</p>
<p><a class="reference external" href="https://www.python.org/dev/peps/pep-0328/#guido-s-decision">https://www.python.org/dev/peps/pep-0328/#guido-s-decision</a></p>
<p>This gets confusing! There is a good discussion on Stack Overflow here:</p>
<p><a class="reference external" href="http://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time">Relative Imports for the Billionth Time</a></p>
<p>Relative imports allow you to refer to other modules relative to where the existing module is in the package hierarchy, rather than in the entire python module namespace. For instance, with the following package structure:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">package</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">subpackage1</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">moduleX</span><span class="o">.</span><span class="n">py</span>
<span class="n">moduleY</span><span class="o">.</span><span class="n">py</span>
<span class="n">subpackage2</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">moduleZ</span><span class="o">.</span><span class="n">py</span>
<span class="n">moduleA</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>You can do (in <code class="docutils literal notranslate"><span class="pre">moduleX.py</span></code>):</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">.moduleY</span> <span class="kn">import</span> <span class="n">spam</span>
<span class="kn">from</span> <span class="nn">.</span> <span class="kn">import</span> <span class="n">moduleY</span>
<span class="kn">from</span> <span class="nn">..subpackage1</span> <span class="kn">import</span> <span class="n">moduleY</span>
<span class="kn">from</span> <span class="nn">..subpackage2.moduleZ</span> <span class="kn">import</span> <span class="n">eggs</span>
<span class="kn">from</span> <span class="nn">..moduleA</span> <span class="kn">import</span> <span class="n">foo</span>
<span class="kn">from</span> <span class="nn">...package</span> <span class="kn">import</span> <span class="n">bar</span>
<span class="kn">from</span> <span class="nn">...sys</span> <span class="kn">import</span> <span class="n">path</span>
</pre></div>
</div>
<p>Similarly to command line shells:</p>
<p>“.” means “the current package”</p>
<p>“..” means “the package above this one”</p>
<p>Note that you have to use the <code class="docutils literal notranslate"><span class="pre">from</span></code> form of import when using relative imports.</p>
<p>(That’s current <em>package</em>, not current <em>module</em>!)</p>
<p class="rubric">Caveats:</p>
<ul class="simple">
<li><p>you can only use relative imports from within a package</p></li>
<li><p>you can not use relative imports from the interpreter</p></li>
<li><p>you can not use relative imports from a top-level script
(if <code class="docutils literal notranslate"><span class="pre">__name__</span></code> is set to <code class="docutils literal notranslate"><span class="pre">__main__</span></code>. So the same python file with relative imports can work if it’s imported, but not if it’s run as a script)</p></li>
</ul>
<p>The alternative is to always use absolute imports:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">package.subpackage</span> <span class="kn">import</span> <span class="n">moduleX</span>
<span class="kn">from</span> <span class="nn">package.moduleA</span> <span class="kn">import</span> <span class="n">foo</span>
</pre></div>
</div>
<p class="rubric">Advantages of Relative Imports:</p>
<ul class="simple">
<li><p>Package does not have to be installed</p></li>
<li><p>You can move things around, and not much has to change</p></li>
</ul>
<p class="rubric">Advantages of Absolute Imports</p>
<ul class="simple">
<li><p>explicit is better than implicit</p></li>
<li><p>imports are the same regardless of where you put the package</p></li>
<li><p>imports are the same in package code, command line, tests, scripts, etc.</p></li>
</ul>
<p>There is debate about which is the “one way to do it” – a bit unpythonic, but you’ll need to make your own decision.</p>
</div>
<div class="section" id="sys-modules">
<h3><code class="docutils literal notranslate"><span class="pre">sys.modules</span></code><a class="headerlink" href="#sys-modules" title="Permalink to this headline"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> is simply a dictionary that stores all the already imported modules.
The keys are the module names, and the values are the module objects themselves.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Remember that everything in Python is an object – including modules. So they can be stored in lists and dict, assigned names, even passed to functions – just like any other object. They are not often used that way, but they can be.</p>
</div>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">In [4]: </span><span class="nb">type</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="gh">Out[4]: </span><span class="go">dict</span>
<span class="gp">In [6]: </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">'textwrap'</span><span class="p">]</span>
<span class="gh">Out[6]: </span><span class="go"><module 'textwrap' from '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/textwrap.py'></span>
<span class="gp">In [10]: </span><span class="p">[</span><span class="n">var</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="nb">vars</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="s1">'textwrap'</span><span class="p">])</span> <span class="k">if</span> <span class="n">var</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">"__"</span><span class="p">)]</span>
<span class="gh">Out[10]:</span>
<span class="go">['__spec__',</span>
<span class="go"> '__package__',</span>
<span class="go"> '__loader__',</span>
<span class="go"> '__doc__',</span>
<span class="go"> '__cached__',</span>
<span class="go"> '__name__',</span>
<span class="go"> '__all__',</span>
<span class="go"> '__file__',</span>
<span class="go"> '__builtins__']</span>
</pre></div>
</div>
<p>You can access the module through the <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> dict:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [12]: </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">'textwrap'</span><span class="p">]</span><span class="o">.</span><span class="vm">__file__</span>
<span class="gh">Out[12]: </span><span class="go">'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/textwrap.py'</span>
</pre></div>
</div>
<p>Which is the same as:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [13]: </span><span class="kn">import</span> <span class="nn">textwrap</span>
<span class="gp">In [14]: </span><span class="n">textwrap</span><span class="o">.</span><span class="vm">__file__</span>
<span class="gh">Out[14]: </span><span class="go">'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/textwrap.py'</span>
<span class="gp">In [15]: </span><span class="nb">type</span><span class="p">(</span><span class="n">textwrap</span><span class="p">)</span>
<span class="gh">Out[15]: </span><span class="go">module</span>
<span class="gp">In [16]: </span><span class="n">textwrap</span> <span class="ow">is</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">'textwrap'</span><span class="p">]</span>
<span class="gh">Out[16]: </span><span class="go">True</span>
</pre></div>
</div>
<p>So, more or less, when you import a module, the interpreter:</p>
<ul class="simple">
<li><p>Looks to see if the module is already in <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>.</p></li>
<li><p>If it is, it binds a name to the existing module in the current module’s namespace.</p></li>
<li><p>If it isn’t:</p></li>
</ul>
<blockquote>
<div><ul class="simple">
<li><p>A module object is created</p></li>
<li><p>The code in the file is run</p></li>
<li><p>The module is added to sys.modules</p></li>
<li><p>The module is added to the current namespace.</p></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="implications-of-module-import-process">
<h3>Implications of module import process:<a class="headerlink" href="#implications-of-module-import-process" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>The code in a module only runs once per program run.</p></li>
<li><p>Importing a module again is cheap and fast.</p></li>
<li><p>Every place your code imports a module it gets the <em>same</em> object
- You can use this to share “global” state where you want to.</p></li>
<li><p>If you change the code in a module while the program is running – the change will <strong>not</strong> show up, even if re-imported.</p>
<ul>
<li><p>That’s what <code class="docutils literal notranslate"><span class="pre">importlib.reload()</span></code> is for.</p></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="the-module-search-path">
<h3>The module search path<a class="headerlink" href="#the-module-search-path" title="Permalink to this headline"></a></h3>
<p>The interpreter keeps a list (<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>) of all the places that it looks for modules or packages when you do an import:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="p">:</span>
<span class="nb">print</span> <span class="n">p</span>
</pre></div>
</div>
<p>you can manipulate that list to add or remove paths to let python find modules in a new place.</p>
<p>Every module has a <code class="docutils literal notranslate"><span class="pre">__file__</span></code> name that points to the path it lives in. This lets you add paths relative to where you are, etc.</p>
<blockquote>
<div><div class="admonition note">
<p class="admonition-title">Note</p>
<p>It’s usually better to use setuptools’ “develop” mode (or <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">-e</span></code>) instead of messing with <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> – see below.</p>
</div>
</div></blockquote>
<p class="rubric">Gotcha!</p>
<p>One “gotcha” in Python is “name shadowing”. The interpreter automatically adds the “current working directory” to <code class="docutils literal notranslate"><span class="pre">sys.path</span></code>. This means you can start the interpreter and just <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">something</span></code> to work with your code. But if you happen to have a python file, or package, in your current working directory that’s the same as an installed package, then it will get imported instead, which can lead to some odd errors. If you are getting confusing errors on import – check for python modules in your current working directory that may match an installed package!</p>
</div>
<div class="section" id="reloading">
<h3>Reloading<a class="headerlink" href="#reloading" title="Permalink to this headline"></a></h3>
<p>Once loaded, a module stays loaded.</p>
<p>If you import it again (usually in another module) it will simply use the version already there – rather than re-running the code.</p>
<p>And you can access all the already loaded modules from <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>. <code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> is a dict with the module names as the keys, and the module objects as the values</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">In [5]: </span><span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="gh">Out[5]: </span><span class="go">dict_keys(['builtins', 'sys', '_frozen_importlib', '_imp', '_warnings', '_thread', '_weakref', '_frozen_importlib_external', '_io', 'marshal', 'posix', 'zipimport', 'encodings', 'codecs', '_codecs'</span>
</pre></div>
</div>
<p>A lot there!</p>
<p>There’s no reason too – but you could import an already imported module like so:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [10]: </span><span class="n">math</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="s1">'math'</span><span class="p">]</span>
<span class="gp">In [11]: </span><span class="n">math</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span>
<span class="gh">Out[11]: </span><span class="go">1.2246467991473532e-16</span>
<span class="gp">In [12]: </span><span class="n">math</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
<span class="gh">Out[12]: </span><span class="go">1.0</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="python-distributions">
<h2>Python Distributions<a class="headerlink" href="#python-distributions" title="Permalink to this headline"></a></h2>
<p>So far, we’ve used the Python from python.org. It works great, and supports a lots of packages via pip.</p>
<p>But there are also a few “curated” distributions. These provide python and a package management system for hard-to-build packages.</p>
<p>Widely used by the scipy community:</p>
<p>(lots of hard to build stuff that needs to work together…)</p>
<blockquote>
<div><ul class="simple">
<li><p>Anaconda (<a class="reference external" href="https://store.continuum.io/cshop/anaconda/">https://store.continuum.io/cshop/anaconda/</a>) and <a class="reference external" href="https://docs.conda.io/en/latest/miniconda.html">miniconda</a></p></li>
<li><p>ActivePython (<a class="reference external" href="http://www.activestate.com/activepython">http://www.activestate.com/activepython</a>)</p></li>
</ul>
</div></blockquote>
<p>Conda has seen a LOT of growth in the last few years – it’s based on the open-source conda packaging system, and provides both a commercial curated set of packages, and a community-developed collection of packages known as conda-forge:</p>
<p><a class="reference external" href="https://conda-forge.org/">https://conda-forge.org/</a></p>
<p>If you are doing data science or scientific development – I recommend you take a look at Anaconda, conda and conda-forge.</p>
</div>
<div class="section" id="installing-packages">
<h2>Installing Packages<a class="headerlink" href="#installing-packages" title="Permalink to this headline"></a></h2>
<p>Every Python installation has its own stdlib and <code class="docutils literal notranslate"><span class="pre">site-packages</span></code> folder.</p>
<p><code class="docutils literal notranslate"><span class="pre">site-packages</span></code> is the default place for third-party packages.</p>
<div class="section" id="from-source">
<h3>From source<a class="headerlink" href="#from-source" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>(<code class="docutils literal notranslate"><span class="pre">setup.py</span> <span class="pre">install</span></code> )</p></li>
<li><p>With the system installer (apt-get, yum, etc…)</p></li>
</ul>
</div>
<div class="section" id="from-binaries">
<h3>From binaries<a class="headerlink" href="#from-binaries" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p>Binary wheels – (More and more of those available)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pip</span></code> should find appropriate binary wheels if they are there.</p></li>
</ul>
</div>
<div class="section" id="a-bit-of-history">
<h3>A bit of history:<a class="headerlink" href="#a-bit-of-history" title="Permalink to this headline"></a></h3>
<p>In the beginning, there was the <code class="docutils literal notranslate"><span class="pre">distutils</span></code>:</p>
<p>But <code class="docutils literal notranslate"><span class="pre">distutils</span></code> is missing some key features:</p>
<ul class="simple">
<li><p>package versioning</p></li>
<li><p>package discovery</p></li>
<li><p>auto-install</p></li>
</ul>
<ul class="simple">
<li><p>And then came <code class="docutils literal notranslate"><span class="pre">PyPi</span></code></p></li>
<li><p>And then came <code class="docutils literal notranslate"><span class="pre">setuptools</span></code> (with easy_install)</p></li>
<li><p>But that wasn’t well maintained…</p></li>
<li><p>Then there was <code class="docutils literal notranslate"><span class="pre">distribute/pip</span></code></p></li>
<li><p>Which has now been merged back into <code class="docutils literal notranslate"><span class="pre">setuptools</span></code></p></li>
</ul>
<p>Now it’s pretty stable: pip+setuptools+wheel: use them.</p>
<p><strong>warning</strong> – setuptools still provides easy_install, but it has mostly been deprecated, so you really want to use pip. And sometimes setuptools will invoke it for you under the hood by accident :-(</p>
</div>
<div class="section" id="id1">
<h3>Installing Packages<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>Actually, it’s still a bit of a mess</p>
<p>But getting better, and the mess is <em>almost</em> cleaned up.</p>
</div>
<div class="section" id="current-state-of-packaging">
<h3>Current State of Packaging<a class="headerlink" href="#current-state-of-packaging" title="Permalink to this headline"></a></h3>
<div class="section" id="to-build-packages-setuptools">
<h4>To build packages: setuptools<a class="headerlink" href="#to-build-packages-setuptools" title="Permalink to this headline"></a></h4>
<blockquote>
<div><ul class="simple">
<li><p><a class="reference external" href="https://setuptools.readthedocs.io/en/latest/">https://setuptools.readthedocs.io/en/latest/</a></p></li>
</ul>
</div></blockquote>
<p>setuptools provides extensions to the build-in distutils:</p>
<p><a class="reference external" href="https://docs.python.org/3/library/distutils.html">https://docs.python.org/3/library/distutils.html</a></p>
<p>But there are a couple of those extensions that you really do need, so most folks use setuptools for everything. In fact, pip itself requires setuptools.</p>
</div>
<div class="section" id="to-install-packages-pip">
<h4>To install packages: pip<a class="headerlink" href="#to-install-packages-pip" title="Permalink to this headline"></a></h4>
<blockquote>
<div><ul class="simple">
<li><p><a class="reference external" href="https://pip.pypa.io/en/latest/installing.html">https://pip.pypa.io/en/latest/installing.html</a></p></li>
</ul>
</div></blockquote>
</div>
<div class="section" id="for-binary-packages-wheels">
<h4>For binary packages: wheels<a class="headerlink" href="#for-binary-packages-wheels" title="Permalink to this headline"></a></h4>
<blockquote>
<div><ul class="simple">
<li><p><a class="reference external" href="http://www.python.org/dev/peps/pep-0427/">http://www.python.org/dev/peps/pep-0427/</a></p></li>
</ul>
</div></blockquote>
<p>(installable by pip)</p>
</div>
</div>
<div class="section" id="compiled-packages">
<h3>Compiled Packages<a class="headerlink" href="#compiled-packages" title="Permalink to this headline"></a></h3>
<p>Biggest issue is with compiled extensions:</p>
<blockquote>
<div><ul class="simple">
<li><p>(C/C++, Fortran, etc.)</p></li>
<li><p>You need the right compiler set up</p></li>
</ul>
</div></blockquote>
<p>Dependencies:</p>
<blockquote>
<div><ul class="simple">
<li><p>Here’s where it gets really ugly</p></li>
<li><p>Particularly on Windows</p></li>
</ul>
</div></blockquote>
<div class="section" id="linux">
<h4>Linux<a class="headerlink" href="#linux" title="Permalink to this headline"></a></h4>
<p>Pretty straightforward:</p>
<ol class="arabic simple">
<li><p>Is there a system package?</p></li>
</ol>
<blockquote>
<div><ul class="simple">
<li><p>use it (apt-get install the_package)</p></li>
</ul>
</div></blockquote>
<ol class="arabic" start="2">
<li><p>Try <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span></code>: it may just work!</p></li>
<li><p>Install the dependencies, build from source:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">build</span>
<span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">install</span>
</pre></div>
</div>
</li>
</ol>
<p>(may need “something-devel” packages)</p>
</div>
<div class="section" id="windows">
<h4>Windows<a class="headerlink" href="#windows" title="Permalink to this headline"></a></h4>
<p>Sometimes simpler:</p>
<ol class="arabic simple">
<li><p>A lot of packages have Windows wheels now.</p></li>
</ol>
<blockquote>
<div><ul class="simple">
<li><p>Often installable with pip (pip will install a wheel for you if it exists)</p></li>
<li><p>Usually for python.org builds</p></li>
<li><p>Excellent source: <a class="reference external" href="http://www.lfd.uci.edu/~gohlke/pythonlibs/">http://www.lfd.uci.edu/~gohlke/pythonlibs/</a></p></li>
<li><p>Make sure you get 32 or 64 bit consistent</p></li>
</ul>
</div></blockquote>
<ol class="arabic simple" start="2">
<li><p>But if no binaries:</p></li>
</ol>
<blockquote>
<div><ul class="simple">
<li><p>Hope the dependencies are available!</p></li>
<li><p>Set up the compiler</p></li>
</ul>
</div></blockquote>
<p>Each version of Python requires a particular version of the MS Compiler:</p>
<p><a class="reference external" href="https://wiki.python.org/moin/WindowsCompilers#Which_Microsoft_Visual_C.2B-.2B-_compiler_to_use_with_a_specific_Python_version_.3F">MS compiler versions</a></p>
<p>You can get the one for recent Pythons
<a class="reference external" href="https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019">here</a>.</p>
</div>
<div class="section" id="os-x">
<h4>OS-X<a class="headerlink" href="#os-x" title="Permalink to this headline"></a></h4>
<p>Lots of Python versions:</p>
<blockquote>
<div><ul class="simple">
<li><p>Apple’s built-in (different for each version of OS)</p></li>
<li><p>python.org builds</p></li>
<li><p>32+64 bit Intel (and even PPC still kicking around)</p></li>
<li><p>Macports</p></li>
<li><p>Homebrew</p></li>
</ul>
</div></blockquote>
<p>Binary wheels are pretty much compatible between them – yeah!</p>
<p>If you have to build it yourself</p>
<p>Xcode compiler (the right version)</p>
<blockquote>
<div><ul class="simple">
<li><p>Version 3.* for 32 bit PPC+Intel</p></li>
<li><p>Version > 4.* for 32+64 bit Intel</p></li>
</ul>
</div></blockquote>
<p>(make sure to get the SDKs for older versions)</p>
<p>If extra dependencies:</p>
<blockquote>
<div><ul class="simple">
<li><p>macports or homebrew often easiest way to build them</p></li>
</ul>
</div></blockquote>
</div>
</div>
<div class="section" id="final-recommendations">
<h3>Final Recommendations<a class="headerlink" href="#final-recommendations" title="Permalink to this headline"></a></h3>
<p>First try: <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span></code></p>
<p>If that doesn’t work:</p>
<p>Read the docs of the package you want to install</p>
<p>Do what they say</p>
<p>(Or use conda!)</p>
</div>
<div class="section" id="virtualenv">
<h3>virtualenv<a class="headerlink" href="#virtualenv" title="Permalink to this headline"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">virtualenv</span></code> is a tool to create isolated Python environments.</p>
<p>Very useful for developing multiple apps</p>
<p>Or deploying more than one app on one system</p>
<p><a class="reference external" href="http://www.virtualenv.org/en/latest/index.html">http://www.virtualenv.org/en/latest/index.html</a>}</p>
<p>You can find some additional notes here: <a class="reference internal" href="../topics/01-setting_up/virtualenv.html#virtualenv-section"><span class="std std-ref">Working with Virtualenv</span></a></p>
<p><strong>NOTE:</strong> conda also provides a similar isolated environment system.</p>
</div>
</div>
<div class="section" id="building-your-own-package">
<h2>Building Your Own Package<a class="headerlink" href="#building-your-own-package" title="Permalink to this headline"></a></h2>
<p>The term “package” is overloaded in Python. As defined above, it means a collection of python modules. But it often is used to refer to not just the modules themselves, but the whole collection, with documentation and tests, bundled up and installable on other systems.</p>
<p>Here are the very basics of what you need to know to make your own package.</p>
<div class="section" id="why-build-a-package">
<h3>Why Build a Package?<a class="headerlink" href="#why-build-a-package" title="Permalink to this headline"></a></h3>
<p>There are a bunch of nifty tools that help you build, install and
distribute packages.</p>
<p>Using a well structured, standard layout for your package makes it
easy to use those tools.</p>
<p>Even if you never want to give anyone else your code, a well
structured package eases development.</p>
</div>
<div class="section" id="what-is-a-package">
<h3>What is a Package?<a class="headerlink" href="#what-is-a-package" title="Permalink to this headline"></a></h3>
<p><strong>A collection of modules</strong></p>
<ul class="simple">
<li><p>… and the documentation</p></li>
<li><p>… and the tests</p></li>
<li><p>… and any top-level scripts</p></li>
<li><p>… and any data files required</p></li>
<li><p>… and a way to build and install it…</p></li>
</ul>
</div>
<div class="section" id="python-packaging-tools">
<h3>Python packaging tools:<a class="headerlink" href="#python-packaging-tools" title="Permalink to this headline"></a></h3>
<p>The <code class="docutils literal notranslate"><span class="pre">distutils</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
</pre></div>
</div>
<p>Getting klunky, hard to extend, maybe destined for deprecation…</p>
<p>You really need to use <code class="docutils literal notranslate"><span class="pre">setuptools</span></code> these days, which fortunatly has a similar API:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">setup</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span></code>: for installing packages</p>
<p><code class="docutils literal notranslate"><span class="pre">wheel</span></code>: for binary distributions</p>
<p>These are pretty much the standard now – very well maintained by:</p>
<p>“The Python Packaging Authority” – <a class="reference external" href="https://www.pypa.io/en/latest/">PaPA</a></p>
<p>This all continues to change quickly, see that site for up to date information.</p>
</div>
<div class="section" id="where-do-i-go-to-figure-this-out">
<h3>Where do I go to figure this out?<a class="headerlink" href="#where-do-i-go-to-figure-this-out" title="Permalink to this headline"></a></h3>
<p>This is a really good guide:</p>
<p>Python Packaging User Guide:</p>
<p><a class="reference external" href="https://packaging.python.org/">https://packaging.python.org/</a></p>
<p>and a more detailed tutorial:</p>
<p><a class="reference external" href="http://python-packaging.readthedocs.io/en/latest/">http://python-packaging.readthedocs.io/en/latest/</a></p>
<p><strong>Follow one of them</strong></p>
<p>There is a sample project here:</p>
<p><a class="reference external" href="https://github.com/pypa/sampleproject">https://github.com/pypa/sampleproject</a></p>
<p>(this has all the complexity you might need…)</p>
<p>You can use this as a template for your own packages.</p>
<p>Here is an opinionated update – a little more fancy, but some good ideas:</p>
<p><a class="reference external" href="https://blog.ionelmc.ro/2014/05/25/python-packaging/">https://blog.ionelmc.ro/2014/05/25/python-packaging/</a></p>
<p>Rather than doing it by hand, you can use the nifty “cookie cutter” project:</p>
<p><a class="reference external" href="https://cookiecutter.readthedocs.io/en/latest/">https://cookiecutter.readthedocs.io/en/latest/</a></p>
<p>And there are a few templates that can be used with that.</p>
<p>The core template written by the author:</p>
<p><a class="reference external" href="https://github.com/audreyr/cookiecutter-pypackage">https://github.com/audreyr/cookiecutter-pypackage</a></p>
<p>And one written by the author of the opinionated blog post above:</p>
<p><a class="reference external" href="https://github.com/ionelmc/cookiecutter-pylibrary">https://github.com/ionelmc/cookiecutter-pylibrary</a></p>
<p>Either are great starting points.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>One confusion for folks new to this is that a LOT of the documentation (and tools) around packaging for Python assumes that you are writing a package that is generally useful, and you want to share it with others on PyPi. That is partly because all the people developing the tools and writing about them are doing just that. It’s also harder to distribute a package properly than to simply make one for internal use, so more tools and docs are needed. But it is still useful to make a package of your code if you aren’t going to distribute it, but you don’t need to do everything that is recommended. See: <a class="reference external" href="http://pythonchb.github.io/PythonTopics/where_to_put_your_code.html">A Package Just for You</a> for a really simple way to do the basics.</p>
</div>
</div>
<div class="section" id="basic-package-structure">
<h3>Basic Package Structure:<a class="headerlink" href="#basic-package-structure" title="Permalink to this headline"></a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">package_name</span><span class="o">/</span>
<span class="nb">bin</span><span class="o">/</span>
<span class="n">CHANGES</span><span class="o">.</span><span class="n">txt</span>
<span class="n">docs</span><span class="o">/</span>
<span class="n">LICENSE</span><span class="o">.</span><span class="n">txt</span>
<span class="n">MANIFEST</span><span class="o">.</span><span class="ow">in</span>
<span class="n">README</span><span class="o">.</span><span class="n">txt</span>
<span class="n">setup</span><span class="o">.</span><span class="n">py</span>
<span class="n">package_name</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">module1</span><span class="o">.</span><span class="n">py</span>
<span class="n">module2</span><span class="o">.</span><span class="n">py</span>
<span class="n">test</span><span class="o">/</span>
<span class="fm">__init__</span><span class="o">.</span><span class="n">py</span>
<span class="n">test_module1</span><span class="o">.</span><span class="n">py</span>
<span class="n">test_module2</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">CHANGES.txt</span></code>: log of changes with each release</p>
<p><code class="docutils literal notranslate"><span class="pre">LICENSE.txt</span></code>: text of the license you choose (do choose one!)</p>
<p><code class="docutils literal notranslate"><span class="pre">MANIFEST.in</span></code>: description of what non-code files to include</p>
<p><code class="docutils literal notranslate"><span class="pre">README.txt</span></code>: description of the package – should be written in ReST (for PyPi):</p>
<p>(<a class="reference external" href="http://docutils.sourceforge.net/rst.html">http://docutils.sourceforge.net/rst.html</a>)</p>
<p>(those are all “metadata” critical if you are distributing to the world – not so much for your own use)</p>
<p><code class="docutils literal notranslate"><span class="pre">setup.py</span></code>: <code class="docutils literal notranslate"><span class="pre">distutils</span></code>/<code class="docutils literal notranslate"><span class="pre">setuptools</span></code> script for building/installing the package.</p>
<p><code class="docutils literal notranslate"><span class="pre">bin/</span></code>: This is where you put top-level scripts</p>
<blockquote>
<div><p>( some folks use <code class="docutils literal notranslate"><span class="pre">scripts</span></code> )</p>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">docs/</span></code>: the documentation</p>
<p><code class="docutils literal notranslate"><span class="pre">package_name/</span></code>: The main package – this is where the code goes.</p>
<p><code class="docutils literal notranslate"><span class="pre">test/</span></code>: your unit tests. Options here:</p>
<p>Put it inside the package – this results in the tests getting isntalled with the package, so they can be run after installation, with:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pip install package_name
>> import package_name.test
>> package_name.test.runall()
</pre></div>
</div>
<p>or</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pytest --pyargs package_name
</pre></div>
</div>
<p>Or, if you have a lot of tests, and do not want the entire set installed with the package, you can keep it at the top level.</p>
<p>Some notes on that: <a class="reference external" href="http://pythonchb.github.io/PythonTopics/where_to_put_tests.html">Where to put Tests</a></p>
</div>
<div class="section" id="the-setup-py-file">
<h3>The <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> File<a class="headerlink" href="#the-setup-py-file" title="Permalink to this headline"></a></h3>
<p>Your <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file is what describes your package, and tells the setuptools how to package, build, and install it</p>
<p>It is python code, so you can add anything custom you need to it.</p>
<p>But in the simple case, it is essentially declarative.</p>
<p><code class="docutils literal notranslate"><span class="pre">http://docs.python.org/3/distutils/</span></code></p>
<div class="section" id="an-example">
<h4>An example:<a class="headerlink" href="#an-example" title="Permalink to this headline"></a></h4>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span> <span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">setup</span>
<span class="n">setup</span><span class="p">(</span>
<span class="c1"># the critical stuff</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'PackageName'</span><span class="p">,</span>
<span class="n">packages</span><span class="o">=</span><span class="p">[</span><span class="s1">'package_name'</span><span class="p">,</span> <span class="s1">'package_name.test'</span><span class="p">],</span>
<span class="n">scripts</span><span class="o">=</span><span class="p">[</span><span class="s1">'bin/script1'</span><span class="p">,</span><span class="s1">'bin/script2'</span><span class="p">],</span>
<span class="c1"># the good to have stuff: particularly if you are distributing it</span>
<span class="n">version</span><span class="o">=</span><span class="s1">'0.1.0'</span><span class="p">,</span>
<span class="n">author</span><span class="o">=</span><span class="s1">'An Awesome Coder'</span><span class="p">,</span>
<span class="n">author_email</span><span class="o">=</span><span class="s1">'aac@example.com'</span><span class="p">,</span>
<span class="n">url</span><span class="o">=</span><span class="s1">'http://pypi.python.org/pypi/PackageName/'</span><span class="p">,</span>
<span class="n">license</span><span class="o">=</span><span class="s1">'LICENSE.txt'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s1">'An awesome package that does something'</span><span class="p">,</span>
<span class="n">long_description</span><span class="o">=</span><span class="nb">open</span><span class="p">(</span><span class="s1">'README.txt'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">(),</span>
<span class="n">install_requires</span><span class="o">=</span><span class="p">[</span>
<span class="s2">"Django >= 1.1.1"</span><span class="p">,</span>
<span class="s2">"pytest"</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="setup-cfg">
<h3><code class="docutils literal notranslate"><span class="pre">setup.cfg</span></code><a class="headerlink" href="#setup-cfg" title="Permalink to this headline"></a></h3>
<p><strong>NOTE:</strong> this is usually a pretty advanced option – simple packages don’t need this.</p>
<p><code class="docutils literal notranslate"><span class="pre">setup.cfg</span></code> provides a way to give the end user some ability to customize the install</p>
<p>It’s an <code class="docutils literal notranslate"><span class="pre">ini</span></code> style file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">command</span><span class="p">]</span>
<span class="n">option</span><span class="o">=</span><span class="n">value</span>
<span class="o">...</span>
</pre></div>
</div>
<p>simple to read and write.</p>
<p><code class="docutils literal notranslate"><span class="pre">command</span></code> is one of the distutils commands (e.g. build, install)</p>
<p><code class="docutils literal notranslate"><span class="pre">option</span></code> is one of the options that command supports.</p>
<p>Note that an option spelled <code class="docutils literal notranslate"><span class="pre">--foo-bar</span></code> on the command-line is spelled f``foo_bar`` in configuration files.</p>
</div>
<div class="section" id="running-setup-py">
<h3>Running <code class="docutils literal notranslate"><span class="pre">setup.py</span></code><a class="headerlink" href="#running-setup-py" title="Permalink to this headline"></a></h3>
<p>With a <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> script defined, setuptools, along with pip, can do a lot:</p>
<ul>
<li><p>builds a source distribution (a tar archive of all the files needed to build and install the package):</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">sdist</span>
</pre></div>
</div>
</li>
<li><p>builds wheels:</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="n">bdist_wheel</span>
</pre></div>
</div>
</li>
</ul>
<p>(you need the wheel package for this to work: <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">wheel</span></code>)</p>
<ul>
<li><p>build from source:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">build</span>
</pre></div>
</div>
</li>
<li><p>and install:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">install</span>
</pre></div>
</div>
</li>
</ul>
<p>or:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="o">.</span>
</pre></div>
</div>
<p>(the dot means “this directory” – pip will look in the current dir for a <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file)</p>
<ul>
<li><p>install in “develop” or “editable” mode:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">develop</span>
</pre></div>
</div>
</li>
</ul>
<p>or:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pip</span> <span class="n">install</span> <span class="o">-</span><span class="n">e</span> <span class="o">.</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>setuptools can be used by itself to build and install packages. But over the years, pip has evolved to a more “modern” way of doing things. When you install from source with pip – it is using setuptools to do the work, but it changes things around, and installs things in a more modern, up to date, and compatible way. For much use, you won’t notice the difference, but it setuptools still has some old crufty ways of doing things, so it’s better to use pip as a front end as much as possible.</p>
</div>
</div>
<div class="section" id="setuptools">
<h3>setuptools<a class="headerlink" href="#setuptools" title="Permalink to this headline"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">setuptools</span></code> is an extension to <code class="docutils literal notranslate"><span class="pre">distutils</span></code> that provides a number of extensions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">setuptools</span> <span class="kn">import</span> <span class="n">setup</span>
</pre></div>
</div>
<p>superset of the <code class="docutils literal notranslate"><span class="pre">distutils</span> <span class="pre">setup</span></code></p>
<p>This buys you a bunch of additional functionality:</p>
<blockquote>
<div><ul class="simple">
<li><p>auto-finding packages</p></li>
<li><p>better script installation</p></li>
<li><p>resource (non-code files) management</p></li>
<li><p><strong>develop mode</strong></p></li>
<li><p>a LOT more</p></li>
</ul>
</div></blockquote>
<p>In fact, virtually all python packages use setuptools these days, and there is currently discussion of deprecating distutils, and making setuptools “official”. So you really want to use it.</p>
<p><a class="reference external" href="http://pythonhosted.org//setuptools/">http://pythonhosted.org//setuptools/</a></p>
</div>
<div class="section" id="wheels">
<h3>wheels<a class="headerlink" href="#wheels" title="Permalink to this headline"></a></h3>
<p>Wheels are a binary format for packages.</p>
<p><a class="reference external" href="http://wheel.readthedocs.org/en/latest/">http://wheel.readthedocs.org/en/latest/</a></p>
<p>Pretty simple, essentially a zip archive of all the stuff that gets installed, i.e. put in <code class="docutils literal notranslate"><span class="pre">site-packages</span></code>.</p>
<p>Can be just pure python or binary with compiled extensions</p>
<p>Compatible with virtualenv.</p>
<p>Building a wheel:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">bdist_wheel</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">packagename</span></code> will find wheels for Windows and OS-X and “manylinux”</p>
<p><code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">--no-use-wheel</span></code> avoids that, and forces a source install.</p>
</div>
<div class="section" id="manylinux">
<h3>manylinux<a class="headerlink" href="#manylinux" title="Permalink to this headline"></a></h3>
<p>There are a lot of Linux distributions out there. So for a long time, there were not easily available binary wheels for Linux – how could you define a standard with all the Linux distros out there?</p>
<p>Enter “manylinux” – no one thinks you can support all Linux distros, but it was found that you could support many of the common ones, by building on an older version and restricting system libraries. This approach worked well for Canopy and conda, so PyPi adopted a similar strategy with manylinux:</p>
<p><a class="reference external" href="https://github.com/pypa/manylinux">https://github.com/pypa/manylinux</a></p>
<p>So there are now binary wheels for Linux on PyPi.</p>
<p>The core scipy stack is a great example – you can now <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">numpy</span></code> on all three systems easily with pip.</p>
</div>
<div class="section" id="pypi">
<h3>PyPi<a class="headerlink" href="#pypi" title="Permalink to this headline"></a></h3>
<p>The Python package index:</p>
<p><a class="reference external" href="https://pypi.python.org/pypi">https://pypi.python.org/pypi</a></p>
<p>You’ve all used this – <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span></code> searches it.</p>
<p>To upload your package to PyPi:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">register</span>
<span class="n">python</span> <span class="n">setup</span><span class="o">.</span><span class="n">py</span> <span class="n">sdist</span> <span class="n">bdist_wheel</span> <span class="n">upload</span>
</pre></div>
</div>
<p><a class="reference external" href="http://docs.python.org/2/distutils/packageindex.html">http://docs.python.org/2/distutils/packageindex.html</a></p>
<p>NOTE: only do this if you really want to share your package with the world!</p>
</div>
<div class="section" id="under-development">
<h3>Under Development<a class="headerlink" href="#under-development" title="Permalink to this headline"></a></h3>
<p>Develop mode (or “editable install”) is <em>really</em> <em>really</em> nice:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ python setup.py develop
</pre></div>
</div>
<p>or:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ pip install -e ./
</pre></div>
</div>
<p>(the e stands for “editable” – it is the same thing)</p>
<p>It puts links into the python installation to your code, so that your package is installed, but any changes will immediately take effect.</p>
<p>This way all your test code, and client code, etc, can all import your package the usual way.</p>
<p>No <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> hacking</p>
<p>Good idea to use it for anything more than a single file project.</p>
<p>(requires <code class="docutils literal notranslate"><span class="pre">setuptools</span></code>)</p>
</div>
<div class="section" id="running-tests">
<h3>Running tests<a class="headerlink" href="#running-tests" title="Permalink to this headline"></a></h3>
<p>It can be a good idea to set up your tests to be run from <code class="docutils literal notranslate"><span class="pre">setup.py</span></code></p>
<p>So that you (or your users) can:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install .
$ python setup.py <span class="nb">test</span>
</pre></div>
</div>
<p><strong>Note:</strong> there is debate about whether this is a good idea. But if you want to:</p>
<p>To do this, you need to add a <code class="docutils literal notranslate"><span class="pre">test_suite</span></code> stanza in setup.py.</p>
<p><strong>pytest</strong></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">setup</span><span class="p">(</span>
<span class="c1">#...,</span>
<span class="n">setup_requires</span><span class="o">=</span><span class="p">[</span><span class="s1">'pytest-runner'</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span>
<span class="n">tests_require</span><span class="o">=</span><span class="p">[</span><span class="s1">'pytest'</span><span class="p">,</span> <span class="o">...</span><span class="p">],</span>
<span class="c1">#...,</span>
<span class="p">)</span>
</pre></div>
</div>
<p>And create an alias into setup.cfg file:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">aliases</span><span class="p">]</span>
<span class="n">test</span><span class="o">=</span><span class="n">pytest</span>
</pre></div>
</div>
<p><a class="reference external" href="https://pytest.org/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner">https://pytest.org/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner</a></p>
<p>This may not be required, as pytest will also let you run the tests installed with a package with:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">--</span><span class="n">pyargs</span> <span class="n">package_name</span>
</pre></div>
</div>
<p><strong>unittest</strong></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">test_suite</span><span class="o">=</span><span class="s2">"tests"</span>
</pre></div>
</div>
</div>
<div class="section" id="handling-the-version-number">
<h3>Handling the version number:<a class="headerlink" href="#handling-the-version-number" title="Permalink to this headline"></a></h3>
<p>One key rule in software (and ANY computer use!):</p>
<p>Never put the same information in more than one place!</p>
<p>With a python package, you want:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">the_package</span>
<span class="n">the_package</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>
</div>
<p>To return the version string – something like:</p>
<p>“1.2.3”</p>
<p>Using <code class="docutils literal notranslate"><span class="pre">__version__</span></code> is not a requirement, but it is a very commonly used convention – <em>use it</em>!</p>
<p>But you also need to specify it in the <code class="docutils literal notranslate"><span class="pre">setup.py</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">setup</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">'package_name'</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s2">"1.2.3"</span><span class="p">,</span>
<span class="o">...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Not Good.</p>
<div class="section" id="my-solution">
<h4>My solution:<a class="headerlink" href="#my-solution" title="Permalink to this headline"></a></h4>
<p>Put the version in the package __init__</p>
<p>__version__ = “1.2.3”</p>
<p>In the setup.py, you could import the package to get the version
number … but it’s not a safe practice to import your package when installing
it (or building it, or…)</p>
<p>So: read the <code class="docutils literal notranslate"><span class="pre">__version__</span></code> string yourself with code like:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">get_version</span><span class="p">():</span>
<span class="sd">"""</span>
<span class="sd"> Reads the version string from the package __init__ and returns it</span>
<span class="sd"> """</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s2">"capitalize"</span><span class="p">,</span> <span class="s2">"__init__.py"</span><span class="p">))</span> <span class="k">as</span> <span class="n">init_file</span><span class="p">:</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">init_file</span><span class="p">:</span>
<span class="n">parts</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">partition</span><span class="p">(</span><span class="s2">"="</span><span class="p">)</span>
<span class="k">if</span> <span class="n">parts</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s2">"__version__"</span><span class="p">:</span>
<span class="k">return</span> <span class="n">parts</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s2">"'"</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">'"'</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">None</span>
</pre></div>
</div>
<p><strong>Alternative:</strong></p>
<p>You can have a script that automatically updates the version number in whatever places it needs to. For instance:</p>
<p><a class="reference external" href="https://pypi.python.org/pypi/bumpversion">https://pypi.python.org/pypi/bumpversion</a></p>
<p>Though I think it’s better to have the version set in the code itself.</p>
</div>
</div>
<div class="section" id="semantic-versioning">
<h3>Semantic Versioning<a class="headerlink" href="#semantic-versioning" title="Permalink to this headline"></a></h3>
<p>Another note on version numbers.</p>
<p>The software development world (at least the open-source one…) has
established a standard for what version numbers mean, known as semantic
versioning. This is helpful to users, as they can know what to expect
they upgrade.</p>
<p>In short, with a x.y.z version number:</p>
<p>x is the Major Version – it could mean changes in API, major features, etc.</p>
<blockquote>
<div><ul class="simple">
<li><p>Likely to to be incompatible with previous versions</p></li>
</ul>
</div></blockquote>
<p>y is the Minor Version – added features, etc, that are backwards compatible.</p>
<p>z is the “Patch” Version – bug fixes, etc. – should be fully compatible.</p>
<p>Read all about it:</p>
<p><a class="reference external" href="http://semver.org/">http://semver.org/</a></p>
</div>
<div class="section" id="tools-to-help">
<h3>Tools to help:<a class="headerlink" href="#tools-to-help" title="Permalink to this headline"></a></h3>
<p>Tox:</p>
<p><a class="reference external" href="https://tox.readthedocs.io/en/latest/">https://tox.readthedocs.io/en/latest/</a></p>
<p>Versioneer:</p>
<p><a class="reference external" href="https://github.com/warner/python-versioneer">https://github.com/warner/python-versioneer</a></p>
</div>
<div class="section" id="dealing-with-data-files">
<h3>Dealing with data files<a class="headerlink" href="#dealing-with-data-files" title="Permalink to this headline"></a></h3>
<p>Oftentimes a package will require some files that are not Python code. In that case, you need to make sure the files are included with the package some how.</p>
<p>There are a few ways to do this:</p>
<p><a class="reference external" href="http://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files">http://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files</a></p>
<div class="section" id="the-simplest-option-package-data">
<h4>The simplest option: package_data<a class="headerlink" href="#the-simplest-option-package-data" title="Permalink to this headline"></a></h4>
<p>I personally like the simplest one with the least magic:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">setup</span><span class="p">(</span>
<span class="o">...</span>
<span class="n">package_data</span><span class="o">=</span><span class="p">{</span><span class="s1">'pkg_name'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'data/datatfile1'</span><span class="p">,</span>
<span class="s1">'data/datafile2'</span><span class="p">]},</span>
<span class="o">...</span>
<span class="p">)</span>
</pre></div>
</div>
<p>This is a dict with the keys being the package(s) you want to add data files to. This is required, as a single setup command can install more than one package. The value(s) is a list of filenames, <em>relative to the package</em> - note that in the above example, the “pkg_name” is not part of the path to the file.</p>
<p>WARNING: For some reason, setuptools does not give you an error or warning if it can’t find the files you specify – which is a real shame - makes it harder to debug.</p>
<p><a class="reference external" href="https://packaging.python.org/tutorials/distributing-packages/#package-data">https://packaging.python.org/tutorials/distributing-packages/#package-data</a></p>
<p>Then you’ll get the data file included in the package in the same place relative to your code regardless of how (or whether) it is installed.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Debugging package building can be kind of tricky: if you install the package, and it doesn’t work, what went wrong?!? One approach that can help is to “build” the package, separately from installing it. setuptools provides a build command: <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">setup.py</span> <span class="pre">build</span></code> that does just that. It will create a <code class="docutils literal notranslate"><span class="pre">build</span></code> directory, and in there, a <code class="docutils literal notranslate"><span class="pre">lib</span></code> dir. In there is what will actually get installed – your “built” package. So you can look there and see if your data files are getting included, and everything else about the package.</p>
</div>
<p>Now you’ll need to write your code to find that data file. You can do that by using the <code class="docutils literal notranslate"><span class="pre">__file__</span></code> module attribute – then the location of the data file will be relative to the <code class="docutils literal notranslate"><span class="pre">__file__</span></code> that your code is in. A little massaging with a <code class="docutils literal notranslate"><span class="pre">pathlib.Path</span></code> should do it. Putting the path to the data directory in the package’s <code class="docutils literal notranslate"><span class="pre">__init__.py</span></code> provides a way for the rest of your code to find it.</p>
<p>In <code class="docutils literal notranslate"><span class="pre">pkg_name/__init__.py</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>
<span class="n">data_dir</span> <span class="o">=</span> <span class="n">data_file</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="s2">"data"</span>
</pre></div>
</div>
<p>Now you can get that dir anywhere else in your code:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pkg_name</span> <span class="kn">import</span> <span class="n">data_dir</span>
</pre></div>
</div>
</div>
<div class="section" id="more-complex-option-data-files">
<h4>More complex option: data_files<a class="headerlink" href="#more-complex-option-data-files" title="Permalink to this headline"></a></h4>
<p>Using the data_files setup option lets you put data files outside your package. They will get installed into the path of <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code>, so you can find them in your code with a path relative to <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code>.</p>
<p>However, this means that the location of the files is different depending on whether the code is properly installed or not. And develop mode (or editable mode) does NOT install the data files.</p>
<p>I honestly can’t think of any reason to do this.</p>
<p><a class="reference external" href="https://packaging.python.org/tutorials/distributing-packages/#data-files">https://packaging.python.org/tutorials/distributing-packages/#data-files</a></p>