Background
There are instances where a different Python environment is required to use specific analytical tools compared to the usual environment. This tip provides steps for creating a virtual environment with `venv` and displaying it as a kernel in a Notebook.
Caution
This guide is intended for those proficient in Python programming and familiar with pip install and venv behavior. Incorrect execution of the steps may necessitate resetting the Matlantis Python environment.
Additionally, the virtual environment created following these steps will be deleted if the Python environment is reset from the Dashboard.
Steps
Open the terminal and execute the following three commands in order. This procedure will create a virtual environment named `py39-custom`. This environment will appear in the Notebook as `Python 3.9 custom`.
1. Creating the Virtual Environment
/usr/local/pyenv/versions/python39/bin/python -m venv /home/jovyan/.py39-custom
2. Configuration to Display the Kernel in Jupyter Lab
mkdir -p /home/jovyan/.local/share/jupyter/kernels/py39-custom/
cat <<EOF > /home/jovyan/.local/share/jupyter/kernels/py39-custom/kernel.json
{
"argv": [
"/home/jovyan/.py39-custom/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3.9 custom",
"language": "python",
"env": {
"PATH": "/home/jovyan/.py39-custom/bin:/usr/local/pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"PYTHONPATH": "/home/jovyan/.py39-custom/lib/python3.9/site-packages:/usr/local/pyenv/versions/python39/lib/python3.9/site-packages",
"CPLUS_INCLUDE_PATH": "/usr/local/pyenv/versions/3.9.16/include/python3.9",
"LD_LIBRARY_PATH": "/usr/local/pyenv/versions/3.9.16/lib/",
"LIBRARY_PATH": "/usr/local/pyenv/versions/3.9.16/lib/"
}
}
EOF
3. Usage in Notebook
To apply the settings, please reload any existing notebooks.
After this operation, it will be available for use in Jupyter Notebooks.
4. Usage in the Terminal
When using the virtual environment created on the terminal, please use the following command:
source ~/.py39-custom/bin/activate
Images for Operation Procedure

Executing with Different Python Versions
As venv cannot create environments across different Python versions, execution is only possible with the Python versions provided by Matlantis.