Installing Tensorflow in a virtual environment in anaconda

Why do we need a virtual or separate environment in python?

Being an open-source platform, python is widely popular not only among developers but also among the scientific community, who have built many cool packages to be used in their respective fields. But this flexibility also creates problems, as the packages are dependent on the version of python on which they are built. So, to work with different packages dependent on different python versions can create a clash if only one version of python or associated packages are used.

Virtual environment solves this clash by creating a separate environment within python. In this environment, we can use our desired package of desired versions, and it won't change the settings of the root environment. So, as long as we are in a virtual environment, we can experiment with various packages without altering the root environment settings.

It is better to install TensorFlow in a separate environment in anaconda

Tensorflow is a useful package for analyzing data with neural network. But proper operation or installation of tensorflow is highly dependent on the version of python, pip etc. Again, anaconda pip install often fails to fetch the proper tensor flow version. So, a customized environment containing necessary packages can be helpful, specially if the windows version or the python version is not updated.

Tensorflow environment types



Tensorflow comes with two versions for windows, one is cpu dependent,and another is gpu dependent. If you have a graphics card and required specifications (you can find them in Tensorflow website), you can work with gpu based tensorflow. Or, if you don't have the required specifications like me, you should go for the cpu based TensorFlow. Here I will explain the later process.

Working with CPU based tensorflow with a virtual environment:

Step 1: Create a virtual environment

Go to anaconda prompt.
Type conda create --environment name

As I want to work with tensorflow here, I can install it while creating it like below
For example, I have created an environment named tf like below:

conda create --tf tensorflow
By this, an environment named tf containing TensorFlow will be created.

By default, after the creation of the environment, the environment will be deactivated. If we want to access the environment, following command is necessary:
conda activate tf
By this the tf environment will be activated.

Step 2: Install the required version of python and pip

We will need pip for further package installation, so it is wise to install it in the first place.
However, you can encounter problems, basically for version mismatch. So, it is wise to install the desired version of python first. For example, my tensorflow requires python 3.8, so I downgraded my python version like below:
conda install python=3.8
After that, install pip
conda install pip
So, now you are good to go.

Step 3: Install tensorflow

Now you can install TensorFlow  like this:
pip install tensorflow
Step 4: Working with jupyter notebook

Now that we have installed tensorflow, we need a compiler to compile. I personally prefer jupyter notebook for its versatility, so I will show how to install it inside the environment.
To install jupyter notebook iside the created tf environment, write this command:
pip install ipykernal
Now, you can work with jupyter notebook.

Necessary resources:




Comments