Skip to main content
TensorFlow Introduction
CHAPTER 02 Intermediate

Setting Up Python and TensorFlow Environment

Updated: May 16, 2026
6 min read

# CHAPTER 2

Setting Up Python and TensorFlow Environment

1. Introduction

Deep Learning requires a specialized toolkit. Before we can stack neural network layers, we need to ensure our computer is equipped with Python, the TensorFlow library, and a suitable Integrated Development Environment (IDE). Because TensorFlow relies heavily on underlying C++ and hardware optimizations (like GPUs), installing it correctly is crucial to avoid frustrating error messages. In this chapter, we will walk through a bulletproof setup process for your Deep Learning environment.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Install a compatible version of Python.
  • Create and manage Python Virtual Environments.
  • Install TensorFlow using pip.
  • Set up Visual Studio Code (VS Code) and Jupyter Notebooks.
  • Understand the basics of GPU acceleration (CUDA).

3. Installing Python

TensorFlow requires a 64-bit version of Python. *Warning: Do not install the very latest version of Python the day it comes out, as TensorFlow usually takes a few months to release a compatible update. Stick to stable, widely supported versions like Python 3.9, 3.10, or 3.11.*

Windows:

  1. 1. Download the installer from python.org/downloads.
  1. 2. CRITICAL: Check the box "Add Python to PATH" before clicking Install.

macOS:

  1. 1. Open your terminal and install Homebrew if you don't have it.
  1. 2. Run: brew install python

Linux (Ubuntu): Open terminal and run: sudo apt update sudo apt install python3 python3-pip python3-venv

4. Virtual Environments

Never install TensorFlow globally on your computer. It requires specific versions of NumPy and other dependencies that will break your other Python projects. We will use a Virtual Environment.
  1. 1. Open your terminal/command prompt.
  1. 2. Create a folder for this course: mkdir tfcourse and enter it: cd tfcourse
  1. 3. Create the virtual environment (named tfenv):
  • Windows: python -m venv tfenv
  • Mac/Linux: python3 -m venv tfenv
  1. 4. Activate it:
  • Windows Command Prompt: tfenv\Scripts\activate
  • Mac/Linux: source tfenv/bin/activate

5. Installing TensorFlow

With your virtual environment activated, installing TensorFlow is a single command. *(Make sure your terminal prompt shows (tf
env)).*

Run:

bash
12
pip install --upgrade pip
pip install tensorflow jupyter pandas matplotlib

*Note: This downloads a large package (often >500MB), so it may take a few minutes.*

6. VS Code and Jupyter Notebook Setup

Data Scientists write Deep Learning code in Jupyter Notebooks, which allow you to run code in blocks and see visualizations instantly.
  1. 1. Download Visual Studio Code from code.visualstudio.com.
  1. 2. Open VS Code, go to the Extensions tab (squares on the left).
  1. 3. Install the Python extension and the Jupyter extension.
  1. 4. Open your tfcourse folder in VS Code.
  1. 5. Create a file named hellotf.ipynb.
  1. 6. Open the file, click "Select Kernel" in the top right, and choose your tf_env environment.

7. Step-by-Step Implementation: Verify Installation

Let's ensure TensorFlow is installed correctly. Type this into the first cell of your Notebook and click Play:
python
12345678910
import tensorflow as tf

print(f"TensorFlow Version: {tf.__version__}")

# Check if TensorFlow can perform basic math
tensor_a = tf.constant(5)
tensor_b = tf.constant(10)
result = tf.add(tensor_a, tensor_b)

print(f"5 + 10 = {result.numpy()}")

*If this prints the version number and "15", congratulations, you are ready to build neural networks!*

8. GPU Setup Basics (Advanced)

Training deep neural networks on a CPU can take days. Training them on a Graphics Processing Unit (GPU) takes hours or minutes.
  • NVIDIA GPUs: TensorFlow is heavily optimized for NVIDIA graphics cards via a software layer called CUDA. Setting up CUDA locally can be an incredibly frustrating process involving downloading specific driver versions.
  • Mac GPUs (Apple Silicon): If you have an M1/M2/M3 Mac, you can install the tensorflow-metal plugin to use your Mac's GPU.
  • The Ultimate Hack (Google Colab): If you don't have a powerful GPU or don't want to deal with installation headaches, use Google Colab (colab.research.google.com). It is a free Jupyter Notebook in the browser that gives you free access to powerful cloud GPUs. No installation required!

9. Common Mistakes

  • Installing 32-bit Python: TensorFlow will not install on 32-bit Python. It will throw a Could not find a version that satisfies the requirement tensorflow error. Ensure you installed 64-bit Python.
  • Naming your file tensorflow.py: If you create a test file named tensorflow.py and run import tensorflow, Python will try to import your file instead of the actual library, causing a massive crash. Never name your files the same name as a library.

10. Best Practices

  • Freeze your requirements: Once you have a working environment, run pip freeze > requirements.txt. This saves the exact versions of TensorFlow and NumPy you are using, so you can perfectly recreate the environment if your computer crashes.

11. Exercises

  1. 1. Follow the steps to create a virtual environment, activate it, and install TensorFlow.
  1. 2. Write a Python script to check if TensorFlow detects a GPU on your system using tf.config.listphysicaldevices('GPU').

12. MCQ Quiz with Answers

Question 1

Why is it important to install TensorFlow inside a Virtual Environment?

Question 2

What is the easiest way to access free GPU computing power without installing complex CUDA drivers on your local machine?

13. Interview Questions

  • Q: What is the purpose of the CUDA toolkit in the context of Deep Learning?
  • Q: Explain the typical software stack required to write and execute TensorFlow code locally.

14. FAQs

Q: I have an AMD graphics card. Can I use it with TensorFlow? A: Historically, TensorFlow only supported NVIDIA (CUDA). Recently, AMD support has improved via a project called ROCm, but it is notoriously difficult to set up on Windows. If you have an AMD card, using Google Colab is highly recommended.

15. Summary

A stable environment is the foundation of data science. By utilizing Python virtual environments, installing TensorFlow via pip, and setting up an interactive Jupyter Notebook in VS Code, we have created a professional workspace. For those requiring massive computational power, understanding GPU acceleration or leveraging cloud tools like Google Colab bridges the gap to professional deep learning.

16. Next Chapter Recommendation

Before we dive into the complex mathematics of Neural Networks, we must ensure our foundational coding skills are sharp. In Chapter 3: Python Basics for Deep Learning, we will review the exact Python syntax and data structures you need to succeed in this course.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·