Setting up an IDE

If you use a code-aware editor or IDE, such as PyCharm or VSCode, you may want to set it up to understand your code layout and dependencies. This will allow it to perform code navigation, auto-completion and other features that rely on code comprehension.

First-party sources

To get your editor to understand the repo's first-party sources, you will probably need to tell it about the repo's source roots. You can list those with:

$ ./pants roots

and then apply the corresponding IDE concept.

For example, in PyCharm you would mark each source root as a "Sources" folder. See Configuring Project Structure to learn more.

In VSCode, the Python extension will look for a file named .env in the current workspace folder. If the file is found, then it will be loaded and evaluated. For Python, this file can be used to set the PYTHONPATH variable. Having this file makes it possible to jump to definitions in the source code across multiple projects. It also makes cross-project refactoring possible.

For Python, to generate the .env file containing all the source roots, you can use something like this:

$ ROOTS=$(./pants roots --roots-sep=' ')
$ python3 -c "print('PYTHONPATH=\"./' + ':./'.join(\"${ROOTS}\".split()) + ':\$PYTHONPATH\"')" > .env

See Use of the PYTHONPATH variable to learn more about using the PYTHONPATH variable in VSCode.

Python third-party dependencies and tools

To get your editor to understand the repo's third-party dependencies, you will probably want to point it at a virtualenv containing those dependencies.

You can use the export goal to create a suitable virtualenv.

❯ ./pants export ::
Wrote virtualenv for the resolve 'python-default' (using CPython==3.9.*) to dist/export/python/virtualenvs/python-default

If you are using the "resolves" feature for Python lockfiles—which we strongly recommend—Pants will write the virtualenv to dist/export/python/virtualenvs/<resolve-name>. If you have multiple resolves, this means that Pants will create one virtualenv per resolve. You can then point your IDE to whichever resolve you want to load at the time.

Tool virtualenvs

./pants export will also create a virtualenv for certain Python tools you use via Pants, like
formatters like Black and Isort. This allows you to configure your editor to use the same version
of the tool that Pants uses for workflows like formatting on save.

To disable a certain tool, set its export option to false, e.g.:

export = false

Generated code

If you're using Protobuf and gRPC, you may want your editor to be able to index and navigate the generated source code.

Normally Pants treats generated code as an internal byproduct, and doesn't expose it. But you can run the export-codegen goal to generate code to a well-known output location for consumption:

$ ./pants export-codegen ::

The generated code will be written to dist/codegen, and you can now add them as sources in the IDE. For example, in PyCharm you would mark dist/codegen as a "Sources" folder.

Warning: you will have to manually rerun this goal when changes are made.

Remote debugging

You can use PyCharm to debug code running under Pants.

See the following links for instructions on how to do so under the test goal and under the run goal.

IDE integrations

We have not yet developed tight IDE integrations, such as a PyCharm plugin or a VSCode extension, that would allow the IDE to run Pants on your behalf. If you're interested in developing this functionality for your favorite IDE, let us know!

Did this page help you?