How to Create a Tensorflow Environment in Jupyter Notebook and Use it?

Priyanka Dandale
5 min readAug 25, 2021

Anaconda will enable you to create virtual environments and installl packages needed for data science. With virtual environments, you can install specific package versions for a particular project or a tutorial without worrying about version conflicts.

First, download Anaconda for your system and choose your required Python version.

On downloading and installing Anaconda, you get Python, Jupyter Notebook, conda, and hundreds of other open-source packages.

Conda is an open-source package management system and environment management system that runs on Windows, macOS, and Linux. Conda quickly installs, runs, and updates packages and their dependencies. Conda 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.

Conda as a package manager helps you find and install packages. If you need a package that requires a different version of Python, you do not need to switch to a different environment manager, because conda is also an environment manager.

With just a few commands, you can set up a totally separate environment to run that different version of Python, while continuing to run your usual version of Python in your normal environment.

Here are some helpful commands using conda:

# update conda in your default environment 
$ conda upgrade conda
$ conda upgrade --all
# create a new environment with conda
$ conda create -n [my-env-name]
$ conda create -n [my-env-name] python=[python-version]
# activate the environment you created
$ source activate [my-env-name]
# take a look at the environment you created
$ conda info
$ conda list
# install a package with conda and verify it's installed
$ conda install numpy
$ conda list
# take a look at the list of environments you currently have
$ conda info -e
# remove an environment
$ conda env remove --name [my-env-name]

Creating virtual environments with Conda

Let us see how to create a Tensorflow virtual environment using conda:

Step1: Open up your Anaconda Prompt command line, bash, or shell terminal.

Then type the following command to create an environment.

conda create -n tensorflow-environment pip python=3.8.8

The command above tells conda to create a new environment named “tensorflow-environment” using the latest version 3.8.8 of python.

It will ask you to proceed with the installation of the new packages or not: once you select ‘y’ for yes, it will download the required packages for you!

Step2: For us to be able to use our newly created environments we need to activate them on the command line.

The command for activating the environment goes like given in your console:

conda activate tensorflow-environment

Once your environment is activated, your prompt changes immediately, looking like this:

Now you are inside your newly created tensorflow-environment!!

Step3: After you activate it, you can install any package you need in this environment.

For example, I am going to install Tensorflow in this environment. The command to do so:

conda install -c conda-forge tensorflow

Once TensorFlow is installed on your tensorflow-environment, your prompt changes immediately, looking like this:

It will again ask you to proceed with the installation of the new packages or not: once you select ‘y’ for yes, it will download the required packages for you and then be done!!

.

.

Now you have successfully installed Tensorflow. Congratulations!! 😀

Step4: Now comes the step to set this conda environment on your jupyter notebook, to do so you need to install ipykernel.

conda install -c anaconda ipykernel

It will again ask you to proceed with the installation of the new packages or not: once you select ‘y’ for yes, it will download the required packages for you and then be done!!

After installing this, type below command

python -m ipykernel install — user — name tensorflow-environment

Note: Have a look at two hyphens before user and name in the above command. 🤓

Using the above command, you now have the newly created tensorflow environment in your Jupyter notebook.

Step 5: Now check your Jupyter Notebook, to see the shining “tensorflow-environment” on the right.

Click on “tensorflow-environment” under the New →Notebook tab.

Voila!! 😄

Thanks for reading ❤

For any suggestions or queries, leave your comments below and follow for updates.

If you liked the article, please hit the 👏 icon to support it. This will help other Medium users find it. Share it, so that others can read it!

Happy Learning! 😊

--

--

Priyanka Dandale

Senior Unit Manager — BFL, Ex-Accenture, Ex-Infosys, Data Scientist, AI Engineer, MSc. Statistics SPPU.