In this page:
We strongly encourage using virtual environments to manage your projects. You can create a Python virtual environment for each of your projects. If you make changes to one of your Python virtual environments, your other environments are not affected. For this reason, virtual environments are helpful especially if you have multiple ongoing projects that require different versions of Python packages.
This article illustrates creating a Python virtual environment in IDAS. For more general information about using Python virtual environments, please see the Python Packaging User Guide and the Python Tutorial.
Aside: You can also use conda to manage packages and environments for any programming language. Please see the Conda Wiki article for more information about using conda in IDAS. For more general information about conda, see the Conda User Guide.
Create a Python virtual environment
First, create an IDAS instance with Python. If you are a student in a class that uses Python in IDAS, follow the instructions here to access your class instance.
In the top right corner of your IDAS home page, click New.
Click Terminal in order to start a Terminal session.
Create a virtual environment called "projectName" inside the directory "virtenvs". This will create the "virtenvs" directory if it doesn’t exist.
python3 -m venv $HOME/virtenvs/projectName
- Note: You will probably have several virtual environments for unrelated projects, so we created the "virtenvs" directory to organize them. You can create another (separate) virtual environment in the future like so: python3 -m venv $HOME/virtenvs/another-project
Once this environment is created, we can activate it.
source $HOME/virtenvs/projectName/bin/activate
The command prompt in your Terminal will change to indicate the active environment.
(projectName) hawkid@jupyter-notebook-research-hawkid:~$
To install packages in this virtual environment:
pip3 install -U package1 package2 package3
The "-U" option upgrades all specified packages to the newest available version. Omit this option if you don't want to upgrade packages.
If you want to use Jupyter Notebook with this virtual environment, we can install a kernel. First, install the IPython kernel:
pip3 install -U ipykernel
Now install a kernel in this environment:
python -m ipykernel install --user --name projectName --display-name "Python Project Name"
The value for "--name" is used by Jupyter internally. Any existing kernel with the same "--name" value will be overwritten. The "--display-name" will be displayed in the Notebook menu in the Jupyter Hub tree page.
- Go back to the Jupyter Hub tree page. Press F5 to reload it. Under New / Notebook, a new option for your kernel will now be available. In this example screenshot, the "Python 3.8" kernel was just installed and now became available to use.
Other useful commands to manage your Python virtual environments
The following commands may be useful to manage your Python virtual environments. Type the following commands in Terminal. For more general information about using Python virtual environments, please see the Python Packaging User Guide and the Python Tutorial. For more information about pip, the Python package manager, see the pip documentation.
Re-enter a virtual environment that was previously created:
source $HOME/virtenvs/projectName/bin/activate
Leave the virtual environment once you are finished working with it:
deactivate
Install packages in the virtual environment:
# first activate the virtual environment source $HOME/virtenvs/projectName/bin/activate # then install packages. The "-U" option upgrades all specified packages to the newest available version. Omit this option if you don't want to upgrade packages. pip3 install -U package1 package2 package3
List all packages installed in the virtual environment:
# first activate the virtual environment source $HOME/virtenvs/projectName/bin/activate # list all installed packages pip list
Contact
If you have any questions or comments, please contact research-computing@uiowa.edu.