Skip to main content

Using packages

Working with packages is an important part of using Python in Hex.

Use a pre-installed package

Hex has a number of popular packages pre-installed and immediately ready for import into your project. You can see the list of pre-installed packages in the Environment tab in the left-hand bar.

Install new packages through pip

You can add new packages to a project using pip, much as you would in a local environment.

Create a new cell, and use a ! to trigger a terminal command:

!pip3 install astropy

Because projects are run from top to bottom with a new kernel every time a user visits, packages installed with !pip install have to be reinstalled every time your project runs.

Fixed package versioning

When starting a new kernel, Hex loads specific versions of some packages that are core to its functionality. This process causes these packages to remain fixed to the versions that Hex imports. Attempting to update the version of these packages can result in errors.

PackageVersion
pandas1.4
psutil5.8.0
duckdb0.5.1
numpy1.22
ipykernel5.4.3
ipython7.32.0
ipython_genutils0.2.0
jedi0.17.2
Jinja23.1.2
jinjasql0.1.8
vegafusion1.0.4
vegafusion-python-embed1.0.4
cryptography3.3.2
snowflake-connector-python2.7.12

Troubleshoot package installation

Occasionally after upgrading a package, importing the package will result in a ContextualVersionConflict exception that indicates the package is still on the version originally installed in Hex. This can happen if the package relies on the pkg_resources module to check for version conflicts during its setup. You can resolve this issue by reloading pkg_resouces after the package installation, but before the package is imported:

import pkg_resources
from importlib import reload

!pip install {my_package}

reload(pkg_resources)

import {my_package}

GitHub packages

If your workspace has been set up to use packages from a GitHub repository, you can install those packages for use in any project. See GitHub packages for setup instructions.

To import functions from .py files included in your package, you will first need to add the path to those files to your Python path. Packages are automatically saved to a directory with the naming convention of {{GitHub user}}-{{GitHub package name}}. For example, the screenshot above shows how to import functions from the draw_hexagon.py file of the draw_hexagon packages, which is associated with the izzymiller GitHub user.

Depending on how your GitHub package has been configured, some modules may have already been installed. In this case, those modules are immediately available for use, just import {{package name}} in a Python cell as usual.

GitHub packages are re-imported from GitHub with every kernel restart of your project. If your package is particularly large this can add some overhead to the intial performance and load time of your projects. Additionally, if your GitHub package was configured to import a specific branch of the package, upon each kernel restart, the latest version of the branch will be imported.

Set number of available cores

Some packages attempt to autodect the number of available cores, so they can generate a certain number of threads accordingly. In some cases, a package may incorrectly scope the kernel being used in the project. To optimize performance for packages that behave in this way, users can use hextoolkit to correctly assign the number of available cores using code like the following:

import hextoolkit.kernel
num_cpus = hextoolkit.kernel.cpu_count()

Then, the package-specific parameter used to set the number of available cores can be set to num_cpus.