In this blog post, we are going to explore how to create and export anaconda environments.
How to create anaconda environment and make it available in Jupyter lab.
There are 2 ways to create anaconda environments. You can create anaconda environment from anaconda prompt command line or from anaconda navigator.
How to create anaconda environment from anaconda navigator
Step 1: Open anaconda navigator
Step 2: click on “environment” located on the left side of the screen
Step 3: Click on “create” located on the bottom of the screen.
Step 4: Type in the name of the new environment–>choose python and/or R installation–>and click create.
Note: Make sure the name you give the new environment has never been used before.
Step 5: Add packages to the new environment by clicking the environment name
- Search for the package you want in the search bar–>click on “ALL” from the drop down menu.
- Select the name of the package you want to install, then click “apply” located in the lower right side of the screen.
- Clicking apply installs the selected package.
This video shows you how to install a package in anaconda. Is true the video focuses on plotly, but you can use this technique to install any package.
How to create anaconda environment from (anaconda prompt in windows, terminal on mac) and make it available in Jupyter lab.
If you are looking to create an anaconda environment for a specific project using your command line, then follow these instructions.
Step 1: Navigate to the folder that contains the project you are working on using anaconda prompt on windows or terminal on mac
Step 2: Run
conda create -n Environment-name python==3.7
Once the command completes, your conda environment should be ready
Next step is to add the required python packages. You will need to ‘activate’ the conda environment to add packages.
Step 3: To activate anaconda environment, type in
source activate Environment-name on Terminal or
conda activate Environment-name on Anaconda Prompt
How to install packages in anaconda environment (2 ways)
After activating your conda environment, you may want to install the packages for the project from the terminal. You can also install the packages for the project from anaconda navigator using STEP 5 FROM ABOVE.
OR…you can also use the instructions below to install package in your anaconda environment.
To install the required packages from the terminal, create a document called “requirements.txt” that has all the packages you will need to install. Here is an example of what it looks like.
This document should be on the folder/directory that has the project you are working on.
Step 4: Once your environment is activate, run
pip install -r requirements.txt which will install the required packages into your environment.
You could alternatively install the package individually by doing
pip install package name. But be warned, it is better to put the packages in a requirements.txt file and install it from there.
Adding Virtual Environment to Jupyter Lab
To make the environment available in jupyterlab, we need to add an Ipython Kernel reference to the conda environment, so we can use it from JupyterLab.
To add conda environment to jupyter lab,
Step 5: run
python -m ipykernel install --user --name Environment-name --display-name "what you want jupyterlab environment called"
This will add a json object to an ipython file, so JupterLab will know that it can use this isolated instance of Python environment.
Step 6: Deactivate your conda environment and launch JupyterLab. You should now see “what you want jupyterlab environment called” in the list of available kernels on jupyter lab launch screen.
Step 7: Run
conda deactivate to deactivate your environment
How to Export anaconda environments.
Exporting anaconda environments are actually easy. Let’s dive in.
First, go to your terminal and follow the steps below. I have not found how to export anaconda environment using anaconda navigator, so the terminal is our only option.
Step 1: Activate the environment to export by typing:
conda activate myenv
On Mac, type
source activate myenv
Step 2: Export your active environment to a new file by typing:
conda env export > environment.yml
On Mac, do
source env export > environment.yml
Go the directory you are currently working on and you will find your environment.yml file.
Step 3: . Deactivate your conda environment after you are done by running
How to Import anaconda environments.
To import anaconda environments, you can do it from anaconda navigator
Step 1: Open anaconda navigator and click on environments
Step 2: Click on import located on the bottom of the screen.
Step 3: Give the environment a new name,
- Click the folder icon and select the environment.yml file you exported in the last section
- Then click import
- Wait a few minutes and the environment will be imported along with it’s dependencies.
Go here for extra resources on anaconda virtual environments https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html