Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python Poetry documentation #12158

Merged
merged 4 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 58 additions & 33 deletions content/docs/languages-sdks/python/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The Pulumi programming model defines the core concepts you will use when creatin
Pulumi. [Concepts](/docs/intro/concepts) describes these concepts
with examples available in Python. These concepts are made available to you in the Pulumi SDK.

The Pulumi SDK is available to Python developers as a Pip package distributed on PyPI. To learn more,
The Pulumi SDK is available to Python developers as a package distributed on PyPI. To learn more,
[refer to the Pulumi SDK Reference Guide](/docs/reference/pkg/python/pulumi/).

The Pulumi programming model includes a core concept of `Input` and `Output` values, which are used to track how outputs of one resource flow in as inputs to another resource. This concept is important to understand when getting started with Python and Pulumi, and the [Inputs and Outputs](/docs/concepts/inputs-outputs/) documentation is recommended to get a feel for how to work with this core part of Pulumi in common cases.
Expand All @@ -44,53 +44,38 @@ asynchronous code within Python Pulumi programs.

### Virtual Environments

It is not required, but we recommend using a virtual environment to isolate the dependencies of your projects and ensure reproducibility between machines.
It is not required, but we recommend using a [virtual environment](https://docs.python.org/3/tutorial/venv.html) to isolate the dependencies of your projects and ensure reproducibility between machines.

As of Pulumi 2.4.0, new Python projects created with `pulumi new` will have a virtual environment created in a `venv` directory with required dependencies from `requirements.txt` installed in it, and Pulumi will automatically use this virtual environment when running the program.
When creating a new Python project with `pulumi new`, you are offered the choice between pip (default) and `poetry` to manage your dependencies. If you choose `pip`, Pulumi will create a virtual environment in a `venv` directory with required dependencies from `requirements.txt` installed in it. If you choose `poetry`, Pulumi will create a `pyproject.toml` file and run Poetry to create a virtual environment in its [default location](https://python-poetry.org/docs/basic-usage/#using-your-virtual-environment). Pulumi will automatically use this virtual environment when running the program.

This behavior is controlled by the following `virtualenv` `runtime` option in `Pulumi.yaml`:
This behavior is controlled by the `toolchain` and `virtualenv` `runtime` options in `Pulumi.yaml`.

To use pip, set the `toolchain` option to `pip`, along with the `virtualenv` option:

```yaml
runtime:
name: python
options:
toolchain: pip
virtualenv: venv
```

`virtualenv` is the path to a virtual environment to use.

Existing Python projects can opt-in to using the built-in virtual environment support by setting the `virtualenv` option. To manually create a virtual environment and install dependencies, run the following commands in your project directory:

{{< chooser os "macos,windows,linux" >}}

{{% choosable os macos %}}
To use Poetry, set the `toolchain` option to `poetry`:

```bash
$ python3 -m venv venv
$ venv/bin/pip install -r requirements.txt
```

{{% /choosable %}}

{{% choosable os linux %}}

```bash
$ python3 -m venv venv
$ venv/bin/pip install -r requirements.txt
```yaml
runtime:
name: python
options:
toolchain: poetry
```

{{% /choosable %}}
To further configure `poetry`, you can provide a [`poetry.toml` configuration file](https://python-poetry.org/docs/configuration/#local-configuration) in the project directory.

{{% choosable os windows %}}
Existing Python projects that do not use a virtual environment can opt-in to using the built-in virtual environment support by setting the above options. After updating the options, run `pulumi install` to create the virtual environment and install dependencies.

```bat
> python -m venv venv
> venv\Scripts\pip install -r requirements.txt
```

{{% /choosable %}}

{{< /chooser >}}
#### Self managed virtual environments

If you prefer to manage the virtual environment on your own (for example, using a tool like [Pipenv](https://github.com/pypa/pipenv)), you can delete the local `venv` directory and unset the `virtualenv` option in `Pulumi.yaml`:

Expand Down Expand Up @@ -119,7 +104,9 @@ When set, Pulumi will invoke the type checker before running your program. This

There are many [Pulumi Python packages](/registry) available.

To install a new dependency in the virtual environment, add an entry to `requirements.txt`, and run the following in your project directory:
#### Pip

To install a new dependency in the virtual environment when using `pip`, add an entry to `requirements.txt`, and run the following in your project directory:

{{< chooser os "macos,windows,linux" >}}

Expand Down Expand Up @@ -149,9 +136,47 @@ $ venv/bin/pip install -r requirements.txt

{{< /chooser >}}

#### Poetry

To add a new dependency when using `poetry`, run the `poetry add` command in your project directory:

{{< chooser os "macos,windows,linux" >}}

{{% choosable os macos %}}

```bash
$ poetry add ${PACKAGE_NAME}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a concrete package name to use here, are we ok with using syntax like ${PACKAGE_NAME} to indicate that this is a place holder?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, yeah, although we don't have a particular standard around this yet (and probably should). I'll poll the team and see what they think, but IMO this is good for now.

```

{{% /choosable %}}

{{% choosable os linux %}}

```bash
$ poetry add ${PACKAGE_NAME}
```

{{% /choosable %}}

{{% choosable os windows %}}

```bat
> poetry add ${PACKAGE_NAME}

```

{{% /choosable %}}

{{< /chooser >}}

### Dev Versions

Pulumi SDKs also publish pre-release versions, that include all the latest changes from the main development branch. If you would like to install them, you can use the `--pre` flag to do so. For example `pip install --pre -r requirements.txt`.
Pulumi SDKs also publish pre-release versions that include all the latest changes from the main development branch. If you would like to install a pre-release version, you can use the `--pre` flag with `pip` or the `--allow-prereleaases` flag with `poetry`. For example:

```bash
pip install --pre -r requirements.txt
poetry add --allow-prereleases ${PACKAGE_NAME}
```

## Package Documentation

Expand Down
3 changes: 2 additions & 1 deletion layouts/shortcodes/install-python.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
</div>
<div class="content">
<p>
<code>pip</code> is required to install dependencies. If you installed Python from source, with an installer from
Either <code>pip</code> or <code>poetry</code> is required to install dependencies. If you installed Python from source, with an installer from
<a href="https://python.org/" target="_blank" rel="noopener">python.org</a>, or via <a href="https://brew.sh/" target="_blank" rel="noopener">Homebrew</a> you should
already have <code>pip</code>. If Python is installed using your OS package manager, you may have to install <code>pip</code> separately, see
<a href="https://packaging.python.org/guides/installing-using-linux-tools/" target="_blank" rel="noopener">
Installing pip/setuptools/wheel with Linux Package Managers</a
>. For example, on Debian/Ubuntu you must run <code>sudo apt install python3-venv python3-pip</code>.
To install <code>poetry</code> follow the <a href="https://python-poetry.org/docs/#installation" target="_blank" rel="noopener">installation instructions</a>.
</p>
</div>
</div>
Expand Down
Loading