|
1 |
| -# example-python |
2 |
| -An example repository to demonstrate Python support in Pants. |
| 1 | +# pantbuild-example-python |
3 | 2 |
|
4 |
| -See [pantsbuild.org](https://www.pantsbuild.org/docs) for much more detailed documentation. |
5 |
| - |
6 |
| -This is only one possible way of laying out your project with Pants. See |
7 |
| -[pantsbuild.org/docs/source-roots#examples](https://www.pantsbuild.org/docs/source-roots#examples) for some other |
8 |
| -example layouts. |
9 |
| - |
10 |
| -# Running Pants |
11 |
| - |
12 |
| -You run Pants goals using the `pants` launcher binary, which will bootstrap the |
13 |
| -version of Pants configured for this repo if necessary. |
14 |
| - |
15 |
| -See [here](https://www.pantsbuild.org/docs/installation) for how to install the `pants` binary. |
16 |
| - |
17 |
| -> :question: Running with Apple Silicon and/or macOS? You will want to make changes to the `search_path` and |
18 |
| -`interpreter_constraints` values in `pants.toml` before running `pants` - there is guidance in `pants.toml` |
19 |
| -for those settings. |
20 |
| - |
21 |
| -Use `pants --version` to see the version of Pants configured for the repo (which you can also find |
22 |
| -in `pants.toml`). |
23 |
| - |
24 |
| -# Goals |
25 |
| - |
26 |
| -Pants commands are called _goals_. You can get a list of goals with |
27 |
| - |
28 |
| -``` |
29 |
| -pants help goals |
30 |
| -``` |
31 |
| - |
32 |
| -# Targets |
33 |
| - |
34 |
| -Targets are a way of setting metadata for some part of your code, such as timeouts for tests and |
35 |
| -entry points for binaries. Targets have types like `python_source`, `resources`, and |
36 |
| -`pex_binary`. They are defined in `BUILD` files. |
37 |
| - |
38 |
| -Pants goals can be invoked on targets or directly on source files (which is often more intuitive and convenient). |
39 |
| -In the latter case, Pants locates target metadata for the source files as needed. |
40 |
| - |
41 |
| -## File specifications |
42 |
| - |
43 |
| -Invoking goals on files is straightforward, e.g., |
44 |
| - |
45 |
| -``` |
46 |
| -pants test helloworld/greet/greeting_test.py |
47 |
| -``` |
48 |
| - |
49 |
| -You can use globs: |
50 |
| - |
51 |
| -``` |
52 |
| -pants lint helloworld/greet/*.py |
53 |
| -``` |
54 |
| - |
55 |
| -But note that these will be expanded by your shell, so this is equivalent to having used |
56 |
| - |
57 |
| -``` |
58 |
| -pants lint helloworld/greet/__init__.py helloworld/greet/greeting.py helloworld/greet/greeting_test.py |
59 |
| -``` |
60 |
| - |
61 |
| -If you want Pants itself to expand the globs (which is sometimes necessary), you must quote them in the shell: |
62 |
| - |
63 |
| -``` |
64 |
| -pants lint 'helloworld/greet/*.py' |
65 |
| -``` |
66 |
| - |
67 |
| -You can run on all changed files: |
68 |
| - |
69 |
| -``` |
70 |
| -pants --changed-since=HEAD lint |
71 |
| -``` |
72 |
| - |
73 |
| -You can run on all changed files, and any of their "dependents": |
74 |
| - |
75 |
| -``` |
76 |
| -pants --changed-since=HEAD --changed-dependents=transitive test |
77 |
| -``` |
78 |
| - |
79 |
| -## Target specifications |
80 |
| - |
81 |
| -Targets are referenced on the command line using their address, of the form `path/to/dir:name`, e.g., |
82 |
| - |
83 |
| -``` |
84 |
| -pants lint helloworld/greet:lib |
85 |
| -``` |
86 |
| - |
87 |
| -You can glob over all targets in a directory with a single trailing `:`, or over all targets in a directory |
88 |
| -and all its subdirectories with a double trailing `::`, e.g., |
89 |
| - |
90 |
| -``` |
91 |
| -pants lint helloworld:: |
92 |
| -``` |
| 3 | +An example repository to demonstrate Python support in Pants with remote caching in Namespace. |
93 | 4 |
|
94 |
| -## Globbing semantics |
95 |
| - |
96 |
| -When you glob over files or targets, Pants knows to ignore ones that aren't relevant to the requested goal. |
97 |
| -For example, if you run the `test` goal over a set of files that includes non-test files, Pants will just ignore |
98 |
| -those, rather than error. So you can safely do things like |
99 |
| - |
100 |
| -``` |
101 |
| -pants test :: |
102 |
| -``` |
103 |
| - |
104 |
| -To run all tests. |
105 |
| - |
106 |
| -# Example Goals |
107 |
| - |
108 |
| -Try these out in this repo! |
109 |
| - |
110 |
| -## List targets |
111 |
| - |
112 |
| -``` |
113 |
| -pants list :: # All targets. |
114 |
| -pants list 'helloworld/**/*.py' # Just targets containing Python code. |
115 |
| -``` |
116 |
| - |
117 |
| -## Run linters and formatters |
118 |
| - |
119 |
| -``` |
120 |
| -pants lint :: |
121 |
| -pants fmt helloworld/greet:: |
122 |
| -``` |
123 |
| - |
124 |
| -## Run MyPy |
125 |
| - |
126 |
| -``` |
127 |
| -pants check :: |
128 |
| -``` |
129 |
| - |
130 |
| -## Run tests |
131 |
| - |
132 |
| -``` |
133 |
| -pants test :: # Run all tests in the repo. |
134 |
| -pants test --output=all :: # Run all tests in the repo and view pytest output even for tests that passed (you can set this permanently in pants.toml). |
135 |
| -pants test helloworld/translator:tests # Run all the tests in this target. |
136 |
| -pants test helloworld/translator/translator_test.py # Run just the tests in this file. |
137 |
| -pants test helloworld/translator/translator_test.py -- -k test_unknown_phrase # Run just this one test by passing through pytest args. |
138 |
| -``` |
139 |
| - |
140 |
| -## Create a PEX binary |
141 |
| - |
142 |
| -The `package` goal requires specifying a target which can be packaged. In this case, the there is a `pex_binary` target with the name `pex_binary` in the `helloworld/BUILD` file. |
143 |
| - |
144 |
| -``` |
145 |
| -pants package helloworld:pex_binary |
146 |
| -``` |
147 |
| - |
148 |
| -The pex file is output to `dist/helloworld/pex_binary.pex` and can be executed directly. |
149 |
| - |
150 |
| -## Run a binary directly |
151 |
| - |
152 |
| -``` |
153 |
| -pants run helloworld/main.py |
154 |
| -``` |
155 |
| - |
156 |
| -## Open a REPL |
157 |
| - |
158 |
| -``` |
159 |
| -pants repl helloworld/greet:lib # The REPL will have all relevant code and dependencies on its sys.path. |
160 |
| -pants repl --shell=ipython helloworld/greet:lib --no-pantsd # To use IPython, you must disable Pantsd for now. |
161 |
| -``` |
162 |
| - |
163 |
| -## Build a wheel / generate `setup.py` |
164 |
| - |
165 |
| -This will build both a `.whl` bdist and a `.tar.gz` sdist. |
166 |
| - |
167 |
| -``` |
168 |
| -pants package helloworld/translator:dist |
169 |
| -``` |
170 |
| - |
171 |
| -## Count lines of code |
172 |
| - |
173 |
| -``` |
174 |
| -pants count-loc '**/*' |
175 |
| -``` |
176 |
| - |
177 |
| -## Generate or update a lockfile containing the dependencies |
178 |
| - |
179 |
| -``` |
180 |
| -pants generate-lockfiles --resolve=python-default |
181 |
| -``` |
182 |
| - |
183 |
| - |
184 |
| -## Create virtualenv for IDE integration |
| 5 | +See [pantsbuild.org](https://www.pantsbuild.org/docs) for much more detailed documentation. |
185 | 6 |
|
186 |
| -``` |
187 |
| -pants export --resolve=python-default |
188 |
| -``` |
| 7 | +This example is based on [pantsbuild/example-python](https://github.com/pantsbuild/example-python). |
0 commit comments