Gustavo
Gustavo

Reputation: 716

How to build multiple packages from a single python module using pyproject.toml and poetry?

I want to achieve a similar behavior as the library Dask does, it is possible to use pip to install dask, dask[dataframe], dask[array] and others. They do it by using the setup.py with a packages key like this. If I install only dask the dask[dataframe] is not installed and they warn you about this when executing the module.

I found this in the poetry documentation but when I execute poetry build I only get one .whl file with all of the packages within.

How can I package my module to be able to install specific parts of a library using poetry?

Upvotes: 5

Views: 11810

Answers (1)

Gustavo
Gustavo

Reputation: 716

Actually the Dask example does not install subpackages separately, it just installs the custom dependencies separately as explained in this link.

In order to accomplish the same behavior using poetry you need to use this (as mentioned by user @sinoroc in this comment)

The example pyproject.toml from the poetry extras page is this:

[tool.poetry]
name = "awesome"

[tool.poetry.dependencies]
# These packages are mandatory and form the core of this package’s distribution.
mandatory = "^1.0"

# A list of all of the optional dependencies, some of which are included in the
# below `extras`. They can be opted into by apps.
psycopg2 = { version = "^2.7", optional = true }
mysqlclient = { version = "^1.3", optional = true }

[tool.poetry.extras]
mysql = ["mysqlclient"]
pgsql = ["psycopg2"]

By using poetry build --format wheel a single wheel file would be created.

In order to install a specific set of extra dependencies using pip and the wheel file you should use:

pip install "wheel_filename.whl[mysql]"

Upvotes: 5

Related Questions