Virtual Environment Quick Start

Sergey Mouzykin
Mar 30 · 5 min read

Virtual environment quick start guide with Anaconda.

Image for post
Image for post
https://www.anaconda.com/distribution/

Introduction

For example, when you start a new project for Data Analysis and Machine Learning most likely you’ll use packages such as Pandas, NumPy and Scikit-Learn. Each one comes with its own dependencies and it may be possible that you’ll encounter conflicts between some packages.

A virtual environment allows you to install only the packages that you need and nothing you don’t. It’s kind of like having a desk with only the book, notebook, and a pen that you need. Nothing extra and no distractions.

Manage Environments

You can launch the Navigator and select the ‘Environments’ tab on the left. Then, select base (root) and press play.

Image for post
Image for post
Upper left corner in the Anaconda Navigator.

Alternatively, you can search your system for the Anaconda Prompt as shown below.

Image for post
Image for post
Anaconda Powershell Prompt

Once opened, we can begin. It automatically starts in your “base” environment.

Image for post
Image for post

New Environment

conda create --name <VENV-NAME>

Additionally, you may want to specify what to install.

conda create --name <VENV-NAME> python pandas numpy scikit-learn

The above commands will download and install the latest version of all specified packages and their dependencies.

Activate Environment

conda activate <VENV-NAME>

Once you have activated your environment, you can install additional packages or IDEs like Jupyter Lab, Jupyter Notebook, Spyder, etc. Once your IDE is installed you can launch it from the command line as well.

For example, you can install Spyder IDE (if you haven’t yet):

conda install spyder=4.0.0

After it’s installed, you can launch with a simple command:

spyder
Image for post
Image for post
Activate virtual environment and launch IDE.

By using the command line instead of the Anaconda Navigator you actually save some of your precious RAM.

New Environment from File

Image for post
Image for post
Example YML file for a new virtual environment.

Create your yml file in any text editor and save it with the .yml extension, then you can use it to create a new virtual environment. Now, let’s create a new environment using the YML file. Note, you need to navigate into the same directory as your YML file.

This will create a new environment with the chosen name and specified packages will be installed. From your command line:

conda env create -f new-env.yml
Image for post
Image for post
New environment from YML

Your virtual environment is now created and the specified packages installed. Now you just have to activate it.

Export Environment

conda env export > environment.yml
Image for post
Image for post
Exported environment to YML.

This will create a new YML file with all installed dependencies listed and their versions. This is very useful if/when, at some point in the future, you find that some new package that you installed is not be compatible with some of the currently installed packages. You export environment file serves as a backup and enables you to essentially restore a working virtual environment with no issues.

Conclusion

Thank you for reading.

Below are some useful links to further your knowledge.

References:

The Startup

Medium's largest active publication, followed by +732K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store