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. These packages include pandas, numpy, pyarrow, jinja2 and psutil.

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.