> "Give me six hours to chop down a tree and I will spend the first four sharpening the axe."
In This Chapter
- Why Setup Matters More Than You Think
- 2.1 Understanding What You're Installing
- 2.2 Installing Python
- 2.3 Choosing Your Code Editor
- 2.4 Setting Up Your First Virtual Environment
- 2.5 Your First Python Programs
- 2.6 Understanding File and Folder Organization
- 2.7 Troubleshooting Common Setup Problems
- 2.8 The Anaconda Path (Alternative Setup)
- 2.9 Installing the Book's Packages
- 2.10 VS Code Configuration for Python
- 2.11 A Brief Tour of Python Files and Modules
- 2.12 What Priya and Maya Are Setting Up
- Summary
- Command Reference Card
Chapter 2: Setting Up Your Python Environment
"Give me six hours to chop down a tree and I will spend the first four sharpening the axe." — Often attributed to Abraham Lincoln
Why Setup Matters More Than You Think
This chapter is about installation and configuration. It may feel like administrative overhead before the real learning starts. It isn't.
Your Python environment is the workshop where every skill in this book gets built. A good setup means your tools work reliably, your projects stay organized, your code runs the same way tomorrow as it does today, and sharing your work with others is straightforward. A poor setup means hours lost to mysterious errors, conflicting package versions, "it works on my machine" problems, and the creeping anxiety that you don't fully understand why your code runs.
The 30–60 minutes you invest in this chapter pays dividends for every hour of Python work that follows.
2.1 Understanding What You're Installing
Before touching an installer, let's understand what we're actually setting up. There are several distinct components, and confusing them causes genuine problems.
Python Itself
Python is the programming language — more precisely, it's the interpreter: the program that reads your Python code and executes it. When you run python my_script.py, you're invoking the Python interpreter.
Python is versioned. This book uses Python 3.10 or higher. (If you've encountered Python 2 anywhere, ignore it — Python 2 reached end-of-life in 2020 and is not used in new work.) At the time of writing, Python 3.12 is the current stable release and is an excellent choice.
When you install Python, you get:
- The Python interpreter itself
- The Python standard library — a large collection of built-in modules covering file handling, math, dates, networking, and much more
- pip, the Python package installer
pip
pip is Python's package installer. When you want to use a third-party library (like pandas), you install it with pip:
pip install pandas
pip downloads the library from the Python Package Index (PyPI — pronounced "pie-pee-eye"), which hosts over 450,000 packages. Almost everything you'll need in this book is on PyPI.
Virtual Environments
A virtual environment is an isolated Python installation for a specific project. This concept is critical and often skipped by beginners, so let's explain it clearly.
Imagine you're working on two Python projects:
- Project A uses version 1.5 of a library called data-tool
- Project B uses version 2.0 of the same library (with breaking changes)
Without virtual environments, installing version 2.0 for Project B would break Project A. With virtual environments, each project has its own isolated set of libraries. They don't interfere with each other.
Best practice: always create a virtual environment for every Python project. It takes 30 seconds and prevents an entire class of problems.
Anaconda (Alternative to pip + venv)
Anaconda is an alternative Python distribution primarily aimed at data science. Instead of managing Python, pip, and virtual environments separately, Anaconda bundles them with a different package manager called conda.
Anaconda installs with many data science packages pre-included (pandas, numpy, matplotlib, scipy, etc.), which makes initial setup faster. The downside: Anaconda is large (several gigabytes) and the conda package manager can be slower and more complex than pip.
Recommendation: Unless you have a specific reason to use Anaconda, install plain Python from python.org and use pip + venv. It's lighter, faster, and more representative of how Python is used in business and production environments. We'll note when a conda alternative exists, but the primary instructions in this book use pip.
2.2 Installing Python
Windows Installation
Step 1: Download Python
Go to python.org and click "Downloads." The site will detect your Windows version and offer the current stable release. Download the installer (.exe file).
Warning
Do not install Python from the Microsoft Store. The Store version has restrictions that will cause problems with some packages. Always install from python.org.
Step 2: Run the Installer
When you launch the installer, you'll see a critical option at the bottom of the first screen: "Add Python to PATH."
Check this box. If you don't, you'll have to add Python to your system PATH manually, which is doable but annoying.
Then click "Install Now" (for a standard install) or "Customize Installation" if you know what you're doing.
Step 3: Verify the Installation
Open a Command Prompt (search for "cmd" in the Start menu) and type:
python --version
You should see something like Python 3.12.0. If you see Python 2.x.x, there's an older Python installation in your PATH — see the troubleshooting section (2.7).
Also verify pip:
pip --version
You should see something like pip 23.x.x from ....
macOS Installation
macOS comes with Python pre-installed, but it's typically an older version (often Python 3.9 or the even older Python 2.7 on very old systems). Don't use the system Python — it belongs to macOS and you shouldn't modify it.
Option 1: Install from python.org (Recommended for Beginners)
Download the macOS installer from python.org and run it. The macOS installer is straightforward.
After installation, verify in Terminal:
python3 --version
pip3 --version
Note: on macOS, the commands are typically python3 and pip3 rather than python and pip, to avoid conflicts with the system Python.
Option 2: Install via Homebrew (Recommended for Power Users)
If you have Homebrew installed (a package manager for macOS):
brew install python
Homebrew-installed Python creates python3 and pip3 commands by default.
Option 3: pyenv (Most Flexible)
pyenv is a tool that lets you install and switch between multiple Python versions. It's useful if you'll eventually work across projects with different Python version requirements. Setup is slightly more complex — see pyenv's documentation for instructions.
Linux Installation
Most Linux distributions come with Python 3 pre-installed. Check your version:
python3 --version
If you need a newer version, the approach varies by distribution. On Debian/Ubuntu:
sudo apt update
sudo apt install python3 python3-pip python3-venv
On Fedora/RHEL:
sudo dnf install python3 python3-pip
Arch Linux:
sudo pacman -S python python-pip
2.3 Choosing Your Code Editor
You need a place to write Python code. The options range from a basic text editor to fully featured integrated development environments (IDEs). Here's the landscape.
VS Code (Strongly Recommended)
Visual Studio Code (VS Code) is a free code editor from Microsoft that has become the most popular choice for Python development across all experience levels. It's not a full IDE, but with extensions it comes close — and it's much faster and lighter than a true IDE.
What you get with VS Code + the Python extension: - Syntax highlighting — Python keywords, strings, and variables are color-coded - IntelliSense — code completion as you type (suggests function names, parameters, etc.) - Inline error detection — problems are underlined before you even run the code - Integrated terminal — run Python directly from within VS Code - Debugger — step through your code line by line to find problems - Variable explorer — see the current value of every variable when debugging - Jupyter Notebook support — run notebooks without a separate browser window
Installation: 1. Download VS Code from code.visualstudio.com 2. Install and open it 3. Click the Extensions icon (puzzle piece on the left sidebar) 4. Search for "Python" and install the Microsoft Python extension 5. The extension will prompt you to select a Python interpreter — choose the one you installed in Section 2.2
PyCharm
PyCharm is a full Python IDE from JetBrains. The Community edition is free; the Professional edition is paid. PyCharm is more feature-rich than VS Code out of the box and particularly strong for large Python projects.
For business professionals learning Python, PyCharm is a reasonable choice but tends to feel heavyweight. VS Code is faster and more versatile. If you're already a JetBrains user (IntelliJ, WebStorm, etc.), PyCharm will feel familiar.
Jupyter Notebook / JupyterLab
Jupyter Notebook is a browser-based environment where code runs in "cells" that you can execute individually, with output (including charts) displayed inline. It's the standard tool for exploratory data analysis — perfect for the kind of interactive work you'll do in Parts 2 and 4 of this book.
Install Jupyter with:
pip install jupyter
Launch it with:
jupyter notebook
This opens a browser window where you can create and run notebooks.
When to use Jupyter vs. VS Code: - Jupyter: Exploratory analysis, building charts, presenting findings interactively, learning - VS Code: Scripts that run unattended (automation), larger programs with multiple files, production code
You'll use both. This book includes examples in both formats.
Thonny (For Absolute Beginners)
Thonny is a Python editor specifically designed for beginners. It's simpler than VS Code and has excellent debugging tools that make the internal workings of Python more visible. If VS Code feels overwhelming in your first week, Thonny is a valid starting point — but plan to graduate to VS Code within a month.
The REPL (Python's Interactive Mode)
The REPL (Read-Evaluate-Print Loop) is Python's built-in interactive mode. You enter it by typing python (or python3) in your terminal without a filename. You get a prompt (>>>) and can type Python expressions that are immediately evaluated and printed:
>>> 2 + 2
4
>>> "Hello, " + "Business"
'Hello, Business'
>>> revenue = 45000
>>> revenue * 1.1
49500.0
The REPL is perfect for: - Quick calculations - Testing a function before using it in a script - Exploring how a library works - Checking the behavior of a specific Python expression
It's not useful for multi-line programs — use VS Code or Jupyter for those.
2.4 Setting Up Your First Virtual Environment
This section covers the virtual environment workflow you'll use for every project in this book. Do this once here; it will become automatic.
Creating the Environment
Navigate to your project folder in the terminal. For a new textbook project:
# Navigate to where you keep your projects
cd Documents/python-projects
# Create a folder for your first project
mkdir ch02-setup && cd ch02-setup
# Create a virtual environment named 'venv'
python -m venv venv
The last command creates a folder called venv inside your project folder. This folder contains an isolated Python installation.
Activating the Environment
Before using the environment, you need to activate it:
Windows (Command Prompt):
venv\Scripts\activate
Windows (PowerShell):
venv\Scripts\Activate.ps1
Note for PowerShell users: You may need to enable script execution first:
powershell Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
macOS / Linux:
source venv/bin/activate
After activation, your terminal prompt will change to show the environment name:
(venv) C:\Documents\python-projects\ch02-setup>
That (venv) prefix means the environment is active. Any packages you install now go into this environment only.
Installing Packages into the Environment
With the environment active:
pip install pandas matplotlib requests
You'll see download progress. After installation:
pip list
Shows all installed packages in this environment.
Deactivating the Environment
When you're done working:
deactivate
The (venv) prefix disappears and you're back to your system Python.
The requirements.txt Workflow
This is the standard way to share package requirements with others (or with your future self):
# Save current packages to requirements.txt
pip freeze > requirements.txt
This creates a file listing every installed package and its version. To recreate the environment on another machine:
pip install -r requirements.txt
Best practice: Create a requirements.txt for every project, and commit it to version control (see Chapter 39).
2.5 Your First Python Programs
Let's run some code. These examples introduce the mechanics of running Python before diving into syntax in Chapter 3.
Method 1: The REPL
Open your terminal, activate your virtual environment, and type python to enter the REPL:
>>> print("Hello from Python!")
Hello from Python!
>>> 1200 * 12
14400
>>> "Acme Corp".upper()
'ACME CORP'
Notice: the REPL immediately shows the result of expressions. 1200 * 12 prints 14400 without a print() call. This is the REPL's main convenience feature.
Exit the REPL with exit() or Ctrl+Z (Windows) / Ctrl+D (macOS/Linux).
Method 2: Running a Python File
Create a file called hello_business.py in your project folder. In VS Code:
1. Click "New File"
2. Name it hello_business.py
3. Enter the following code:
# hello_business.py
# My first Python program
company_name = "Acme Corp"
employees = 200
annual_revenue = 15_400_000 # Underscores in numbers are legal and readable
print(f"Welcome to {company_name}")
print(f"Employee count: {employees}")
print(f"Annual revenue: ${annual_revenue:,.0f}")
print(f"Revenue per employee: ${annual_revenue / employees:,.0f}")
Run it from the terminal:
python hello_business.py
Output:
Welcome to Acme Corp
Employee count: 200
Annual revenue: $15,400,000
Revenue per employee: $77,000
There's a lot happening in those 8 lines that we'll unpack in Chapters 3 and 4. For now, notice:
- # starts a comment (ignored by Python)
- Variables store values (Chapter 3)
- f"..." is an f-string for embedding values in text (Chapter 3)
- :,.0f is a format specifier for numbers (Chapter 3)
Method 3: Jupyter Notebook
With Jupyter installed, launch it:
jupyter notebook
Your browser opens. Create a new notebook (click "New" → "Python 3"). In the first cell, type:
# First Jupyter cell
print("Hello from Jupyter!")
2 + 2
Press Shift+Enter to run the cell. You'll see the print output and the result of 2 + 2 displayed below the cell.
Jupyter notebooks are powerful for analysis — we'll use them extensively in Part 2. For now, just confirm they run.
2.6 Understanding File and Folder Organization
Good organization prevents a surprising amount of frustration. Here's the folder structure we recommend for the projects in this book:
python-projects/
├── ch02-setup/
│ ├── venv/
│ ├── hello_business.py
│ └── requirements.txt
├── ch03-basics/
│ ├── venv/
│ ├── variables_demo.py
│ └── requirements.txt
├── acme-corp-project/ ← Your real business project
│ ├── venv/
│ ├── data/
│ │ └── sales_2024.csv
│ ├── scripts/
│ │ ├── load_data.py
│ │ └── generate_report.py
│ ├── output/
│ └── requirements.txt
Key principles:
- One virtual environment per project (in a venv/ folder inside the project folder)
- Separate folder for data files (keeps code and data distinct)
- Separate folder for output files (reports, generated CSVs, images)
- requirements.txt in every project root
Note for VS Code users: VS Code automatically detects the
venv/folder in your project and activates it when you open a Python file. You may see a notification at the bottom of the screen about selecting a Python interpreter — choose the one in yourvenv/folder.
2.7 Troubleshooting Common Setup Problems
"python is not recognized as an internal or external command" (Windows)
This means Python wasn't added to your PATH during installation. Solutions:
- Reinstall Python and check "Add Python to PATH" (easiest)
- Manually add Python to PATH:
- Open System Properties → Advanced → Environment Variables
- Find the "Path" variable in User variables
- Add the path to your Python installation (typically
C:\Users\YourName\AppData\Local\Programs\Python\Python312\) - Add the Scripts subdirectory too (...\Python312\Scripts\)
"pip is not recognized" (Windows)
Usually occurs alongside the above problem. Fix the PATH and both problems resolve.
On Windows, also try py -m pip install as an alternative to pip install.
Wrong Python Version
If python --version shows an unexpected version:
where python # Windows — shows all Python installations in PATH
which python # macOS/Linux
The first result is what's being used. If it's not what you want, you may have multiple Python installations. You can specify a version explicitly:
python3.12 --version # Runs the specific version
"Permission denied" when installing packages
If you see permission errors:
1. Make sure your virtual environment is activated before installing
2. If installing globally, use --user flag: pip install --user package-name
3. Never use sudo pip install — this modifies your system Python and can cause serious problems
PowerShell script execution blocked (Windows)
If you see "running scripts is disabled on this system" when activating your venv in PowerShell:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
This allows locally created scripts to run without affecting system security.
ImportError: No module named 'pandas' (or any package)
This almost always means:
1. Your virtual environment isn't activated (check for the (venv) prefix in your terminal)
2. You installed pandas in one environment but are running Python from a different one
3. You installed pandas in a Jupyter kernel that doesn't match your terminal environment
Fix: activate your virtual environment, then run pip list to confirm pandas is installed.
Jupyter and VS Code Not Finding Your Packages
If VS Code's IntelliSense doesn't recognize libraries, or Jupyter's kernel doesn't find your installed packages:
In VS Code: Click the Python version indicator in the bottom status bar and select the interpreter from your venv folder (usually labeled "venv: Python 3.12.x").
In Jupyter: The kernel must match your virtual environment. Install ipykernel in your venv:
pip install ipykernel
python -m ipykernel install --user --name=myenv --display-name "Python (myenv)"
Then in Jupyter, go to Kernel → Change kernel and select "Python (myenv)".
2.8 The Anaconda Path (Alternative Setup)
If you prefer Anaconda, here's the parallel workflow:
Download and install from anaconda.com/downloads. Choose the Individual Edition (free). The installer is several gigabytes.
Create a conda environment:
conda create --name business-python python=3.12
Activate:
conda activate business-python
Install packages:
conda install pandas matplotlib seaborn
# For packages not in conda's default channel:
pip install plotly # Can mix pip and conda in most cases
Export environment:
conda env export > environment.yml
Recreate environment from file:
conda env create -f environment.yml
Note
The rest of this book uses pip/venv commands. If you're using conda, translate pip install X to conda install X (or pip install X when the package isn't in conda).
2.9 Installing the Book's Packages
Now that your environment is set up, install the packages you'll use throughout this book. Create a project folder called python-business-textbook, create and activate a virtual environment, then install:
# Core data analysis
pip install pandas numpy openpyxl xlrd matplotlib seaborn plotly
# Utility
pip install requests python-dateutil tqdm rich
For later chapters, you'll install additional packages as needed. The full list is in the requirements.txt file at the root of this textbook's repository.
Verify the installation:
# Run this in your Python REPL or a new .py file
import pandas
import matplotlib
import seaborn
import plotly
print("pandas:", pandas.__version__)
print("matplotlib:", matplotlib.__version__)
print("seaborn:", seaborn.__version__)
print("plotly:", plotly.__version__)
If all four lines print version numbers without errors, your environment is ready for Parts 1 and 2.
2.10 VS Code Configuration for Python
A few VS Code settings that improve the Python development experience:
Recommended Extensions
| Extension | Purpose |
|---|---|
| Python (Microsoft) | Core Python support: IntelliSense, debugging, linting |
| Pylance | Faster, more accurate type checking |
| Jupyter | Run notebooks inside VS Code |
| Black Formatter | Automatic code formatting (Chapter 39) |
| GitLens | Enhanced Git integration (Chapter 39) |
Useful Settings
Open VS Code settings (Ctrl+, on Windows, Cmd+, on macOS) and search for these:
- Python: Default Interpreter Path — Point to your venv's Python executable
- Editor: Format on Save — Set to
trueto auto-format your code on every save - Python: Linting Enabled — Set to
trueto show errors inline
The Integrated Terminal
VS Code has a built-in terminal (Ctrl+backtick). Use it instead of switching between VS Code and a separate terminal window. When you open VS Code in a folder with avenv/`, the integrated terminal auto-activates the virtual environment.
2.11 A Brief Tour of Python Files and Modules
As you start writing Python, you'll encounter a few file types:
| Extension | What It Is |
|---|---|
.py |
Standard Python script file |
.ipynb |
Jupyter Notebook file (JSON format internally) |
.pyc |
Compiled Python cache file (created automatically; ignore these) |
.toml |
Modern configuration format (pyproject.toml) |
.cfg |
Old-style configuration format |
.env |
Environment variable file (never commit to git) |
When Python executes a .py file, it compiles it to bytecode (.pyc files, stored in __pycache__/). This speeds up subsequent executions. You don't need to manage these files — Python handles them automatically.
Modules and imports: When you write import pandas, Python looks for a file or package called pandas in your Python path. For installed packages, this path includes your virtual environment's site-packages directory. For your own code, Python looks in the current directory and any directories you've added to the path.
2.12 What Priya and Maya Are Setting Up
Priya at Acme Corp is setting up Python on her work laptop. IT has given her a standard Windows 10 machine with local admin rights (necessary for installation). She goes with VS Code + Python from python.org. She creates a project folder called acme-analytics and her first virtual environment. Marcus Web watches over her shoulder skeptically. "Just doesn't break anything you're not supposed to," he says.
Maya Reyes sets up on her personal MacBook Pro. She goes with the python.org installer and VS Code. She also installs Jupyter because she's read that it's good for analysis (she'll use it extensively in Part 2). She names her first project maya-consulting.
By the end of this chapter, both of them have:
- Python 3.12 installed
- VS Code configured
- A virtual environment ready
- The core data packages installed
- Their first Python script (hello_business.py) running
The rest is just practice.
Summary
- Your Python environment consists of: the Python interpreter, pip (package installer), and virtual environments.
- Always create a virtual environment for every project (
python -m venv venv). - VS Code with the Python extension is the recommended editor for this book.
- Jupyter Notebooks are the preferred tool for data exploration (Parts 2 and 4).
- The REPL is Python's interactive mode — useful for quick tests and experiments.
- Common setup problems (PATH issues, wrong Python version, package not found) all have straightforward solutions.
- Key command summary:
python -m venv venv,source venv/bin/activate(orvenv\Scripts\activateon Windows),pip install package-name,pip freeze > requirements.txt.
Command Reference Card
| Task | Command |
|---|---|
| Check Python version | python --version |
| Create virtual environment | python -m venv venv |
| Activate venv (Windows CMD) | venv\Scripts\activate |
| Activate venv (macOS/Linux) | source venv/bin/activate |
| Deactivate venv | deactivate |
| Install a package | pip install package-name |
| Install from requirements.txt | pip install -r requirements.txt |
| List installed packages | pip list |
| Save packages to requirements.txt | pip freeze > requirements.txt |
| Run a Python script | python script_name.py |
| Enter the REPL | python |
| Exit the REPL | exit() |
| Launch Jupyter | jupyter notebook |
Chapter 3: Python Basics: Variables, Data Types, and Operators →