Skip to content

Commit

Permalink
feat: Create features and environments from extras (prefix-dev#1077)
Browse files Browse the repository at this point in the history
Co-authored-by: Ruben Arts <[email protected]>
Co-authored-by: Ruben Arts <[email protected]>
  • Loading branch information
3 people authored Apr 5, 2024
1 parent 6331cc9 commit 1d259ff
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 85 deletions.
43 changes: 43 additions & 0 deletions docs/advanced/pyproject_toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,49 @@ matplotlib = "*"
This would result in the conda dependencies being installed and the pypi dependencies being ignored.
As pixi takes the conda dependencies over the pypi dependencies.

## Optional dependencies
If your python project includes groups of optional dependencies, pixi will automatically interpret them as [pixi features](../configuration.md#the-feature-table) of the same name with the associated `pypi-dependencies`.

You can add them to pixi environments manually, or use `pixi init` to setup the project, which will create one environment per feature. Self-references to other groups of optional dependencies are also handled.

For instance, imagine you have a project folder with a `pyproject.toml` file similar to:
```toml
[project]
name = "my_project"
dependencies = ["package1"]

[project.optional-dependencies]
test = ["pytest"]
all = ["package2","my_project[test]"]
```

Running `pixi init` in that project folder will transform the `pyproject.toml` file into:
```toml
[project]
name = "my_project"
dependencies = ["package1"]

[project.optional-dependencies]
test = ["pytest"]
all = ["package2","my_project[test]"]

[tool.pixi.project]
channels = ["conda-forge"]
platforms = ["linux-64"] # if executed on linux

[tool.pixi.environments]
default = {features = [], solve-group = "default"}
test = {features = ["test"], solve-group = "default"}
all = {features = ["all", "test"], solve-group = "default"}
```
In this example, three environments will be created by pixi:

- **default** with 'package1' as pypi dependency
- **test** with 'package1' and 'pytest' as pypi dependencies
- **all** with 'package1', 'package2' and 'pytest' as pypi dependencies

All environments will be solved together, as indicated by the common `solve-group`, and added to the lock file. You can edit the `[tool.pixi.environments]` section manually to adapt it to your use case (e.g. if you do not need a particular environment).

## Example
As the `pyproject.toml` file supports the full pixi spec with `[tool.pixi]` prepended an example would look like this:
```toml title="pyproject.toml"
Expand Down
2 changes: 2 additions & 0 deletions examples/flask-hello-world-pyproject/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# GitHub syntax highlighting
pixi.lock linguist-language=YAML
2 changes: 2 additions & 0 deletions examples/flask-hello-world-pyproject/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# pixi environments
.pixi
*.egg-info
119 changes: 111 additions & 8 deletions examples/flask-hello-world-pyproject/pixi.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion examples/flask-hello-world-pyproject/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ version = "0.1.0"
description = "Example how to get started with flask in a pixi environment."
readme = "README.md"
requires-python = ">=3.11"
dependencies = ["flask==2.*", "pytest"]
dependencies = ["flask==2.*"]

[project.optional-dependencies]
test = ["pytest"]

[build-system]
requires = ["setuptools", "wheel"]
Expand All @@ -17,6 +20,12 @@ platforms = ["linux-64", "osx-arm64", "osx-64", "win-64"]
[tool.pixi.pypi-dependencies]
flask-hello-world-pyproject = { path = ".", editable = true }

[tool.pixi.environments]
default = { features = [], solve-group = "default" }
test = { features = ["test"], solve-group = "default" }

[tool.pixi.tasks]
start = "python -m flask --app flask_hello_world_pyproject.app:app run --port=5050"

[tool.pixi.feature.test.tasks]
test = "pytest -v tests/*"
Loading

0 comments on commit 1d259ff

Please sign in to comment.