This article will help you create a python virtual environment. In this example, we'll create one on a Bren computing server.
- Log into a Bren computing server.
- Open up Terminal session in either JupyterLab or Jupyter Notebooks.
- Run the following commands:
To see if python is pointing to correct python3 install.
which python3
Check that you are in the root of your home folder (or where you want to store your virtual environment). File path of your home folder is /Users/(BrenUsername)
pwd
Create environment
python3 -m venv environment_name
View top level tree of virtual environment
tree -d -L 1 environment_name
Activate virtual environment
. /Users/(BrenUsername)/environment_name/bin/activate
Register kernel with Jupyter
ipython kernel install --user --name=environment_name
Verify that kernel is registered with Jupyter
jupyter kernelspec list
Once you're done, you can deactivate the environment.
. /Users/(brenusername)/environment_name/bin/deactivate
Then remove kernel from Jupyter
jupyter kernelspec uninstall environment_name
For MEDS computation server (workbench-1 and workbench-2):
- Create your environment. My test environment is named “test_env”
- Type “y” to proceed
- Activate your conda environment. You’ll know it is activated if (base) at the beginning of your line changes to (test_env).
- Pip install ipykernel is allow jupyter to utilize the environment
- If you have a requirements.txt file you want to install into your new environment, you can run the following code (you will need to change the file path to your requirements.txt file):
- Otherwise, you can pip install or conda install python modules like normal
- Install jupyter kernel in your conda environment. The example environment name is “test_env” and display name is “Test Environment”
- Select the Posit logo to go back to your session launcher. Then join the current session again. Without completing this step, your environment will not show up.
- Now you should be able to launch a new notebook / console with your new environment:
____________
If you have any questions or run into any issues, please feel free to send us an email at compute@bren.ucsb.edu.