You might have heard a lot about creating a virtual environment as a recommended approach for installing a package. Python applications will usually employ packages and modules that are developed by third parties and do not come with Python itself. Applications may need a specific version of a package for a variety of reasons. This is troublesome as one Python installation cannot meet the obligations of every application since we cannot install all versions of any package in one environment that we can call the panacea!
An example is working with two applications A and B. One requires TensorFlow version 2.1 and the other one requires TensorFlow 1.9. So that tells us we need to separate environment one has TensorFlow 2.1 and the other one embraces TensorFlow 1.9.
The solution for this predicament is to create and utilize a virtual environment, an independent directory tree that includes a Python installation for a special version of Python in addition to some packages with controlled versioning.
In this tutorial you will learn:
To start working with any package, we should first install it in a Python environment. I recommend you to use Anaconda to make life easier for you. It has a lot of advantages, especially in data science.
- Anaconda (1) provides a great selection of packages. It is an environment handler, which provides the ability to build various python environments, independent of each other and (2) is a python configuration, with a comprehensive package management tools.
- Conda is the heart of Anaconda. It is the Anaconda’s package manager employed for keeping anaconda and its libraries up to date. Conda is also a cross-platform package that provides installing, administering, and updating different packages and their dependencies.
Create a Virtual Environment
There are a variety of ways to create a virtual environment. I recommend using the ”conda” to create a virtual environment as it is very simple and fast. Let’s get started. At first, we check if the conda is available!
Then, we are going to install the virtual environment as below:
conda create -n datascience python=3.7 anaconda
- We set the name of the virtual environment to be “datascience”. It is arbitrary. You can name your virtual environment whatever you want.
- We set the Python version to 3.7 by command python=3.7. You can pick whatever version by python=x.x as part of the above line of command.
Activate the Virtual Environment
The next step is to switch the Python environment from the system to the virtual environment by activating the virtual environment:
conda activate datascience
Install a Package
Let’s assume we want to install Pandas package. It can be done with the simple following pip command.
pip install pandas
Deactivate the Environment
After you are done and want to get back to the main system environment, you can deactivate the the virtual environment”
Check the following video that walks you through the steps:
Alternative Solution: Use Google Colaboratory
Google Colaboratory is a free platform (environment might be a better word though!) for programmers to do coding! Beyond that, it is in essence developed to facilitate the Machine Learning and Deep Learning research by providing free GPU resources! In Google Colab:
- You have access to a free GPU with limited runtime!
- You write your code in a nice ready-to-use notebook.
- Installing new packages is very easy!
Google Colab basically provide descent computation resources for whoever around the world that (1) desire to do Machine Learning and (2) have a Gmail account!
In case, you want to just learn and do not want to work on comprehensive projects, Google Colaboratory is a great option to start with.
To learn to about Google Colab, you can use the following link to learn more:
In this tutorial, you learn what are Anaconda and Conda. You also learned the concept of a virtual environment, how it can help us and how to setup one. The story does not end here of course. There are various ways to create and utilize a virtual environments. I pick Conda as it is very common in the data science work and it is very simple. I hope you enjoyed this content. Feel free to share your thoughts by commenting below.