Sitemap
Python Environments

This publication aims to evaluate the best way to setup a Python environment depending on the goals of your specific project. This way, you can spend less time setting up your environment and more time creating value.

Python Environments

Managing Python Environments With Anaconda: Part 1

A Comprehensive Starters Guide To Anaconda

8 min readMay 15, 2022

--

Source: https://www.anaconda.com/

Introduction

As you work on more advanced Python projects, you will eventually need to install third party packages. These packages are collections of .py files that contain predefined functions and classes that can make your life a whole lot easier when writing Python code. One way to organize these packages is through the use of “environments”. Managing Python environments can be one of the most difficult concepts for a beginner to grasp. If you’re like me, then you probably just pip-installed every package into your base environment. This may work just fine for a while, but eventually you are bound to run into issues.

This article is part one of a series dedicated to explaining different methodologies for managing Python environments. The topic of this writing will be the Anaconda Distribution, the world’s most popular open-source Python distribution platform.

What is a Python Environment?

At the simplest level, a Python environment is a folder located on your machine that contains everything you will need to run your Python scripts. Typically, this includes a Python interpreter, and any third party packages that are to be used within the environment.

Press enter or click to view image in full size
Multiple Python Environments (dataquest.io)

As you can see in the picture above, each environment may have a specific installation of Python, and also different versions of each package. This is especially useful when working on multiple projects on the same machine, that must each utilize different versions of the same packages. Environments can also facilitate sharing of scripts with co-workers. Instead of having to install each and every package to run your code, a colleague can just import the entire environment and run your script with ease. (Refer to part two for a specific example)

The Anaconda Distribution

Anaconda is marketed as “the easiest way to perform Python/R data science and machine learning on a single machine”.

The Anaconda distribution is composed of three main components.

1. Anaconda Repository: Collection of commonly used Python/R packages. Similar to the PyPI Index.

“…[The Anaconda repository] features over 8,000 open-source data science and machine learning packages, Anaconda-built and compiled for all major operating systems and architectures.”

