A Guide to Conda Environments in Jupyter, PyCharm and GitHub
How to manage anaconda environments across development platforms: Jupyter Notebook, PyCharm and GitHub.
Anaconda environments are used extensively in industry for Python application development. They allow us to install different versions of Python and/or Python packages for each of our applications. This allows us to isolate the development environment of different applications, avoiding version clashes.
Conda documentation discusses the basics of creating, activating, deactivating, updating, cloning and sharing conda environments. However, this information is specifically for terminal/cmd/bash or Anaconda prompt only. However, while coding with Python, we frequently use tools like Jupyter Notebook or PyCharm. We also work with Github repositories to manage, version control and publish our code. So, the question arises, how to use our anaconda environment consistently across these platforms. This article aims to answer just that question.
Adding Anaconda Environment in Jupyter Notebook
- Install Anaconda Navigator.
- When you launch Anaconda Navigator → Home tab, the Applications on drop down menu lists down all your conda environments (which you have created in Anaconda prompt or Anaconda Navigator → Environment tab).
3. Select your required environment and launch the Jupyter notebook from within the Anaconda Navigator. If Jupyter notebook is not installed in this environment, you will first need to install and then launch. All these actions can be performed within the Navigator GUI.
4. In the Jupyter Notebook, a conda tab will appear where you can see all the conda environments.
5. You can also select the kernel with your chosen conda environment from the Kernel → change kernel option, when you open/create a Jupyter Notebook, as shown below:
Adding Anaconda Environment in PyCharm
- Launch your PyCharm IDE and open your Python Project
- Go to settings at the top right corner of the PyCharm IDE.
- Go to Project:{Your Project Name} → Python Interpreter
- Go to the settings inside here and click add:
5. In Add Python Interpreter select conda env
6. Select existing environment option and click on your required conda environment path from the drop down menu OR add the path of the python.exe file in your conda environment. As a reference, I am adding the path for my windows10 system: C:\Users\maria\AppData\Local\Continuum\anaconda3\envs\<mycondaenv>\python.exe . It can vary for your system based on the installation configuration.
The same settings can be done while creating a new Python Project in PyCharm as shown below:
Publishing Python Project on Github with Anaconda Environment
As discussed earlier, the goal of Anaconda environments is to group and isolate all the Python packages required for a given project. This enables us to share the list of names and version numbers of these Python packages when publishing our project code for other team members, and users. With this list, others can install all the packages required to run our code without a hassle.
To build this list and publish it along with your code on Github, you can use the following two approaches:
Publish a requirements.txt file:
The common standard is to add a requirements.txt file into the git repository. This file lists down the Python version along with names and versions of all the packages required for executing your project. An example requirements.txt will look like this:
To make a requirements.txt file for your project simply install the pipreqs package in conda:
pip install pipreqs
pipreqs /path/to/project
Publish an environment.yml file:
The environment.yml is more commonly used for sharing the conda environment across development teams but it can be added to the git repo as well.
To create environment.yml file, activate the conda environment in Anaconda prompt and then use this command:
conda env export > environment.yml
Closing remarks:
Good luck with your upcoming projects!
If you find this guide useful, hit the clap button down there to encourage me to write more. Your suggestions and critique is as valuable to me as your appreciation.