forked from pantsbuild/pantsbuild.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_readme.html
774 lines (736 loc) · 47.1 KB
/
python_readme.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
<!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>Python Projects with Pants</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="pants-logo.ico">
<!-- In case this is a "test publish", tell search engines where real version lives: -->
<link rel="canonical" href="http://pantsbuild.org/python_readme.html">
<link rel="stylesheet" href="bootstrap-custom.min.css">
<link rel="stylesheet" href="bootstrap-custom-theme.min.css">
<link rel="stylesheet" href="docsite.css">
</head>
<body>
<div class="header">
<nav class="navbar navbar-default navbar-static-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand navbar-brand-img" href="index.html">
<img src="pants-logo.ico" alt="[pantsbuild logo]">
</a>
<a class="navbar-brand" href="index.html">
Pants
</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="/">Docs</a></li>
<li><a href="community.html">Community</a></li>
<li><a href="https://www.github.com/pantsbuild/pants">GitHub</a></li>
<li>
<form class="navbar-form navbar-left search" role="search" action="https://www.google.com/search">
<div class="form-group">
<input type="text" name="as_q" class="form-control query" placeholder="Search">
<input name="as_sitesearch" value="pantsbuild.org" type="hidden">
</div>
</form>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
</div>
<div class="page">
<div class="container-fluid">
<div class="row">
<div class="col-md-1">
</div>
<div class="col-md-2">
<div class="site-toc">
<ul>
<li class="toc-h1 toc-heading">
Getting Started
</li>
<li class="toc-h1 toc-link ">
<a href="install.html">Installing Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="setup_repo.html">Setting Up Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="first_tutorial.html">Tutorial</a>
</li>
<li class="toc-h1 toc-link ">
<a href="common_tasks.html">Common Tasks</a>
</li>
<li class="toc-h1 toc-heading">
Pants Basics
</li>
<li class="toc-h1 toc-link ">
<a href="why_use_pants.html">Why Use Pants?</a>
</li>
<li class="toc-h1 toc-link ">
<a href="first_concepts.html">Pants Concepts</a>
</li>
<li class="toc-h1 toc-link ">
<a href="build_files.html">BUILD files</a>
</li>
<li class="toc-h1 toc-link ">
<a href="target_addresses.html">Target Addresses</a>
</li>
<li class="toc-h1 toc-link ">
<a href="3rdparty.html">Third-Party Dependencies</a>
</li>
<li class="toc-h1 toc-link ">
<a href="options.html">Pants Options</a>
</li>
<li class="toc-h1 toc-link ">
<a href="invoking.html">Invoking Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="reporting_server.html">Reporting Server</a>
</li>
<li class="toc-h1 toc-link ">
<a href="ide_support.html">IDE Support</a>
</li>
<li class="toc-h1 toc-heading">
JVM
</li>
<li class="toc-h1 toc-link ">
<a href="jvm_projects.html">JVM Projects with Pants</a>
</li>
<li class="toc-h1 toc-link ">
<a href="3rdparty_jvm.html">JVM 3rdparty Pattern</a>
</li>
<li class="toc-h1 toc-link ">
<a href="scala.html">Scala Support</a>
</li>
<li class="toc-h1 toc-link ">
<a href="publish.html">Publishing Artifacts</a>
</li>
<li class="toc-h1 toc-link ">
<a href="from_maven.html">Pants for Maven Experts</a>
</li>
<li class="toc-h1 toc-heading">
Python
</li>
<li class="toc-h1 toc-link toc-here">
Python Projects with Pants
</li>
<li class="toc-h1 toc-link ">
<a href="3rdparty_py.html">Python 3rdparty Pattern</a>
</li>
<li class="toc-h1 toc-heading">
Go
</li>
<li class="toc-h1 toc-link ">
<a href="go_readme.html">Go support for Pants</a>
</li>
<li class="toc-h1 toc-heading">
Codegen
</li>
<li class="toc-h1 toc-link ">
<a href="thrift_deps.html">Thrift</a>
</li>
<li class="toc-h1 toc-heading">
Docgen
</li>
<li class="toc-h1 toc-link ">
<a href="page.html">Markdown</a>
</li>
<li class="toc-h1 toc-heading">
Getting Help
</li>
<li class="toc-h1 toc-link ">
<a href="tshoot.html">Troubleshooting</a>
</li>
<li class="toc-h1 toc-link ">
<a href="community.html">Community</a>
</li>
<li class="toc-h1 toc-heading">
Reference
</li>
<li class="toc-h1 toc-link ">
<a href="build_dictionary.html">Pants BUILD Dictionary</a>
</li>
<li class="toc-h1 toc-link ">
<a href="options_reference.html">Pants Reference</a>
</li>
<li class="toc-h1 toc-heading">
Release Notes
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.3.x.html">1.3.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.2.x.html">1.2.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.1.x.html">1.1.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-1.0.x.html">1.0.x Stable Releases</a>
</li>
<li class="toc-h1 toc-link ">
<a href="notes-master.html">Master Pre-Releases</a>
</li>
<li class="toc-h1 toc-heading">
Developer
</li>
<li class="toc-h1 toc-link ">
<a href="dev.html">Pants Developer Center</a>
</li>
<li class="toc-h1 toc-link ">
<a href="export.html">Export Format</a>
</li>
</ul>
</div> <!-- site-toc -->
</div>
<div class="col-md-8">
<div class="content">
<div class="mainflow">
<nav class="pagetoc">
<ul>
<li class="toc-h1"><a href="#relevant-goals-and-targets">Relevant Goals and Targets</a></li>
<li class="toc-h1"><a href="#build-for-a-simple-binary">BUILD for a Simple Binary</a></li>
<li class="toc-h1"><a href="#build-for-tests">BUILD for Tests</a></li>
<li class="toc-h1"><a href="#handling-python_requirement">Handling python_requirement</a></li>
<li class="toc-h1"><a href="#pex-contents">PEX Contents</a></li>
<li class="toc-h1"><a href="#interactive-console-with-repl-goal">Interactive Console with repl Goal</a></li>
<li class="toc-h1"><a href="#python_binary-entry_point">python_binary entry_point</a></li>
<li class="toc-h1"><a href="#more-about-python-tests">More About Python Tests</a></li>
<li class="toc-h2"><a href="#code-coverage">Code Coverage</a></li>
<li class="toc-h2"><a href="#interactive-debugging-on-test-failure">Interactive Debugging on Test Failure</a></li>
<li class="toc-h1"><a href="#building-a-setuppy-distutils-package">Building a setup.py Distutils Package</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="#pex_interpreter1">PEX_INTERPRETER=1</a></li>
<li class="toc-h2"><a href="#pex_verbose1">PEX_VERBOSE=1</a></li>
<li class="toc-h2"><a href="#pex_moduleentry_point">PEX_MODULE=entry_point</a></li>
<li class="toc-h2"><a href="#pex_coverage">PEX_COVERAGE</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>
<h1 id="python-projects-with-pants">Python Projects with Pants</h1>
<p>Pants makes the manipulation and distribution of hermetically sealed
Python environments painless. You can organize your code in the
<a href="first_concepts.html">Pants way</a>
with targets for binaries, libraries, and
tests. Pants builds Python code into
<a href="https://github.com/pantsbuild/pex/blob/master/docs/whatispex.rst">PEXes</a>.
(A PEX is, roughly, an archive file containing a runnable Python
environment.) Pants isn't the only PEX-generation tool out there; but if
you have some "common code" used by more than one PEX, Pants makes it
easy to manage the dependencies.</p>
<p>This page assumes that you've already read
<a href="first_tutorial.html">the Pants Tutorial</a>.</p>
<h2 id="relevant-goals-and-targets">Relevant Goals and Targets</h2>
<p><strong>Runnable Binary</strong></p>
<blockquote>
<p>Pants can generate PEXes, executables built from Python. Invoke the
<a href="options_reference.html#oref_goal_binary" pantsref="oref_goal_binary"><code>binary</code></a> goal on a
<a href="build_dictionary.html#bdict_python_binary" pantsref="bdict_python_binary"><code>python_binary</code></a> target to generate a <code>.pex</code>.
You can also invoke the
<a href="options_reference.html#oref_goal_run" pantsref="oref_goal_run"><code>run</code></a> goal on a
<code>python_binary</code> to run its code "in place."</p>
</blockquote>
<p><strong>Importable Code</strong></p>
<blockquote>
<p><a href="build_dictionary.html#bdict_python_library" pantsref="bdict_python_library"><code>python_library</code></a> BUILD targets make Python
code "import-able". The rule of thumb is that each directory of <code>.py</code>
files has a <code>BUILD</code> file with a <code>python_library</code> target. A Python
target that has a <code>python_library</code> in its <code>dependencies</code> can import
its code.</p>
<p>To use code that's not in your workspace, use a
<a href="build_dictionary.html#bdict_python_requirement_library" pantsref="bdict_python_requirement_library"><code>python_requirement_library</code></a>
and a
<a href="build_dictionary.html#bdict_python_requirement" pantsref="bdict_python_requirement"><code>python_requirement</code></a> to refer to
the code. To use several several of these via a <code>pip</code>-style
<code>requirements.txt</code> file, use a
<a href="build_dictionary.html#bdict_python_requirements" pantsref="bdict_python_requirements"><code>python_requirements</code></a>.
For details, see <a href="3rdparty_py.html">Python 3rdparty Pattern</a>.</p>
</blockquote>
<p><strong>Tests</strong></p>
<blockquote>
<p>A <a href="build_dictionary.html#bdict_python_tests" pantsref="bdict_python_tests"><code>python_tests</code></a> BUILD target has some <code>pytest</code> tests.
It normally depends on a <code>python_library</code> target so it can import and test the library's code.
Use the <a href="options_reference.html#oref_goal_test" pantsref="oref_goal_test"><code>test</code></a> goal to run these tests.</p>
</blockquote>
<p><strong>Generated Code</strong></p>
<blockquote>
<p>A <a href="build_dictionary.html#bdict_python_thrift_library" pantsref="bdict_python_thrift_library"><code>python_thrift_library</code></a> generates
Python code from <code>.thrift</code> source; a Python target that has this
target in its <code>dependencies</code> can <code>import</code> the generated Python code.</p>
</blockquote>
<h2 id="build-for-a-simple-binary">BUILD for a Simple Binary</h2>
<p>The pantsbuild/pants repo has a simple "hello world" sample Python
binary. You can use <code>binary</code> to build a PEX from it. You can then
run the PEX:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants binary examples/src/python/example/hello/main
...much output...
<span class="nv">$ </span>./dist/main.pex <span class="c"># run the generated PEX</span>
Hello, world!
<span class="nv">$ </span>./dist/main.pex Whirled
Hello, Whirled!
<span class="err">$</span>
</pre></div>
<p>You can also run the binary "from source" with the <code>run</code> goal:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants run.py --args<span class="o">=</span><span class="s1">'Whirled'</span> examples/src/python/example/hello/main
...much output...
14:32:01 00:00 <span class="o">[</span>py<span class="o">]</span>
14:32:02 00:01 <span class="o">[</span>run<span class="o">]</span>
Hello, Whirled!
14:32:02 00:01 <span class="o">[</span>jvm<span class="o">]</span>
SUCCESS
<span class="err">$</span>
</pre></div>
<p><a href="https://github.com/pantsbuild/pants/blob/master/examples/src/python/example/hello/main/BUILD"><code>examples/src/python/example/hello/main/BUILD</code></a>
defines a <code>python_binary</code> target, a build-able thing that defines a runnable program made from
Python code:</p>
<p>
<div class="md-included-snippet"><div class="codehilite"><pre><span class="n">python_binary</span><span class="p">(</span>
<span class="n">dependencies</span><span class="o">=</span><span class="p">[</span>
<span class="s">'examples/src/python/example/hello/greet:greet'</span><span class="p">,</span>
<span class="p">],</span>
<span class="n">source</span><span class="o">=</span><span class="s">'main.py'</span><span class="p">,</span>
<span class="p">)</span>
</pre></div></div>
</p>
<p>This binary has a source file, <code>main.py</code>, with its "main". A Python binary's "main" can be in a
depended-upon <code>python_library</code> or in the <code>python_binary</code>'s <code>source</code>. (Notice that's <code>source</code>,
not <code>sources</code>; a binary can have only one source file. If you want more, put them in a
<code>python_library</code> and let the <code>python_binary</code> depend on that.)</p>
<p>
<div class="md-included-snippet"><div class="codehilite"><pre><span class="k">if</span> <span class="n"><strong>name</strong></span> <span class="o">==</span> <span class="s">'<strong>main</strong>'</span><span class="p">:</span>
<span class="n">greetees</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="ow">or</span> <span class="p">[</span><span class="s">'world'</span><span class="p">]</span>
<span class="k">for</span> <span class="n">greetee</span> <span class="ow">in</span> <span class="n">greetees</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">greet</span><span class="p">(</span><span class="n">greetee</span><span class="p">))</span>
</pre></div></div>
</p>
<p>This code imports code from another target. To make this work, the
binary target has a dependency <code>examples/src/python/example/hello/greet</code>
and the Python code can thus import things from <code>example.hello.greet</code>.</p>
<p>You remember that libraries configure "importable" code;
<code>example/hello/greet/BUILD</code> has a <code>python_library</code>:</p>
<p>
<div class="md-included-snippet"><div class="codehilite"><pre><span class="n">python_library</span><span class="p">(</span>
<span class="n">dependencies</span><span class="o">=</span><span class="p">[</span>
<span class="s">'3rdparty/python:ansicolors'</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">)</span>
</pre></div></div>
</p>
<p>This <code>python_library</code> pulls in <code>greet.py</code>'s Python code:</p>
<p>
<div class="md-included-snippet"><div class="codehilite"><pre><span class="kn">from</span> <span class="nn">colors</span> <span class="kn">import</span> <span class="n">green</span>
<span class="k">def</span> <span class="nf">greet</span><span class="p">(</span><span class="n">greetee</span><span class="p">):</span>
<span class="sd">"""Given the name, return a greeting for a person of that name."""</span>
<span class="k">return</span> <span class="n">green</span><span class="p">(</span><span class="s">'Hello, {}!'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">greetee</span><span class="p">))</span>
</pre></div></div>
</p>
<h2 id="build-for-tests">BUILD for Tests</h2>
<p>To test the library's code, we set up
<a href="https://github.com/pantsbuild/pants/blob/master/examples/tests/python/example_test/hello/greet/BUILD"><code>examples/tests/python/example_test/hello/greet/BUILD</code></a>
with a <code>python_tests</code> target. It depends on the library:</p>
<p>
<div class="md-included-snippet"><div class="codehilite"><pre><span class="n">python_tests</span><span class="p">(</span>
<span class="n">dependencies</span><span class="o">=</span><span class="p">[</span>
<span class="s">'examples/src/python/example/hello/greet:greet'</span><span class="p">,</span>
<span class="s">':prep'</span><span class="p">,</span>
<span class="p">],</span>
<span class="p">)</span>
<span class="c"># Prepare for the 'greet' test. Realistically, you wouldn't set up a</span>
<span class="c"># prep_command just to create an emtpy temp file. This is meant as a</span>
<span class="c"># simple example.</span>
<span class="n">prep_command</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'prep'</span><span class="p">,</span>
<span class="n">prep_executable</span><span class="o">=</span><span class="s">'touch'</span><span class="p">,</span>
<span class="n">prep_args</span><span class="o">=</span><span class="p">[</span><span class="s">'/tmp/prep_command_result'</span><span class="p">]</span>
<span class="p">)</span>
</pre></div></div>
</p>
<p>Use <code>test</code> to run the tests. This uses <code>pytest</code>:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants <span class="nb">test </span>examples/tests/python/example_test/hello/greet
13:29:28 00:00 <span class="o">[</span>main<span class="o">]</span>
<span class="o">(</span>To run a reporting server: ./pants server<span class="o">)</span>
13:29:28 00:00 <span class="o">[</span>bootstrap<span class="o">]</span>
13:29:28 00:00 <span class="o">[</span>setup<span class="o">]</span>
13:29:28 00:00 <span class="o">[</span>parse<span class="o">]</span>
...
13:29:29 00:01 <span class="o">[</span><span class="nb">test</span><span class="o">]</span>
13:29:29 00:01 <span class="o">[</span>pytest<span class="o">]</span>
13:29:29 00:01 <span class="o">[</span>run<span class="o">]</span>
<span class="o">==============</span> <span class="nb">test </span>session <span class="nv">starts</span> <span class="o">===============</span>
platform darwin -- Python 2.6.8 -- py-1.4.20 -- pytest-2.5.2
plugins: cov, timeout
collected 2 items
examples/tests/python/example_test/hello/greet/test_greet.py ..
<span class="o">============</span> 1 passed in 0.02 <span class="nv">seconds</span> <span class="o">============</span>
13:30:18 00:50 <span class="o">[</span>junit<span class="o">]</span>
13:30:18 00:50 <span class="o">[</span>specs<span class="o">]</span>
SUCCESS
<span class="err">$</span>
</pre></div>
<h2 id="handling-python_requirement">Handling <code>python_requirement</code></h2>
<p><code>BUILD</code> files specify outside Python dependencies via
<a href="build_dictionary.html#bdict_python_requirements" pantsref="bdict_python_requirements"><code>python_requirements</code></a> and a
<code>requirements.txt</code> file and/or
<a href="build_dictionary.html#bdict_python_requirement_library" pantsref="bdict_python_requirement_library"><code>python_requirement_library</code></a> targets wrapping
<a href="build_dictionary.html#bdict_python_requirement" pantsref="bdict_python_requirement"><code>python_requirement</code></a>s.</p>
<p>Pants handles these dependencies for you. It never installs anything
globally. Instead, it builds the dependencies, caches them in <code>.pants.d</code>,
and assembles them <em>a la carte</em> into an execution environment.</p>
<h2 id="pex-contents">PEX Contents</h2>
<p>To build a PEX, invoke <code>./pants binary</code> on a <code>python_binary</code>
target:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants binary examples/src/python/example/hello/main
...
SUCCESS
<span class="nv">$ </span>./dist/main.pex
Hello, world!
</pre></div>
<p>Though the binary itself specifies just one dependency, the transitive
closure of hello/main's dependencies pulled in hello/greet and, in turn,
hello/greet's dependencies. Pants bundles up the closed set of all
dependencies into into the PEX.</p>
<h2 id="interactive-console-with-repl-goal">Interactive Console with <code>repl</code> Goal</h2>
<p>Use the <a href="options_reference.html#oref_goal_repl" pantsref="oref_goal_repl"><code>repl</code></a> goal with a Python target to run an interactive
Python REPL session.
Within the session, you can <code>import</code> the target's code and the code of its dependencies.</p>
<p>To drop into our example library target <code>examples/src/python/example/hello/greet</code> with verbosity
turn on to see what's going on in the background:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants -ldebug repl examples/src/python/example/hello/greet
15:11:41 00:00 <span class="o">[</span>main<span class="o">]</span>
<span class="o">(</span>To run a reporting server: ./pants server<span class="o">)</span>
...lots of build output...
15:11:42 00:01 <span class="o">[</span>repl<span class="o">]</span>
15:11:42 00:01 <span class="o">[</span>python-repl<span class="o">]</span>Building chroot <span class="k">for</span> <span class="o">[</span>PythonLibrary<span class="o">(</span>BuildFileAddress<span class="o">(</span>/Users/lhosken/workspace/pants/examples/src/python/example/hello/greet/BUILD, greet<span class="o">))]</span>:
Dumping library: PythonLibrary<span class="o">(</span>BuildFileAddress<span class="o">(</span>/Users/lhosken/workspace/pants/examples/src/python/example/hello/greet/BUILD, greet<span class="o">))</span>
Dumping requirement: <span class="nv">ansicolors</span><span class="o">==</span>1.0.2
Dumping distribution: .../ansicolors-1.0.2-py2-none-any.whl
15:11:42 00:01 <span class="o">[</span>run<span class="o">]</span>
Python 2.7.5 <span class="o">(</span>default, Mar 9 2014, 22:15:05<span class="o">)</span>
<span class="o">[</span>GCC 4.2.1 Compatible Apple LLVM 5.0 <span class="o">(</span>clang-500.0.68<span class="o">)]</span> on darwin
Type <span class="s2">"help"</span>, <span class="s2">"copyright"</span>, <span class="s2">"credits"</span> or <span class="s2">"license"</span> <span class="k">for </span>more information.
<span class="o">(</span>InteractiveConsole<span class="o">)</span>
>>>
</pre></div>
<p>Pants loads <code>ansicolors</code> (<code>greet</code>'s 3rdparty dependency). It would have
fetched this dependency over the network if necessary. (It wasn't
necessary to download <code>ansicolors</code>; Pants had already fetched it while
"bootstrapping" itself.)</p>
<p>To convince yourself that the environment contains <code>greet</code>'s dependencies, you can inspect
<code>sys.path</code> and import libraries:</p>
<div class="codehilite"><pre><span class="o">>>></span> <span class="kn">from</span> <span class="nn">example.hello.greet.greet</span> <span class="kn">import</span> <span class="n">greet</span>
<span class="o">>>></span> <span class="n">greet</span><span class="p">(</span><span class="s">"escape codes"</span><span class="p">)</span>
<span class="s">u'</span><span class="se">\x1b</span><span class="s">[32mHello, escape codes!</span><span class="se">\x1b</span><span class="s">[0m'</span>
<span class="o">>>></span> <span class="kn">from</span> <span class="nn">colors</span> <span class="kn">import</span> <span class="n">red</span>
<span class="o">>>></span> <span class="n">red</span><span class="p">(</span><span class="s">"other escape codes"</span><span class="p">)</span>
<span class="s">'</span><span class="se">\x1b</span><span class="s">[31mother escape codes</span><span class="se">\x1b</span><span class="s">[0m'</span>
</pre></div>
<p><strong>Dependencies built by Pants are never installed globally</strong>. These
dependencies only exist for the duration of the Python interpreter
forked by Pants.</p>
<h2 id="python_binary-entry_point"><code>python_binary</code> <code>entry_point</code></h2>
<p>An advanced feature of <code>python_binary</code> 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><span class="n">python_binary</span><span class="p">(</span><span class="n">name</span> <span class="o">=</span> <span class="s">"fab"</span><span class="p">,</span>
<span class="n">entry_point</span> <span class="o">=</span> <span class="s">"fabric.main:main"</span><span class="p">,</span>
<span class="n">dependencies</span> <span class="o">=</span> <span class="p">[</span>
<span class="s">"3rdparty/python:fabric"</span><span class="p">,</span>
<span class="p">]</span>
<span class="p">)</span>
</pre></div>
<p>We build:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants src/python/fabwrap:fab
...
Wrote /private/tmp/wickman-pants/dist/fab.pex
</pre></div>
<p>And now dist/fab.pex behaves like a standalone fab binary:</p>
<div class="codehilite"><pre><span class="nv">$ </span>dist/fab.pex -h
Usage: fab <span class="o">[</span>options<span class="o">]</span> <<span class="nb">command</span>><span class="o">[</span>:arg1,arg2<span class="o">=</span>val2,host<span class="o">=</span>foo,hosts<span class="o">=</span><span class="s1">'h1;h2'</span>,...<span class="o">]</span> ...
Options:
-h, --help show this <span class="nb">help </span>message and <span class="nb">exit</span>
-d NAME, --display<span class="o">=</span>NAME
print detailed info about <span class="nb">command </span>NAME
-F FORMAT, --list-format<span class="o">=</span>FORMAT
formats --list, choices: short, normal, nested
-l, --list print list of possible commands and <span class="nb">exit</span>
--set<span class="o">=</span><span class="nv">KEY</span><span class="o">=</span>VALUE,... comma separated <span class="nv">KEY</span><span class="o">=</span>VALUE pairs to <span class="nb">set </span>Fab env vars
--shortlist <span class="nb">alias </span><span class="k">for</span> -F short --list
-V, --version show program<span class="s1">'s version number and exit</span>
<span class="s1"> -a, --no_agent don'</span>t use the running SSH agent
-A, --forward-agent forward <span class="nb">local </span>agent to remote end
--abort-on-prompts abort instead of prompting <span class="o">(</span><span class="k">for </span>password, host, etc<span class="o">)</span>
...
</pre></div>
<h2 id="more-about-python-tests">More About Python Tests</h2>
<p>Pants runs Python tests with <code>pytest</code>. You can pass CLI options to <code>pytest</code> with
<code>test.pytest --options</code>. For example, to only run tests whose names contain <code>req</code>,
you could run:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants test.pytest --options<span class="o">=</span><span class="s1">'-k req'</span> examples/tests/python/example_test/hello/greet
...
<span class="o">==============</span> <span class="nb">test </span>session <span class="nv">starts</span> <span class="o">===============</span>
platform darwin -- Python 2.6.8 -- py-1.4.20 -- pytest-2.5.2
plugins: cov, timeout
collected 2 <span class="nv">items</span>
<span class="o">=========</span> 2 tests deselected by <span class="s1">'-kfoo'</span> <span class="o">==========</span>
<span class="o">==========</span> 2 deselected in 0.01 <span class="nv">seconds</span> <span class="o">==========</span>
13:34:28 00:02 <span class="o">[</span>junit<span class="o">]</span>
13:34:28 00:02 <span class="o">[</span>specs<span class="o">]</span>
SUCCESS
</pre></div>
<p>You can pass CLI options to <code>pytest</code> via passthrough parameters if <code>test.pytest</code> is the last goal
and task on your command line. E.g., to run only tests whose names contain <code>req</code> via passthrough
parameters:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants test.pytest examples/tests/python/example_test/hello/greet -- -k req
...lots of build output...
10:43:04 00:01 <span class="o">[</span><span class="nb">test</span><span class="o">]</span>
10:43:04 00:01 <span class="o">[</span>run_prep_command<span class="o">]</span>
10:43:04 00:01 <span class="o">[</span>prep_command<span class="o">]</span>
10:43:04 00:01 <span class="o">[</span>pytest<span class="o">]</span>
10:43:04 00:01 <span class="o">[</span>run<span class="o">]</span>
<span class="o">==============</span> <span class="nb">test </span>session <span class="nv">starts</span> <span class="o">===============</span>
platform darwin -- Python 2.7.5 -- py-1.4.26 -- pytest-2.6.4
plugins: cov, timeout
collected 2 items
examples/tests/python/example_test/hello/greet/test_greet.py .
<span class="o">=========</span> 1 tests deselected by <span class="s1">'-kreq'</span> <span class="o">==========</span>
<span class="o">=====</span> 1 passed, 1 deselected in 0.05 <span class="nv">seconds</span> <span class="o">=====</span>
10:43:05 00:02 <span class="o">[</span>junit<span class="o">]</span>
10:43:05 00:02 <span class="o">[</span>specs<span class="o">]</span>
SUCCESS
</pre></div>
<p>...and to "unsilence" py.test (not suppress stderr and stdout), pass <code>-- -s</code>:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants test.pytest examples/tests/python/example_test/hello/greet -- -s
</pre></div>
<p>...and to remind yourself of py.test's help:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants test.pytest examples/tests/python/example_test/hello/greet -- -h
</pre></div>
<h3 id="code-coverage">Code Coverage</h3>
<p>To get code coverage data, set the <code>--coverage</code> flag in <code>test.pytest</code> scope.</p>
<p>The value of the flag is a comma-separated list of names of python packages or directories
containing code to measure coverage against. It can also take the special value <code>auto</code>, which
will cause Pants to attempt to deduce what to measure coverage against.</p>
<p>This auto-deduction examines the <code>coverage</code> attribute on each <code>python_tests</code>
target, which should be a list of packages. If a target has no <code>coverage</code> attribute,
Pants uses the package containing the test code. This is of course only useful if the
code under test lives in the same package as the code that tests it.</p>
<p>If you do specify package or directory names with <code>--coverage</code>, this overrides any <code>coverage</code>
attributes on <code>python_tests</code> targets.</p>
<p>For example:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants test.pytest --coverage<span class="o">=</span>example.hello.greet,example.hello.main examples/tests/python/example_test/hello/greet
...lots of build output...
<span class="o">============</span> 2 passed in 0.22 <span class="nv">seconds</span> <span class="o">============</span>
Name Stmts Miss Branch BrMiss Cover
------------------------------------------------------------------------------------
examples/src/python/example/hello/greet/__init__ 0 0 0 0 100%
examples/src/python/example/hello/greet/greet 4 0 0 0 100%
------------------------------------------------------------------------------------
TOTAL 4 0 0 0 100%
</pre></div>
<p>This measures coverage against all python code in <code>example.hello.greet</code> and <code>example.hello.main</code>.</p>
<p>Similarly, a list of directories can be specified (either absolute or relative to the build root):</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants test.pytest --coverage<span class="o">=</span>example/hello examples/tests/python/example_test/hello/greet
...lots of build output...
<span class="o">============</span> 2 passed in 0.23 <span class="nv">seconds</span> <span class="o">============</span>
Name Stmts Miss Branch BrMiss Cover
------------------------------------------------------------------------------------
examples/src/python/example/hello/__init__ 0 0 0 0 100%
examples/src/python/example/hello/greet/__init__ 0 0 0 0 100%
examples/src/python/example/hello/greet/greet 4 0 0 0 100%
------------------------------------------------------------------------------------
TOTAL 4 0 0 0 100%
</pre></div>
<h3 id="interactive-debugging-on-test-failure">Interactive Debugging on Test Failure</h3>
<p>You can invoke the Python debugger on a test failure by leaving out the
<code>test</code> and passing <code>--pdb</code>. This can be useful for inspecting the
state of objects especially if you are mocking interfaces.</p>
<h2 id="building-a-setuppy-distutils-package">Building a <code>setup.py</code> Distutils Package</h2>
<p>You can build Distutils packages from <code>python_library</code> targets.</p>
<p>To make a <code>python_library</code> "setup-able", give it a <code>provides</code> parameter; this parameter's value
should be a <a href="build_dictionary.html#bdict_setup_py" pantsref="bdict_setup_py"><code>setup_py</code></a> call; this call's parameters will be
passed to the <code>setup</code> function.</p>
<div class="codehilite"><pre><span class="n">python_library</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s">'test_infra'</span><span class="p">,</span>
<span class="n">dependencies</span><span class="o">=</span><span class="p">[</span>
<span class="s">'tests/python/pants_test:base_test'</span><span class="p">,</span>
<span class="o">...</span>
<span class="p">],</span>
<span class="n">provides</span><span class="o">=</span><span class="n">setup_py</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s">'pantsbuild.pants.testinfra'</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s">'0.0.24'</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s">'Test support for writing pants plugins.'</span><span class="p">,</span>
<span class="n">long_description</span><span class="o">=</span><span class="s">'''A much longer description of this package. Pages and pages!'''</span><span class="p">,</span>
<span class="n">url</span><span class="o">=</span><span class="s">'https://github.com/pantsbuild/pants'</span><span class="p">,</span>
<span class="n">license</span><span class="o">=</span><span class="s">'Apache License, Version 2.0'</span><span class="p">,</span>
<span class="n">zip_safe</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">namespace_packages</span><span class="o">=</span><span class="p">[</span><span class="s">'pants_test'</span><span class="p">],</span>
<span class="n">classifiers</span><span class="o">=</span><span class="p">[</span>
<span class="s">'Intended Audience :: Developers'</span><span class="p">,</span>
<span class="s">'License :: OSI Approved :: Apache Software License'</span><span class="p">,</span>
<span class="s">'Operating System :: MacOS :: MacOS X'</span><span class="p">,</span>
<span class="s">'Operating System :: POSIX :: Linux'</span><span class="p">,</span>
<span class="s">'Programming Language :: Python'</span><span class="p">,</span>
<span class="s">'Topic :: Software Development :: Build Tools'</span><span class="p">,</span>
<span class="s">'Topic :: Software Development :: Testing'</span><span class="p">,</span>
<span class="p">]</span>
<span class="p">)</span>
<span class="p">)</span>
</pre></div>
<p>The <a href="options_reference.html#oref_goal_setup-py" pantsref="oref_goal_setup-py"><code>setup-py</code></a> goal builds a package from such a target:</p>
<div class="codehilite"><pre><span class="nv">$ </span>./pants setup-py src/python/pants:test_infra
10:23:06 00:00 <span class="o">[</span>main<span class="o">]</span>
<span class="o">(</span>To run a reporting server: ./pants server<span class="o">)</span>
10:23:07 00:01 <span class="o">[</span>bootstrap<span class="o">]</span>
10:23:07 00:01 <span class="o">[</span>setup<span class="o">]</span>
10:23:07 00:01 <span class="o">[</span>parse<span class="o">]</span>
Executing tasks in goals: setup-py
10:23:07 00:01 <span class="o">[</span>setup-py<span class="o">]</span>
10:23:07 00:01 <span class="o">[</span>setup-py<span class="o">]</span>
Running packager against /Users/you/workspace/pants/dist/pantsbuild.pants.testinfra-0.0.24
Writing /Users/you/workspace/pants/dist/pantsbuild.pants.testinfra-0.0.24.tar.gz
SUCCESS
</pre></div>
<h2 id="manipulating-pex-behavior-with-environment-variables">Manipulating PEX behavior with environment variables</h2>
<p>You can alter a PEX file's behavior during invocation by setting some
environment variables.</p>
<h3 id="pex_interpreter1"><code>PEX_INTERPRETER=1</code></h3>
<p>If you have a PEX file with a prescribed executable source or
<code>entry_point</code>, you can still drop into an interpreter with the
environment bootstrapped. Set <code>PEX_INTERPRETER=1</code> in your environment,
and the PEX bootstrapper skips any execution and instead launches an
interactive interpreter session.</p>
<h3 id="pex_verbose1"><code>PEX_VERBOSE=1</code></h3>
<p>If your environment is failing to bootstrap or simply bootstrapping very
slowly, it can be useful to set <code>PEX_VERBOSE=1</code> in your environment to
get debugging output printed to the console. Debugging output includes:</p>
<ol>
<li>Fetched dependencies</li>
<li>Built dependencies</li>
<li>Activated dependencies</li>
<li>Packages scrubbed out of sys.path</li>
<li>The sys.path used to launch the interpreter</li>
</ol>
<h3 id="pex_moduleentry_point"><code>PEX_MODULE=entry_point</code></h3>
<p>If you have a PEX file without a prescribed entry point, or want to
change the <code>entry_point</code> for a single invocation, you can set
<code>PEX_MODULE=entry_point</code> using the same format as described in the
<a href="build_dictionary.html#bdict_python_binary" pantsref="bdict_python_binary"><code>python_binary</code></a> Pants target.</p>
<p>This can be useful for bundling up some packages together and using that
single file to execute scripts from each of them.</p>
<p>Another common pattern is to link pytest into your PEX file, and run
<code>PEX_MODULE=pytest my_pex.pex tests/*.py</code> to run your test suite in its
isolated environment.</p>
<h3 id="pex_coverage"><code>PEX_COVERAGE</code></h3>
<p>There is nascent support for performing code coverage within PEX files
by setting <code>PEX_COVERAGE=<suffix></code>. By default the coverage files will
be written into the current working directory with the file pattern
<code>.coverage.<suffix></code>. This requires that the coverage Python module has
been linked into your PEX.</p>
<p>You can then combine the coverage files by running <code>PEX_MODULE=coverage</code>
<code>my_pex.pex</code> <code>.coverage.suffix*</code> and run a report using
<code>PEX_MODULE=coverage</code>
<code>my_pex.pex</code> report. Since PEX files are just zip files, coverage is able
to understand and extract source and line numbers from them in order to
produce coverage reports.</p>
<!-- main content end -->
<div class="generated">
Generated by <a href="docs.html">publish_docs</a>
from dist/markdown/html/examples/src/python/example/README.html 2017-05-29T10:30:51.100709
</div>
</div> <!-- mainflow -->
</div> <!-- content -->
</div>
<div class="col-md-1">
</div>
</div> <!-- row -->
</div> <!-- container-fluid -->
</div> <!-- page -->
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha384-I6F5OKECLVtK/BL+8iSLDEHowSAfUo76ZL9+kGAgTRdiByINKJaqTPH/QVNS1VDb" crossorigin="anonymous"></script>
<script src="bootstrap-custom.min.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-78111411-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>