...
Create a Python virtual environment
...
1. 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.
2. In the top right corner of your IDAS home page, click New.
3. Click Terminal in order to start a Terminal session.
4. Create a virtual environment called "projectName" inside the directory "virtenvs". This will create the "virtenvs" directory if it doesn’t exist.
Code Block |
---|
python3 -m venv $HOME/virtenvs/projectName |
a) 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
5. Once this environment is created, we can activate it.
Code Block |
---|
source $HOME/virtenvs/projectName/bin/activate |
6. The command prompt in your Terminal will change to indicate the active environment.
Code Block |
---|
(projectName) hawkid@jupyter-notebook-research-hawkid:~$ |
...
7. There are several ways to install packages in this virtual environment:
a) You can specify the names of the packages that you want to install:
Code Block |
---|
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.
b) If you have a requirement file that contains a list of packages that you want to install, you can use:
Code Block |
---|
pip3 install -r requirements.txt |
8. If you want to use Jupyter Notebook with this virtual environment, we can install a kernel. First, install the IPython kernel:
Code Block |
---|
pip3 install -U ipykernel |
9. Now install a kernel in this environment:
Code Block |
---|
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.
10. 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. Click on that new option will start a notebook using the Python virtual environment you just created.
Other useful commands to manage your Python virtual environments
...