Module 1.2

Setting Up Python

Get Python installed on your computer and set up a professional development environment with VS Code. You will be ready to write and run Python code in just 15 minutes!

25 min read
Beginner
Installation Guide
What You'll Learn
  • Install Python 3.11+ properly
  • Set up VS Code for Python
  • Create virtual environments
  • Use pip to install packages
  • Run your first Python script
Contents
01

What You Need to Get Started

Before writing Python code, you need two things: the Python interpreter (to run your code) and a code editor (to write your code). We will use the official Python distribution and VS Code, which is the most popular setup among developers.

Good News: This entire setup takes about 15 minutes. Once done, you will have a professional development environment!
Python 3.11+

The Python interpreter with pip package manager included

VS Code

Free, powerful code editor with excellent Python support

Virtual Environment

Isolated workspace to keep your projects organized

pip Package Manager

Install any of the 400,000+ Python packages

02

Installing Python

Follow the instructions for your operating system. We recommend Python 3.11 or 3.12 for the best experience.

Windows Installation

1
Download Python

Visit python.org/downloads and click the big yellow "Download Python 3.x" button

2
Run the Installer

Double-click the downloaded .exe file to start installation

Critical: Check the box "Add Python to PATH" at the bottom of the installer. This is essential!
3
Verify Installation

Open Command Prompt (search "cmd" in Start Menu) and run:

python --version
pip --version

You should see Python 3.11.x or later and pip version

macOS Installation

1
Download Python

Visit python.org/downloads and download the macOS installer (choose Intel or Apple Silicon based on your Mac)

2
Run the Installer

Double-click the .pkg file and follow the installation wizard

3
Verify Installation

Open Terminal and run:

python3 --version
pip3 --version

Note: On macOS, use python3 instead of python

Linux Installation

1
Install via Package Manager

Most Linux distributions come with Python. To install the latest version:

Ubuntu/Debian:

sudo apt update
sudo apt install python3.11 python3-pip python3-venv

Fedora:

sudo dnf install python3.11 python3-pip
2
Verify Installation
python3 --version
pip3 --version
03

Setting Up VS Code

Visual Studio Code is the most popular code editor for Python development. It is free, fast, and has excellent extensions for Python.

1
Download VS Code

Visit code.visualstudio.com and download for your operating system

2
Install Python Extension

Open VS Code and go to Extensions (Ctrl+Shift+X or Cmd+Shift+X)

  • Python (by Microsoft) - Essential Python support
  • Pylance - Fast IntelliSense and auto-completion
  • Python Indent - Fixes Python indentation issues
3
Select Python Interpreter

Press Ctrl+Shift+P (or Cmd+Shift+P), type "Python: Select Interpreter", and choose your Python installation

4
Create Your First File

Create a new file, save it as hello.py, and add:

print("Hello, Python!")

Click the Run button (triangle icon) or press F5 to run it!

Pro Tip: VS Code also supports Jupyter Notebooks! Install the "Jupyter" extension to run .ipynb files directly in VS Code.
04

Virtual Environments

Virtual Environment

An isolated Python environment that keeps project dependencies separate from your system Python installation.

Think of it as a clean room where each project has its own set of tools without affecting others.

Virtual environments are essential for Python development. They prevent package conflicts and keep your projects organized. Here is how to create and use them:

1
Create a Project Folder

Open terminal and navigate to where you want your project:

mkdir my_python_project
cd my_python_project
2
Create Virtual Environment

Windows:

python -m venv venv

macOS/Linux:

python3 -m venv venv

This creates a venv folder with a clean Python environment

3
Activate Virtual Environment

Windows (Command Prompt):

venv\Scripts\activate

Windows (PowerShell):

venv\Scripts\Activate.ps1

macOS/Linux:

source venv/bin/activate

You should see (venv) appear in your terminal prompt

4
Install Packages

With your virtual environment activated, install packages using pip:

pip install requests
pip install numpy pandas matplotlib
5
Deactivate When Done

To exit the virtual environment:

deactivate
Best Practice: Always create a virtual environment for each new project. This keeps dependencies isolated and makes sharing your project easier.
05

Running Your First Script

Let us verify everything is working by creating and running a Python script. We will test both basic Python and an installed package.

Test 1: Basic Python

Create a file called test_setup.py with this code:

import sys

print("Python Setup Test")
print("=" * 40)
print(f"Python version: {sys.version}")
print(f"Python path: {sys.executable}")

# Test basic operations
name = input("Enter your name: ")
print(f"Hello, {name}! Your Python setup is working!")

# Test a simple calculation
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
average = total / len(numbers)
print(f"Sum of {numbers} = {total}")
print(f"Average = {average}")

Run it from the terminal:

python test_setup.py

Test 2: Using pip Packages

First, install the requests library:

pip install requests

Then create test_requests.py:

import requests

# Fetch data from a public API
response = requests.get("https://api.github.com")

print("Testing requests library...")
print(f"Status code: {response.status_code}")
print(f"GitHub API says: Connection successful!")

# Show some response headers
print(f"Content-Type: {response.headers['content-type']}")

If you see "Connection successful!", your pip installation works perfectly!

Congratulations! Your Python environment is fully set up. You are ready to start coding!
06

Common Issues and Solutions

Problem: "python" not recognized

Solution: Python was not added to your system PATH during installation.

  • Windows: Reinstall Python and check "Add Python to PATH" option, or manually add it to Environment Variables
  • macOS/Linux: Use python3 instead of python
Problem: Virtual environment won't activate

Solution: Different activation methods depending on your shell:

  • Windows Command Prompt: venv\Scripts\activate.bat
  • Windows PowerShell: venv\Scripts\Activate.ps1 (may need to enable scripts first)
  • macOS/Linux: source venv/bin/activate

If PowerShell gives an error, run: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Problem: "ModuleNotFoundError" when importing libraries

Solution: Either the library is not installed, or you are not in your virtual environment:

# 1. Make sure venv is activated (you should see (venv) in prompt)
source venv/bin/activate  # or venv\Scripts\activate on Windows

# 2. Install the missing library
pip install library-name

# 3. Verify it is installed
pip list | grep library-name
Problem: VS Code can't find Python interpreter

Solution:

  1. Open Command Palette (Ctrl+Shift+P or Cmd+Shift+P)
  2. Type "Python: Select Interpreter"
  3. Look for your virtual environment path (e.g., ./venv/bin/python)
  4. If not listed, click "Enter interpreter path" and browse to venv/bin/python (or venv\Scripts\python.exe on Windows)
Still stuck? Check the Python Setup Documentation or ask on Stack Overflow.

Key Takeaways

Python 3.11+

Download from python.org and always check "Add to PATH" during installation

VS Code + Extensions

Install Python and Pylance extensions for the best development experience

Virtual Environments

Always use venv to isolate project dependencies and avoid conflicts

pip Package Manager

Use pip install to add any of 400,000+ packages from PyPI

Command Line Basics

Learn to navigate, activate venv, and run Python scripts from the terminal

Test Your Setup

Always verify your installation by running test scripts before starting a project

Knowledge Check

Test your understanding of Python setup concepts:

Question 1 of 6

Why is it important to check "Add Python to PATH" during installation?

Question 2 of 6

What is the purpose of a virtual environment in Python?

Question 3 of 6

Which command creates a new virtual environment?

Question 4 of 6

How do you activate a virtual environment on Windows (Command Prompt)?

Question 5 of 6

Which command installs a Python package using pip?

Question 6 of 6

What VS Code extension is essential for Python development?

Answer all questions to check your score