Using Python Virtual environments in ParaView 5.13.0

August 29, 2024
ParaView Logo inside a Hexagon

Starting with ParaView 5.13.0, you will be able to take advantage of Python virtual environments for many use cases. This opens up the possibility to add additional Python packages that are not provided by ParaView’s integrated Python installation.

Python virtual environments

A Python virtual environment is a way to have “virtual” Python installations with different packages installed but with only a single real Python installation backing them. They are a useful way to organize per-project Python environments that have installed only the packages needed for that project. If you have a project in a directory named ProjectX that requires several Python packages, you can create a virtual environment in the ProjectX directory and install into it only the packages your project needs. When you are ready to run ProjectX, simply activate its virtual environment first, and all the packages installed in it will be available to it.

A popular way to create and manage virtual environments is with the venv package that is available by default in Python 3. When you create a virtual environment, venv creates a directory on your filesystem where Python packages you install are stored. To use the virtual environment, you “activate” it, which more or less boils down to adding a specific subdirectory of this directory to the Python PATH environment variable, making those packages available to the Python interpreter.

Using virtual environments in ParaView’s Python

ParaView builds and packages its own Python interpreter and libraries to provide powerful scripting capabilities. The binary packages available on www.paraview.org ship with a variety of Python packages. However, if ParaView does not come with the Python package you want to use in your ParaView Python scripts, you would have been out of luck in the past.

Starting in ParaView 5.13.0, there is a new option that lets you use many Python packages installed in virtual environments for a parallel Python installation on your system. The only requirement is that this other Python installation is compatible with the Python in ParaView. ParaView 5.13.0 ships with Python 3.10, so that means that if you have a Python 3.10 installation on the same computer, you can create a virtual environment for this other Python installation and tell ParaView’s Python to use it. The end result is that any Python packages installed in this other Python will be available in ParaView’s Python environments, including the Programmable Source, Programmable Filter, and Python Shell.

Let’s say we have created a virtual environment named .venv in our ProjectX directory. To use a virtual environment ProjectX in ParaView, add the command-line option –venv to specify the full directory path to the directory created for ProjectX:

paraview --venv /path/to/ProjectX/.venv

Now, you’ll have all packages you installed available in ProjectX’s virtual environment available in ParaView!

Putting it all together

Let’s assume that you have ParaView 5.13.0 (or a recent nightly build) downloaded from www.paraview.org. Its Python environment doesn’t come with the PyYAML package, but let’s also assume you really want to use PyYAML in ProjectX. Here are the steps needed to make it available in ParaView:

  1. Make sure Python 3.10 is installed on your system (one different from the one that comes with ParaView)
  1. Create a virtual environment in the ProjectX directory. This is often named .venv by convention

> cd ProjectX

> python3.10 -m venv .venv

  1. Activate the virtual environment and install yaml

> source .venv/bin/activate

> pip install pyyaml

  1. Deactivate the virtual environment

> deactivate

  1. Now run ParaView with this virtual environment

> paraview --venv .venv/

  1. Open up ParaView’s Python Shell and type import yaml at the console prompt. It should import successfully.

Now you can incorporate functions from the PyYAML package in your ParaView Python scripts. Note: if you did not supply the --venv argument with the virtual environment containing PyYAML, the import yaml statement would have failed.

This example shows --venv used with the ParaView client application. It is also available to other executables in the ParaView suite, including pvpython, pvbatch, and pvserver.

Requirements and caveats

For the --venv option to work, it is assumed that:

  • The major and minor versions of Python match between ParaView and the system Python you have installed.
  • The packages you want to use in ParaView are mostly pure Python; packages that depend on dynamic libraries may have been built with a compiler or C runtime that is not compatible with the one used to build ParaView’s Python.
  • The libraries in your virtual environment don’t conflict with those available in ParaView. Binary distributions of ParaView include a number of Python packages, and if you install a different version of a package in your virtual environment, you may encounter runtime problems. This is particularly true for packages that include compiled libraries, such as NumPy. 

Acknowledgements

Authorship of this article was funded by Sandia National Laboratories.

Sandia National Laboratories is a multimission laboratory managed and operated by National Technology and Engineering Solutions of Sandia, LLC., a wholly owned subsidiary of Honeywell International, Inc., for the U.S. Department of Energy’s National Nuclear Security Administration under contract DE-NA0003525.

SAND2024-11261O

Leave a Reply