Anaconda Repository (https://www.anaconda.com/)

2. Conda: Command line environment manager compatible with all major operating systems.

“Conda is an open-source package and environment management system that runs on Windows, macOS, and Linux. Conda quickly installs, runs, and updates packages and their dependencies. It also easily creates, saves, loads, and switches between environments on your local computer. It was created for Python programs, but it can package and distribute software for any language.”

3: Anaconda Navigator: Desktop GUI to manage environments without command line.

Anaconda Navigator (https://www.anaconda.com/)

Anaconda Installation

The download process for Anaconda is fairy simple, and Anaconda maintains excellent documentation so click here to access the detailed guide based on your operating system. Once installed, navigate to your Anaconda 3 folder and explore its contents. Understanding how the Anaconda directories are stored will help drastically in conceptualizing how Python environments work.

A Quick Note On pip

By default, each new Anaconda environment that includes Python will also come with pip pre-installed. Its documentation sums up the capabilities elegantly.

“pip is the package installer for Python. You can use it to install packages from the Python Package Index and other indexes.”

Pip truly is one of my favorite Python package installers. It works great in conjunction with Anaconda, and can allow you to install packages that may not be available on the Anaconda repository. One thing to note, however is that working with pip and Conda in conjunction can lead to potential issues. The rule of thumb is Conda before pip.

Issues may arise when using pip and conda together. When combining conda and pip, it is best to use an isolated conda environment. Only after conda has been used to install as many packages as possible should pip be used to install any remaining software.

Using Anaconda To Manage A Python Environment

For this tutorial, I am going to be using Conda for all of my environment management needs. If you can grasp how to use Conda from the command line, then utilizing the Anaconda Navigator GUI will be a breeze. I use Conda exclusively for environment management because it is fast and simple. (And I enjoy command line programming because it reminds me of the matrix 😎)

Setting Up Conda

Depending on what OS you are operating on, there may be different ways of setting up Conda for use. For this example, I will go over how to set up on Windows, but I will provide a resource for MacOS here. On Windows, Anaconda is nice enough to come pre-installed with Anaconda Prompt, which is an isolated command line dedicated to running Conda commands. If you still prefer to run Conda from the standard Windows command line, you must first add it to your environment path.

  1. Type “env” into the Windows search and click the first result
Press enter or click to view image in full size

2. Navigate to “advanced” from the tab, and click “Environment Variables…”

3. Click on your “Path” variable and then edit

4. Add new path to the condabin folder within your anaconda3 directory. (“wherever_you_installed\anaconda3\condabin”)

5. Congrats! You should now be able to issue Conda commands from the Windows command line. Test it out by typing “Conda env list” in a new cmd window. You should see your base environment.

Conda Commands

Note: All of these commands will work by default on Anaconda prompt. In order to use in command prompt/terminal, be sure to first enable Conda for your specific operating system.

Get Eric Flynn’s stories in your inbox

Join Medium for free to get updates from this writer.

There are very few Conda commands that you will use on a day to day basis. Here I will show you the 5 top commands and give a brief explanation.

conda activate "env"

This is the most basic of all Conda commands, and it is used to activate a given environment. For this example, since we haven’t created any environments yet, I will activate “base”. Base environment is the standard environment that comes with Anaconda upon installation. As you can see, once activated you will see (base) next to your curser. For those using Anaconda prompt, you will start in your base environment by default so you can skip this one.

Press enter or click to view image in full size
conda env list

“conda env list” is used to show all the current Anaconda environments you have installed on your machine. This can be utilized without first activating an environment. As you can see, I have many different Conda environments. This is because I chose to isolate each project to an environment. If you haven’t yet created any Conda environments, then you will only see your base environment here. On the right hand side, you can see the path to each environment directory.

Press enter or click to view image in full size
conda create -n "Name_Of_Env" "Package"="Version"

The “conda create” is used to create a brand new Conda environment for use. You will just have to input the desired name of the environment and any packages that you would like to have installed upon initial setup. For this example, we will create a new environment “myenv” with python 3.10 installed on it. By typing “y” + enter you will initiate the installation. Once the download is open, try running “conda env list” again to see it now added into your environment list.

Note: to add multiple packages to initial installation, just separate them with spaces and follow the same “package=version” format.

Press enter or click to view image in full size
Press enter or click to view image in full size
conda list

“conda list” is similar to “conda env list” however it is used to list out each individual package you have installed in a given environment. For this command, you must first activate your environment of choice.

Press enter or click to view image in full size
conda remove --name "Name_Of_Env" --all

“conda remove” is pretty self explanatory and allows you to delete a Conda environment. Make sure to deactivate any open environments before running this command.

Press enter or click to view image in full size

Installing Packages

Installing packages into an Anaconda environment is extremely easy. First, we will launch Python from our previously created environment and attempt to import pandas. In order to launch Python, you must first change your directory to the location of where your Python interpreter is saved. Remember, Python environments are just folders that are stored locally on your machine. We must tell our computer which version of Python we want to load. To do this, we will cd (change directory) to the Python installation within our “myenv” environment folder. Recall from before, if you type “conda env list” you can see the location where each of your Conda environments are stored on your local machine. Locate your “myenv” folder, and navigate to it using cd. (Shown below). Then type “python” and you will see the curser change to >>> . This means you are now operating in Python.

Press enter or click to view image in full size

If we try importing pandas at this point, we will get an error message because we haven’t yet downloaded the pandas package into “myenv”

To install pandas, open another cmd window and activate “myenv”. Once activated, we can either pip/conda install pandas. In this example, I used pip. The only difference is the location of the repository from which you download the package. (Either PyPi or Conda repository)

Press enter or click to view image in full size

As you can see, when we go back into Python and try to import pandas we now have no issue. We can even call on a pandas function to verify that it worked properly.

Conclusion

In conclusion, this article has explained the basics of what a Python environment is and why every Python programmer should be utilizing them. The Anaconda Distribution is an industry leader in Python environment management due to its simplicity, open-source nature, and extensive documentation. Conda is Anaconda’s command line environment manager. This article reviewed how to get started using Conda and then showed the top five Conda commands. These commands should be more than enough to get started and begin experimenting on your own with Anaconda environments. Thank you for reading and I hope that you now have a better understanding of Python environments and Anaconda! 👋

References

https://www.anaconda.com/

https://docs.conda.io/en/latest/

https://pypi.org/

https://pip.pypa.io/en/stable/

https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/

--

--

Python Environments
Python Environments

Published in Python Environments

This publication aims to evaluate the best way to setup a Python environment depending on the goals of your specific project. This way, you can spend less time setting up your environment and more time creating value.