My database wrapper package (bh-database) requires either the mysql-connector-python package or the psycopg2-binary package to function. Previously, it would install both packages regardless of which database the application was using. However, I recently learned how to make the installation of the required package optional. In this post, we will explore how to achieve this.

115-feature-image.png
Python: How to Enable the Installation of Optional Packages

Let’s take a look at the commands that enable the installation of optional packages mysql-connector-python or psycopg2-binary for the bh-database package:

pip install bh-database[mysql-connector-python]
pip install bh-database[psycopg2-binary]
  1. The first command installs the bh-database and the required mysql-connector-python packages.
  2. The second command installs the bh-database and the required psycopg2-binary packages.

👉 If the application works with both MySQL and PostgreSQL, then run both commands to install the two required packages.

To achieve this, we need to configure the pyproject.toml file as follows:

34
35
36
37
38
39
40
[project.optional-dependencies]
mysql-connector-python = [
    "mysql-connector-python"
]
psycopg2-binary = [
    "psycopg2-binary"
]

This is documented in the section titled Dependencies and requirements of the official Python documentation page Writing your pyproject.toml.

The tokens to the right of the equal sign equal sign (‘=’) are keys. In this case, they are mysql-connector-python and psycopg2-binary. We could name them whatever seems appropriate, for example, mysql and postgresql respectively.

Basically, under the project.optional-dependencies table, we define our optional entries. Please note that each entry key can have more than one package listed as per the official documentation.

The current version of bh-database is 0.0.6. The build command produces the following two files: bh_database-0.0.6-py3-none-any.whl and bh_database-0.0.6.tar.gz. We can also install bh_database-0.0.6-py3-none-any.whl into an active virtual environment using the below commands, given that bh_database-0.0.6-py3-none-any.whl resides at the same level as the active virtual environment’s directory:

pip install bh_database-0.0.6-py3-none-any.whl[mysql-connector-python]
pip install bh_database-0.0.6-py3-none-any.whl[psycopg2-binary]

👎 However, configuring bh_database-0.0.6-py3-none-any.whl in pyproject.toml such as:

[project]
...
dependencies = [
    'tomli; python_version >= "3.12"',
    'fastapi',
    ...
    'bh_database-0.0.6-py3-none-any.whl[mysql-connector-python]',
    'bh_database-0.0.6-py3-none-any.whl[psycopg2-binary]',
    ...,
]

💥 will not work.

The above observations are based on my attempts at trying out bh_database-0.0.6-py3-none-any.whl. I have not come across any such discussions or official documentation. Please do not quote me, I am not taking responsibility for anything. Please try it out for yourselves and decide based on your own observations.

I have not come across any tutorials or sites which mentioned optional package installation in Python. I just assumed that this feature is not available. My pyproject.toml files just work, so I did not bother to look into the official documentation on pyproject.toml file. Only when I started learning FastAPI did I see the optional package installation feature. It seems to be not a very actively discussed Python topic. Hence, I would just like to share my learning experience.

Thank you for reading. I hope you find the information in this post useful. Stay safe, as always.

✿✿✿

Feature image source: