Installing Python and Setting Up Your Environment

A comprehensive guide to installing Python 3, setting up virtual environments with venv, and configuring VS Code for Python development across Windows, macOS, and Linux platforms.

Installing Python and Setting Up Your Environment

Getting started with Python for network automation begins with setting up a proper development environment. Whether you're on Windows, macOS, or Linux, having the right tools configured will save you countless hours and frustrations down the road. Let's walk through installing Python 3, setting up virtual environments, and configuring VS Code for Python development.

Installing Python 3

First, let's install Python on your operating system. Python comes in two main versions: Python 2 (legacy) and Python 3 (current). Always choose Python 3 for new projects, as Python 2 reached end-of-life in 2020.

Windows Installation

Download Python from the official website at python.org. During installation, make sure to check "Add Python to PATH" - this is crucial for running Python from the command line. After installation, verify it worked by opening Command Prompt and typing:

python --version
pip --version

macOS Installation

While macOS comes with Python pre-installed, it's often an older version. Install the latest Python 3 from python.org or use Homebrew:

brew install python3

Verify the installation with:

python3 --version
pip3 --version

Linux Installation

Most Linux distributions include Python 3. If not, install it using your package manager:

# Ubuntu/Debian
sudo apt update
sudo apt install python3 python3-pip

# CentOS/RHEL
sudo yum install python3 python3-pip

Understanding pip

pip is Python's package installer, and it comes bundled with Python 3.4+. Think of pip as your gateway to thousands of Python libraries. You'll use it constantly to install network automation libraries like netmiko, napalm, and requests.

Test pip by installing a simple package:

pip install requests

Creating Virtual Environments

Here's where many beginners make a critical mistake: installing everything globally. Virtual environments solve this by creating isolated Python environments for each project. This prevents version conflicts and keeps your system clean.

Create a virtual environment using Python's built-in venv module:

# Create a virtual environment
python -m venv network_automation

# Activate it (Windows)
network_automation\Scripts\activate

# Activate it (macOS/Linux)
source network_automation/bin/activate

When activated, your command prompt will show the environment name in parentheses: (network_automation). Now any packages you install with pip will only affect this environment.

To deactivate the virtual environment:

deactivate

Setting Up VS Code for Python

Visual Studio Code is an excellent free editor for Python development. Download it from code.visualstudio.com and install the Python extension by Microsoft - it's the most popular Python extension with millions of downloads.

After installing VS Code and the Python extension:

  1. Open VS Code and press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
  2. Type "Python: Select Interpreter"
  3. Choose the Python interpreter from your virtual environment

The extension provides syntax highlighting, debugging, IntelliSense code completion, and integrated terminal support. When you create a new Python file (.py), VS Code automatically recognizes it and enables Python features.

Testing Your Setup

Create a simple test script to verify everything works:

# test_setup.py
import sys
print(f"Python version: {sys.version}")
print("Setup successful!")

# Test importing a common networking library
try:
    import requests
    print("Requests library available")
except ImportError:
    print("Install requests: pip install requests")

Run this script in VS Code or from the terminal to confirm your environment is working correctly.

What's Next

Now that you have Python installed and your development environment configured, you're ready to start learning Python fundamentals. In the next post, we'll cover Python basics specifically relevant to network engineers: variables, data types, and the control structures you'll use most in network automation scripts.