Create a Python Virtual Environment

The steps below will walk you through creating a Python virtual environment and kernel so that it may be used in your notebooks.

1. Open a Terminal.

If you're not already logged in, follow the steps found here. In the upper-right corner, click the New button and select Terminal.

2. Create virtual environment

In the Terminal, enter the command below to create a virtual environment. In this example, myEnv is the name used for the virtual environment, but you may name this whatever you'd like.

conda create -n myEnv python=2.7

Press return when asked to proceed. All packages needed by the virtual environment will be downloaded and installed.

3. Activate virtual environment

Run source activate myEnv to activate your virtual environment. Again, you may have used a different name, so substitute as appropriate. You should now see a prompt with the name of your virtual environment in parenthesis as shown.

grahamhenke@bacon:~$ source activate myEnv
(myEnv) grahamhenke@bacon:~$

4. Install packages

Now that you are in your virtual environment, you may install packages using conda install or pip install. For example, to install the requests package, run:

conda install requests

Note: We highly recommend trying conda install first, as it installs low level dependencies that some packages (such as tensorflow) need. If this does not work, then use pip install.

5. Install ipykernel

To use your virtual environment and the packages installed in it, you will need to create an environment kernel. To do this, you first need to install ipykernel by running the following:

conda install ipykernel

6. Create the kernel

Now create the kernel for your environment by running:

python -m ipykernel install --user --name myEnv --display-name myEnv

Note: Neither the name or display-name have to be the same as your virtual environment name, but it is recommended to avoid confusion.

7. Open a notebook with the kernel

Navigate back to the Home tab. Make sure to first refresh the page. Next, click the New button and select the name of your kernel, which is myEnv in this example.

8. Import packages

You can now import and use packages from your kernel. Notice the requests package imports successfully.

Note: Please be aware that common packages you may be used to using (such as pandas) are not installed by default and you will have to explicitly install these using conda install in your virtual environment.