forked from pantsbuild/pantsbuild.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_old.html
1528 lines (1411 loc) · 60.2 KB
/
python_old.html
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 lang="en">
<!--
Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
Licensed under the Apache License, Version 2.0 (see LICENSE).
-->
<head>
<meta charset="utf-8" />
<title>Using Pants for Python development, 2014 and Earlier</title>
<link rel="stylesheet" href="s.css">
<script src="s.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="logo.ico">
<!-- In case this is a "test publish", tell search engines where real version lives: -->
<link rel="canonical" href="https://pantsbuild.github.io/python_old.html">
</head>
<body>
<div id="navtoggler">
◄ <!-- triangle -->
</div>
<div class="mainflow">
<nav class="breadcrumbs">
<ul>
<li><a href="index.html">Pants Build</a>
<li><a href="python-readme.html">Python Projects with Pants</a>
<li><a href="python_old.html">Using Pants for Python development, 2014 and Earlier</a>
</ul>
</nav>
<nav class="pagetoc">
<ul>
<li class="toc-h1"><a href="#why-use-pants-for-python-development">Why use Pants for Python development?</a></li>
<li class="toc-h2"><a href="#alternatives">Alternatives</a></li>
<li class="toc-h2"><a href="#pants-and-pex">Pants and PEX</a></li>
<li class="toc-h1"><a href="#getting-started">Getting started</a></li>
<li class="toc-h1"><a href="#tldr-hello-world-with-pants-python">TL;DR - 'Hello world!' with Pants Python</a></li>
<li class="toc-h1"><a href="#describing-python-environments-in-pants">Describing Python environments in Pants</a></li>
<li class="toc-h2"><a href="#build-file-format">BUILD file format</a></li>
<li class="toc-h2"><a href="#addressing-targets">Addressing targets</a></li>
<li class="toc-h2"><a href="#python-target-types">Python target types</a></li>
<li class="toc-h3"><a href="#python95library">python_library</a></li>
<li class="toc-h3"><a href="#python95binary">python_binary</a></li>
<li class="toc-h3"><a href="#python95requirement">python_requirement</a></li>
<li class="toc-h3"><a href="#python95thrift95library">python_thrift_library</a></li>
<li class="toc-h3"><a href="#python95tests">python_tests</a></li>
<li class="toc-h1"><a href="#building-your-first-pex">Building your first PEX</a></li>
<li class="toc-h1"><a href="#environment-manipulation-with-pants-py">Environment manipulation with pants py</a></li>
<li class="toc-h2"><a href="#pants-py-semantics">pants py semantics</a></li>
<li class="toc-h2"><a href="#external-dependencies">external dependencies</a></li>
<li class="toc-h2"><a href="#running-an-application-using-pants-py">Running an application using pants py</a></li>
<li class="toc-h2"><a href="#pants-py-pex">pants py --pex</a></li>
<li class="toc-h2"><a href="#pex-file-as-interpreter">PEX file as interpreter</a></li>
<li class="toc-h2"><a href="#python95binary-entry95point">python_binary entry_point</a></li>
<li class="toc-h1"><a href="#python-tests">Python Tests</a></li>
<li class="toc-h2"><a href="#defining-python95tests-targets">Defining python_tests Targets</a></li>
<li class="toc-h2"><a href="#running-python-tests">Running Python Tests</a></li>
<li class="toc-h2"><a href="#getting-python-code-coverage">Getting Python Code Coverage</a></li>
<li class="toc-h2"><a href="#interactve-debugging-on-test-failure">Interactve Debugging on Test Failure</a></li>
<li class="toc-h2"><a href="#using-other-testing-frameworks">Using Other Testing Frameworks</a></li>
<li class="toc-h1"><a href="#manipulating-pex-behavior-with-environment-variables">Manipulating PEX behavior with environment variables</a></li>
<li class="toc-h2"><a href="#pex95interpreter1">PEX_INTERPRETER=1</a></li>
<li class="toc-h2"><a href="#pex95verbose1">PEX_VERBOSE=1</a></li>
<li class="toc-h2"><a href="#pex95moduleentry95point">PEX_MODULE=entry_point</a></li>
<li class="toc-h2"><a href="#pex95coverage">PEX_COVERAGE</a></li>
<li class="toc-h1"><a href="#how-pex-files-work">How PEX files work</a></li>
<li class="toc-h2"><a href="#the-utility-of-zipimport-and-9595main9595py">the utility of zipimport and __main__.py</a></li>
<li class="toc-h2"><a href="#pex-9595main9595py">PEX __main__.py</a></li>
<li class="toc-h1"><a href="#advanced-pantspex-features">Advanced Pants/PEX features</a></li>
</ul>
</nav>
<!-- main content start -->
<!-- generated by pants! -->
<style>
.codehilite .hll { background-color: #ffffcc }
.codehilite { background: #f0f0f0; }
.codehilite .c { color: #60a0b0; font-style: italic } /* Comment */
.codehilite .err { border: 1px solid #FF0000 } /* Error */
.codehilite .k { color: #007020; font-weight: bold } /* Keyword */
.codehilite .o { color: #666666 } /* Operator */
.codehilite .cm { color: #60a0b0; font-style: italic } /* Comment.Multiline */
.codehilite .cp { color: #007020 } /* Comment.Preproc */
.codehilite .c1 { color: #60a0b0; font-style: italic } /* Comment.Single */
.codehilite .cs { color: #60a0b0; background-color: #fff0f0 } /* Comment.Special */
.codehilite .gd { color: #A00000 } /* Generic.Deleted */
.codehilite .ge { font-style: italic } /* Generic.Emph */
.codehilite .gr { color: #FF0000 } /* Generic.Error */
.codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.codehilite .gi { color: #00A000 } /* Generic.Inserted */
.codehilite .go { color: #808080 } /* Generic.Output */
.codehilite .gp { color: #c65d09; font-weight: bold } /* Generic.Prompt */
.codehilite .gs { font-weight: bold } /* Generic.Strong */
.codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.codehilite .gt { color: #0040D0 } /* Generic.Traceback */
.codehilite .kc { color: #007020; font-weight: bold } /* Keyword.Constant */
.codehilite .kd { color: #007020; font-weight: bold } /* Keyword.Declaration */
.codehilite .kn { color: #007020; font-weight: bold } /* Keyword.Namespace */
.codehilite .kp { color: #007020 } /* Keyword.Pseudo */
.codehilite .kr { color: #007020; font-weight: bold } /* Keyword.Reserved */
.codehilite .kt { color: #902000 } /* Keyword.Type */
.codehilite .m { color: #40a070 } /* Literal.Number */
.codehilite .s { color: #4070a0 } /* Literal.String */
.codehilite .na { color: #4070a0 } /* Name.Attribute */
.codehilite .nb { color: #007020 } /* Name.Builtin */
.codehilite .nc { color: #0e84b5; font-weight: bold } /* Name.Class */
.codehilite .no { color: #60add5 } /* Name.Constant */
.codehilite .nd { color: #555555; font-weight: bold } /* Name.Decorator */
.codehilite .ni { color: #d55537; font-weight: bold } /* Name.Entity */
.codehilite .ne { color: #007020 } /* Name.Exception */
.codehilite .nf { color: #06287e } /* Name.Function */
.codehilite .nl { color: #002070; font-weight: bold } /* Name.Label */
.codehilite .nn { color: #0e84b5; font-weight: bold } /* Name.Namespace */
.codehilite .nt { color: #062873; font-weight: bold } /* Name.Tag */
.codehilite .nv { color: #bb60d5 } /* Name.Variable */
.codehilite .ow { color: #007020; font-weight: bold } /* Operator.Word */
.codehilite .w { color: #bbbbbb } /* Text.Whitespace */
.codehilite .mf { color: #40a070 } /* Literal.Number.Float */
.codehilite .mh { color: #40a070 } /* Literal.Number.Hex */
.codehilite .mi { color: #40a070 } /* Literal.Number.Integer */
.codehilite .mo { color: #40a070 } /* Literal.Number.Oct */
.codehilite .sb { color: #4070a0 } /* Literal.String.Backtick */
.codehilite .sc { color: #4070a0 } /* Literal.String.Char */
.codehilite .sd { color: #4070a0; font-style: italic } /* Literal.String.Doc */
.codehilite .s2 { color: #4070a0 } /* Literal.String.Double */
.codehilite .se { color: #4070a0; font-weight: bold } /* Literal.String.Escape */
.codehilite .sh { color: #4070a0 } /* Literal.String.Heredoc */
.codehilite .si { color: #70a0d0; font-style: italic } /* Literal.String.Interpol */
.codehilite .sx { color: #c65d09 } /* Literal.String.Other */
.codehilite .sr { color: #235388 } /* Literal.String.Regex */
.codehilite .s1 { color: #4070a0 } /* Literal.String.Single */
.codehilite .ss { color: #517918 } /* Literal.String.Symbol */
.codehilite .bp { color: #007020 } /* Name.Builtin.Pseudo */
.codehilite .vc { color: #bb60d5 } /* Name.Variable.Class */
.codehilite .vg { color: #bb60d5 } /* Name.Variable.Global */
.codehilite .vi { color: #bb60d5 } /* Name.Variable.Instance */
.codehilite .il { color: #40a070 } /* Literal.Number.Integer.Long */
</style>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h1 id="using-pants-for-python-development-2014-and-earlier">Using Pants for Python development, 2014 and Earlier</h1></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#using-pants-for-python-development-2014-and-earlier">¶</a>
</div></td>
</tr>
</tbody>
</table>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="why-use-pants-for-python-development">Why use Pants for Python development?</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#why-use-pants-for-python-development">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>Pants makes the manipulation and distribution of hermetically sealed
Python environments painless.</p>
<p>But why another system?</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="alternatives">Alternatives</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#alternatives">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>There are several solutions for package management in Python. Almost
everyone is familiar with running sudo easy_install PackageXYZ. This
leaves a lot to be desired. Over time, your Python installation will
collect dozens of packages, become annoyingly slow or even broken, and
reinstalling it will invariably break a number of the applications that
you were using.</p>
<p>A marked improvement over the sudo easy_install model is
<a href="http://www.virtualenv.org">virtualenv</a> to isolate Python environments
on a project by project basis. This is useful for development but does
not directly solve any problems related to deployment, whether it be to
a production environment or to your peers. It is also challenging to
explain to a Python non-expert.</p>
<p>A different solution altogether, <a href="http://www.buildout.org/">zc.buildout</a>
attempts to provide a framework and recipes for many common development
environments. It has arguably gone the farthest for automating
environment reproducibility amongst the popular tools, but shares the
same complexity problems as all the other abovementioned solutions.</p>
<p>Most solutions leave deployment as an afterthought. Why not make the
development and deployment environments the same by taking the
environment along with you?</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="pants-and-pex">Pants and PEX</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#pants-and-pex">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>The lingua franca of Pants is the PEX file (PEX itself does not stand
for anything in particular, though in spirit you can think of it as a
"Python EXecutable".)</p>
<p><strong>PEX files are single-file lightweight virtual Python environments.</strong></p>
<p>The only difference is no virtualenv setup instructions or
pip install foo bar baz. PEX files are self-bootstrapping Python
environments with no strings attached and no side-effects. Just a simple
mechanism that unifies both your development and your deployment.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="getting-started">Getting started</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#getting-started">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>First it is necessary to install Pants. See
<a href="install.html">Installing Pants</a>.</p>
<p>It is also helpful to read the
<a href="first_concepts.html">Pants Conceptual Overview</a>.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="tldr-hello-world-with-pants-python">TL;DR - 'Hello world!' with Pants Python</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#tldr-hello-world-with-pants-python">¶</a>
</div></td>
</tr>
</tbody>
</table>
<div class="codehilite"><pre>$ git clone git://github.com/twitter/commons
$ cd commons
$ mkdir -p src/python/twitter/my_project
$ vi src/python/twitter/my_project/BUILD
</pre></div>
<p><code>src/python/twitter/my_project/BUILD</code>:</p>
<div class="codehilite"><pre>python_binary(
name = 'hello_world',
source = 'hello_world.py'
)
$ vi src/python/twitter/my_project/hello_world.py
</pre></div>
<p><code>src/python/twitter/my_project/hello_world.py</code> might have contents:</p>
<div class="codehilite"><pre>print('Hello world!')
</pre></div>
<p>To run directly:</p>
<div class="codehilite"><pre>$ ./pants py src/python/twitter/my_project:hello_world
Build operating on target: PythonBinary(src/python/twitter/my_project/BUILD:hello_world)
Hello world!
</pre></div>
<p>To build:</p>
<div class="codehilite"><pre>$ ./pants src/python/twitter/my_project:hello_world
Build operating on targets: OrderedSet([PythonBinary(src/python/twitter/my_project/BUILD:hello_world)])
Building PythonBinary PythonBinary(src/python/twitter/my_project/BUILD:hello_world):
Wrote /Users/wickman/clients/science-py-csl/dist/hello_world.pex
</pre></div>
<p>and run separately:</p>
<div class="codehilite"><pre>$ dist/hello_world.pex
Hello world!
</pre></div>
<p>NOTE: The first time you run <code>./pants</code> will likely take a ridiculous
amount of time as Pants bootstraps itself inside your directory. Note,
it never installs anything in a global site-packages.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="describing-python-environments-in-pants">Describing Python environments in Pants</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#describing-python-environments-in-pants">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>Build dependencies in Pants are managed with <code>BUILD</code> files that are
co-located with your source. These files are used to describe the
following:</p>
<ol>
<li>libraries: bundles of sources and resources, that may or may not
also depend on other libraries</li>
<li>binaries: a single source (the executable) and libraries it depends
upon</li>
<li>requirements: external dependencies as resolved by dependency
managers e.g. pypi in Python or ivy on the JVM</li>
</ol>
<p>The main point of Pants is to take these BUILD files and do something
useful with them.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="build-file-format">BUILD file format</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#build-file-format">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>These descriptions are stored in files named BUILD and colocated near
the binaries/libraries they describe. Let's take for example the
src/python/twitter/tutorial subtree in commons:</p>
<div class="codehilite"><pre>$ ls -lR src/python/twitter/tutorial/
total 16
-rw-r--r-- 1 wickman wheel 137 Apr 9 22:59 BUILD
-rw-r--r-- 1 wickman wheel 118 Apr 9 22:59 hello_world.py
</pre></div>
<p>Let's take a look at the BUILD file in
<code>src/python/twitter/tutorial/BUILD</code>:</p>
<div class="codehilite"><pre>python_binary(
name = "hello_world",
source = "hello_world.py",
dependencies = [
pants("src/python/twitter/common/app"),
]
)
</pre></div>
<p>This BUILD file names one target: hello_world, which is a
<a href="build_dictionary.html#bdict_python_binary" pantsref="bdict_python_binary">python_binary target</a>.
The hello_world target contains one source file,
hello_world.py and depends upon one other target, the format of which
will be described shortly.</p>
<p>It should be noted that sources are relative to the location of the
BUILD file itself, e.g. hello_world.py inside of
src/python/twitter/tutorial/BUILD actually refers to
<code>src/python/twitter/tutorial/hello_world.py</code>:</p>
<div class="codehilite"><pre>from twitter.common import app
def main():
print('Hello world!')
app.main()
</pre></div>
<p>Dependencies, on the other hand, are relative to the <em>source root</em> of
the repository which is defined by the BUILD file that sits next to the
pants command:</p>
<div class="codehilite"><pre># Define the repository layout
source_root('src/antlr', doc, page, java_antlr_library, python_antlr_library)
source_root('src/java', annotation_processor, doc, jvm_binary, java_library, page)
source_root('src/protobuf', doc, java_protobuf_library, page)
source_root('src/python', doc, page, python_binary, python_library)
source_root('src/scala', doc, jvm_binary, page, scala_library)
source_root('src/thrift', doc, java_thrift_library, page, python_thrift_library)
source_root('tests/java', doc, java_library, java_tests, page)
source_root('tests/python', doc, page, python_library, python_tests, python_test_suite)
source_root('tests/scala', doc, page, scala_library, scala_tests)
</pre></div>
<p>This file can be tailored to map to any source root structure such as
Maven style, Twitter style (as described above) or something more flat
such as a <code>setup.py</code>-based project. This however is an advanced topic that
is not covered in this document.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="addressing-targets">Addressing targets</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#addressing-targets">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>Within the src/python/twitter/tutorial/BUILD, only one target is
defined, specifically hello_world. This target is addressed by
src/python/twitter/tutorial:hello_world which means the target
hello_world within src/python/twitter/tutorial/BUILD. In general,
targets take the form <code><path>:<target name></code> with the special cases:</p>
<ol>
<li>in the case of path/to/directory/BUILD:target, the BUILD component
may be elided and instead path/to/directory:target may be used</li>
<li>path/to/directory is short form for path/to/directory:directory, so
src/python/twitter/common/app is short form for
src/python/twitter/common/app/BUILD:app</li>
</ol>
<p>src/python/twitter/tutorial/BUILD referenced
pants('src/python/twitter/common/app') in its dependencies. The pants()
keyword is akin to a "pointer dereference" for an address. It will point
to whatever target is described at that address, in this case a
python_library target:</p>
<p><code>src/python/twitter/common/app/BUILD</code>:</p>
<div class="codehilite"><pre>python_library(
name = "app",
sources = globs('*.py'),
dependencies = [
pants('src/python/twitter/common/dirutil'),
pants('src/python/twitter/common/lang'),
pants('src/python/twitter/common/options'),
pants('src/python/twitter/common/util'),
pants('src/python/twitter/common/app/modules'),
]
)
</pre></div>
<p>which in turn includes even more dependencies. The job of Pants is to
manage the transitive closure of all these dependencies and manipulate
collections of these targets for you.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="python-target-types">Python target types</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python-target-types">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>BUILD files themselves are just Python. The only thing magical is that
the statement <code>from pants import *</code> has been autoinjected. This provides
a number of Python-specific targets such as:</p>
<ol>
<li>python_library</li>
<li>python_binary</li>
<li>python_requirement</li>
<li>python_thrift_library</li>
</ol>
<p>and a whole host of other targets including Java, Scala, Python,
Markdown, the universal pants target and so forth. See
src/python/pants/__init__.py for a comprehensive list of targets.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h4 id="python95library">python_library</h4></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python95library">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>A python_library target has a name, zero or more source files, zero or
more resource files, and zero or more dependencies. These dependencies
may include other python_library-like targets (python_library,
python_thrift_library, python_antlr_library and so forth) or
python_requirement targets.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h4 id="python95binary">python_binary</h4></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python95binary">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>A python_binary target is almost identical to a python_library target
except instead of sources, it takes one of two possible parameters:</p>
<ol>
<li>
<p><code>source</code>: The source file that should be executed within the
"library" otherwise defined by python_binary</p>
</li>
<li>
<p><code>entry_point</code>: The entry point that should be executed within the
"library" otherwise defined by python_binary. Entry points take the
format of pkg_resources.EntryPoint, which is something akin to
some.module.name:my.attr which means run the function pointed by my.attr
inside the module some.module inside the environment. The :my.attr
component can be omitted and the module is executed directly (presuming
it has a <code>__main__.py</code>.)</p>
</li>
</ol>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h4 id="python95requirement">python_requirement</h4></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python95requirement">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>A python_requirement target describes an external dependency as
understood by easy_install or pip. It takes only a single non-keyword
argument of the Requirement-style string, e.g.,</p>
<div class="codehilite"><pre>python_requirement('django-celery')
python_requirement('tornado==2.2')
python_requirement('kombu>=2.1.1,<3.0')
</pre></div>
<p>This will resolve the dependency and its transitive closure, for example
django-celery pulls down the following dependencies: <code>celery>=2.5.1</code>,
<code>django-picklefield>=0.2.0</code>, <code>ordereddict</code>, <code>python-dateutil</code>,
<code>kombu>=2.1.1,<3.0</code>, <code>anyjson>=0.3.1</code>, <code>importlib</code>, and <code>amqplib>=1.0</code>.</p>
<p>Pants takes care of handling these dependencies for you. It will never
install anything globally. Instead it will build the dependency and
cache it in .pants.d and assemble them a la carte into an execution
environment.</p>
<p>The python_requirement for a particular dependency should appear only
once in a BUILD file. It creates a local target name which can then be
included in other dependencies in the file.:</p>
<div class="codehilite"><pre>python_requirement('django-celery')
python_library(
name = 'mylib_1',
sources = [
'mylib_1.py',
],
dependencies = [
pants(':django-celery')
]
)
python_library(
name = 'mylib_2',
sources = [
'mylib_2.py',
],
dependencies = [
pants(':django-celery')
]
)
</pre></div>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h4 id="python95thrift95library">python_thrift_library</h4></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python95thrift95library">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>A python_thrift_library target takes the same arguments as
python_library arguments, except that files described in sources must
be thrift files. If your library or binary depends upon this target
type, Python bindings will be autogeNerated and included within your
environment.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h4 id="python95tests">python_tests</h4></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python95tests">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>A python_tests target takes the same arguments as python_library
arguments, with the addition of the optional coverage argument that is a
list of namespaces that you want to generate coverage data for.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="building-your-first-pex">Building your first PEX</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#building-your-first-pex">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>Now you're ready to build your first PEX file (technically you already
have, by building Pants itself.) By default if you specify
<code>./pants <target></code>, it assumes you mean <code>./pants build <target></code> and
does precisely that:</p>
<div class="codehilite"><pre>$ PANTS_VERBOSE=1 ./pants src/python/twitter/tutorial:hello_world
Build operating on targets: OrderedSet([PythonBinary(src/python/twitter/tutorial/BUILD:hello_world)])
Resolver: Calling environment super => 0.046ms
Building PythonBinary PythonBinary(src/python/twitter/tutorial/BUILD:hello_world):
Building PythonBinary PythonBinary(src/python/twitter/tutorial/BUILD:hello_world):
Dumping library: PythonLibrary(src/python/twitter/common/app/BUILD:app) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/dirutil/BUILD:dirutil) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/lang/BUILD:lang) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/options/BUILD:options) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/util/BUILD:util) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/app/modules/BUILD:modules) [relative module: ]
Resolver: Calling environment super => 0.016ms
Dumping binary: twitter/tutorial/hello_world.py
Wrote /private/tmp/wickman-commons/dist/hello_world.pex
</pre></div>
<p>You will see that despite specifying just one dependency, the transitive
closure of hello_world pulled in all of src/python/twitter/common/app
and its direct descendants. That's because those library targets
depended upon other library targets, than in turn depending on even
more. At the end of the day, we bundle up the closed set of all
dependencies and bundle them into hello_world.pex.</p>
<p>Since it uses the twitter.common.app framework, we know we can fire it
up and poke around with <code>--help</code>:</p>
<div class="codehilite"><pre>$ dist/hello_world.pex --help
Options:
-h, --help, --short-help
show this help message and exit.
--long-help show options from all registered modules, not just the
__main__ module.
</pre></div>
<p>If we specify --long-help, we can see the help of transitively included
modules, e.g. twitter.common.app itself:</p>
<div class="codehilite"><pre>$ dist/hello_world.pex --long-help
Options:
-h, --help, --short-help
show this help message and exit.
--long-help show options from all registered modules, not just the
__main__ module.
From module twitter.common.app:
--app_daemonize Daemonize this application. [default: False]
--app_profile_output=FILENAME
Dump the profiling output to a binary profiling
format. [default: None]
--app_daemon_stderr=TWITTER_COMMON_APP_DAEMON_STDERR
Direct this app's stderr to this file if daemonized.
[default: /dev/null]
--app_debug Print extra debugging information during application
initialization. [default: False]
--app_daemon_stdout=TWITTER_COMMON_APP_DAEMON_STDOUT
Direct this app's stdout to this file if daemonized .
[default: /dev/null]
--app_profiling Run profiler on the code while it runs. Note this can
cause slowdowns. [default: False]
--app_ignore_rc_file
Ignore default arguments from the rc file. [default:
False]
--app_pidfile=TWITTER_COMMON_APP_PIDFILE
The pidfile to use if --app_daemonize is specified.
[default: None]
</pre></div>
<p>Or we can simply execute it as intended:</p>
<div class="codehilite"><pre>$ dist/hello_world.pex
Hello world!
</pre></div>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="environment-manipulation-with-pants-py">Environment manipulation with pants py</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#environment-manipulation-with-pants-py">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>We've only discussed so far the "pants build" command. There's also a
dedicated "py" command that allows you to manipulate the environments
described by python_binary and python_library targets, such as drop
into an interpreter with the environment set up for you.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="pants-py-semantics">pants py semantics</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#pants-py-semantics">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>The default behavior of <code>pants py <target></code> is the following:</p>
<ol>
<li>For python_binary targets, build the environment and execute the
target</li>
<li>For one or more python_library targets, build the environment that
is the transitive closure of all targets and drop into an
interpreter.</li>
<li>For a combination of python_binary and python_library targets,
build the transitive closure of all targets and execute the first
binary target.</li>
</ol>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="external-dependencies">external dependencies</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#external-dependencies">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>Let's take src/python/twitter/tutorial/BUILD and split out the
dependencies from our hello_world target into hello_world_lib and add
dependencies upon <a href="http://github.com/facebook/tornado">Tornado</a> and
<a href="http://code.google.com/p/psutil/">psutil</a>.</p>
<div class="codehilite"><pre>python_binary(
name = "hello_world",
source = "hello_world.py",
dependencies = [
pants(":hello_world_lib")
]
)
python_library(
name = "hello_world_lib",
dependencies = [
pants("src/python/twitter/common/app"),
python_requirement("tornado"),
python_requirement("psutil"),
]
)
</pre></div>
<p>This uses the python_requirement target which can refer to any string
in pkg_resources.Requirement format as recognized by tools such as
easy_install and pip as described above.</p>
<p>Now that we've created a library-only target
src/python/twitter/tutorial:hello_world_lib, let's drop into it using
pants py with verbosity turned on so that we can see what's going on in
the background:</p>
<div class="codehilite"><pre>$ PANTS_VERBOSE=1 ./pants py src/python/twitter/tutorial:hello_world_lib
Build operating on target: PythonLibrary(src/python/twitter/tutorial/BUILD:hello_world_lib)
Resolver: Calling environment super => 0.019ms
Building PythonBinary PythonLibrary(src/python/twitter/tutorial/BUILD:hello_world_lib):
Dumping library: PythonLibrary(src/python/twitter/tutorial/BUILD:hello_world_lib) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/app/BUILD:app) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/dirutil/BUILD:dirutil) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/lang/BUILD:lang) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/options/BUILD:options) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/util/BUILD:util) [relative module: ]
Dumping library: PythonLibrary(src/python/twitter/common/app/modules/BUILD:modules) [relative module: ]
Dumping requirement: tornado
Dumping requirement: psutil
Resolver: Calling environment super => 0.029ms
Resolver: Activating cache /private/tmp/wickman-commons/3rdparty/python => 356.432ms
Resolver: Resolved tornado => 357.219ms
Resolver: Activating cache /private/tmp/wickman-commons/.pants.d/.python.install.cache => 41.117ms
Resolver: Fetching psutil => 10144.264ms
Resolver: Building psutil => 1794.474ms
Resolver: Distilling psutil => 224.896ms
Resolver: Constructing distribution psutil => 2.855ms
Resolver: Resolved psutil => 12210.066ms
Dumping distribution: .../tornado-2.2-py2.6.egg
Dumping distribution: .../psutil-0.4.1-py2.6-macosx-10.4-x86_64.egg
Python 2.6.7 (r267:88850, Aug 31 2011, 15:49:05)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
</pre></div>
<p>In the background, pants used cached version of tornado but fetched
psutil from pypi and any necessary transitive dependencies (none in this
case) and built a platform-specific version for us.</p>
<p>You can convince yourself that the environment contains all the
dependencies by inspecting sys.path and importing libraries as you
desire:</p>
<div class="codehilite"><pre>>>> import psutil
>>> help(psutil)
>>> from twitter.common import app
>>> help(app)
</pre></div>
<p>It should be stressed that <em>dependencies built by Pants are never
installed globally</em>. These dependencies only exist for the duration of
the Python interpreter forked by Pants.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="running-an-application-using-pants-py">Running an application using pants py</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#running-an-application-using-pants-py">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>Let us turn our hello_world.py into a basic top application using
<code>tornado</code>:</p>
<div class="codehilite"><pre>from twitter.common import app
import psutil
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('<pre>Running pids:\n%s</pre>' % '\n'.join(map(str, psutil.get_pid_list())))
def main():
application = tornado.web.Application([
(r"/", MainHandler)
])
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
app.main()
</pre></div>
<p>We have now split our application into two parts: the hello_world
binary target and the hello_world_lib library target. If we run
pants py
src/python/twitter/tutorial:hello_world_lib, the default behavior is
to drop into an interpreter.</p>
<p>If we run pants py src/python/twitter/tutorial:hello_world, the default
behavior is to run the binary target pointed to by <code>hello_world</code>:</p>
<div class="codehilite"><pre>$ ./pants py src/python/twitter/tutorial:hello_world
</pre></div>
<p>Then point your browser to <a href="http://localhost:8888">http://localhost:8888</a></p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="pants-py-pex">pants py --pex</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#pants-py-pex">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>There is also a --pex option to pants py that allows you to build a PEX
file from a union of python_library targets that does not necessarily
have a python_binary target defined for it. Since there is no entry
point specified, the resulting .pex file just behaves like a Python
interpreter, but with the sys.path bootstrapped for you:</p>
<div class="codehilite"><pre>$ ./pants py --pex src/python/twitter/tutorial:hello_world_lib
Build operating on target: PythonLibrary(src/python/twitter/tutorial/BUILD:hello_world_lib)
Wrote /private/tmp/wickman-commons/dist/hello_world_lib.pex
$ ls -la dist/hello_world_lib.pex
-rwxr-xr-x 1 wickman wheel 1404174 Apr 10 13:00 dist/hello_world_lib.pex
</pre></div>
<p>Now if you use dist/hello_world_lib.pex, since it has no entry point,
it will drop you into an interpreter:</p>
<div class="codehilite"><pre>$ dist/hello_world_lib.pex
Python 2.6.7 (r267:88850, Aug 31 2011, 15:49:05)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import tornado
</pre></div>
<p>As mentioned before, it's like a single-file lightweight alternative to
a virtualenv. We can even use it to run our hello_world.py application:</p>
<div class="codehilite"><pre>$ dist/hello_world_lib.pex src/python/twitter/tutorial/hello_world.py
</pre></div>
<p>This can be an incredibly powerful and lightweight way to manage and
deploy virtual environments without using virtualenv.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="pex-file-as-interpreter">PEX file as interpreter</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#pex-file-as-interpreter">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>As mentioned above, PEX files without default entry points behave like
Python interpreters that carry their dependencies with them. For
example, let's create a target that provides a Fabric dependency within
<code>src/python/twitter/tutorial/BUILD</code>:</p>
<div class="codehilite"><pre>python_library(
name = 'fabric',
dependencies = [
python_requirement('Fabric')
]
)
</pre></div>
<p>And let's build a fabric PEX file:</p>
<div class="codehilite"><pre>$ ./pants py --pex src/python/twitter/tutorial:fabric
Build operating on target: PythonLibrary(src/python/twitter/tutorial/BUILD:fabric)
Wrote /private/tmp/wickman-commons/dist/fabric.pex
</pre></div>
<p>By default it does nothing more than drop us into an interpreter:</p>
<div class="codehilite"><pre>$ dist/fabric.pex
Python 2.6.7 (r267:88850, Aug 31 2011, 15:49:05)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
</pre></div>
<p>But suppose we have a local script that depends upon Fabric,
<code>fabric_hello_world.py</code>:</p>
<div class="codehilite"><pre>from fabric.api import *
def main():
local('echo hello world')
if __name__ == '__main__':
main()
</pre></div>
<p>We can now use fabric.pex as if it were a Python interpreter but with
fabric available in its environment. Note that fabric has never been
installed globally in any site-packages anywhere. It is just bundled
inside of fabric.pex:</p>
<div class="codehilite"><pre>$ dist/fabric.pex fabric_hello_world.py
[localhost] local: echo hello world
hello world
</pre></div>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="python95binary-entry95point">python_binary entry_point</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python95binary-entry95point">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>An advanced feature of python_binary targets, you may in addition
specify direct entry points into PEX files rather than a source file.
For example, if we wanted to build an a la carte fab wrapper for fabric:</p>
<div class="codehilite"><pre>python_binary(name = "fab",
entry_point = "fabric.main:main",
dependencies = [
python_requirement("fabric"),
]
)
</pre></div>
<p>We build:</p>
<div class="codehilite"><pre>$ ./pants src/python/twitter/tutorial:fab
Build operating on targets: OrderedSet([PythonBinary(src/python/twitter/tutorial/BUILD:fab)])
Building PythonBinary PythonBinary(src/python/twitter/tutorial/BUILD:fab):
Wrote /private/tmp/wickman-commons/dist/fab.pex
</pre></div>
<p>And now dist/fab.pex behaves like a standalone fab binary:</p>
<div class="codehilite"><pre>$ dist/fab.pex -h
Usage: fab [options] <command>[:arg1,arg2=val2,host=foo,hosts='h1;h2',...] ...
Options:
-h, --help show this help message and exit
-d NAME, --display=NAME
print detailed info about command NAME
-F FORMAT, --list-format=FORMAT
formats --list, choices: short, normal, nested
-l, --list print list of possible commands and exit
--set=KEY=VALUE,... comma separated KEY=VALUE pairs to set Fab env vars
--shortlist alias for -F short --list
-V, --version show program's version number and exit
-a, --no_agent don't use the running SSH agent
-A, --forward-agent forward local agent to remote end
--abort-on-prompts abort instead of prompting (for password, host, etc)
...
</pre></div>
<p>Pants also has excellent support for JVM-based builds and can do similar
things like resolving external JARs and packaging them as standalone
environments with default entry points.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h2 id="python-tests">Python Tests</h2></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#python-tests">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>By default Python tests are run via pytest. Any option that py.test has
can be used since arguments are passed on by pants.</p>
<table class="h-plus-pilcrow">
<tbody>
<tr>
<td class="h-plus-pilcrow-holder"><h3 id="defining-python95tests-targets">Defining python_tests Targets</h3></td>
<td><div class="pilcrow-div">
<a class="pilcrow-link" href="#defining-python95tests-targets">¶</a>
</div></td>
</tr>
</tbody>
</table>
<p>When setting up your test targets, the BUILD file will be something
like:</p>
<div class="codehilite"><pre>python_tests(
name = "your_tests",
sources = globs("*.py"),
coverage = ["twitter.your_namespace"],
dependencies = [
pants("3rdparty/python:mock")
pants("src/python/twitter/your_namespace")
]
)
</pre></div>