Introduction to Development Environments
A proper development environment is like a well-organized workshop. Everything has its place, your tools are at hand, and you can focus on creating rather than searching for the right screwdriver.
For web development, your environment consists of various tools, applications, and configurations that work together to help you write, test, and deploy code efficiently. A good environment should:
- Enhance productivity by automating repetitive tasks
- Maintain consistency across different projects and team members
- Facilitate debugging with appropriate tools and logs
- Reflect production conditions to minimize "works on my machine" issues
- Allow portability so you can work from different computers or share setups
In this section, we'll set up the core tools for Python web development, starting with a code editor, version control system, Python installation, and Docker. Later in the course, we'll add more specialized tools for different aspects of web development.
Code Editor: Visual Studio Code
A code editor is your primary interface for writing code. Visual Studio Code (VS Code) has become the de facto standard for web development due to its balance of simplicity and power. Its extensions ecosystem makes it particularly well-suited for Python and web development.
Why VS Code?
VS Code offers several advantages for Python web development:
- Free and open-source: No licensing costs or restrictions
- Cross-platform: Works on Windows, macOS, and Linux
- Lightweight yet powerful: Faster than full IDEs but with many of the same features
- Rich extension ecosystem: Customize for different languages and workflows
- Integrated terminal: Run commands without leaving the editor
- Git integration: Manage version control directly in the editor
- Debugging support: Set breakpoints, inspect variables, etc.
- IntelliSense: Intelligent code completion and hints
Installation
Download VS Code from https://code.visualstudio.com/ and follow the installation instructions for your operating system:
Windows
- Run the downloaded installer (.exe file)
- Follow the installation wizard
- Ensure "Add to PATH" is checked during installation
macOS
- Open the downloaded .dmg file
- Drag Visual Studio Code.app to the Applications folder
- Optionally, add VS Code to the Dock
Linux
- For Debian/Ubuntu:
sudo apt install ./downloaded-file.deb - For Red Hat/Fedora:
sudo dnf install ./downloaded-file.rpm - Alternatively, use your distribution's package manager
Essential Extensions for Python Web Development
After installing VS Code, add these extensions to enhance your Python web development experience:
Python Extensions
- Python (Microsoft): Core Python language support
- Pylance: Fast, feature-rich language server for Python
- Python Indent: Corrects indentation for Python
- Python Docstring Generator: Generate documentation strings
Web Development Extensions
- HTML CSS Support: Enhanced HTML and CSS editing
- ESLint: JavaScript linting
- Prettier: Code formatter for web languages
- Live Server: Launch a local development server
Tool Integration Extensions
- Docker: Create, manage, and debug Docker containers
- Remote - Containers: Develop inside Docker containers
- GitLens: Enhanced Git capabilities
- GitHub Pull Requests: Review and manage Pull Requests
Configuring VS Code for Python
To optimize VS Code for Python web development, adjust these settings:
- Open Settings (File > Preferences > Settings or Ctrl+,)
- Search for and adjust these settings:
- Python > Formatting Provider: "black" (after installing Black)
- Editor: Format On Save: Check to enable
- Python > Linting: Enabled: Check to enable
- Python > Linting: Pylint Enabled: Check to enable
- Files: Auto Save: "afterDelay" (saves files automatically)
You can also create a workspace configuration file (.vscode/settings.json) to share settings with team members or across multiple computers:
{
"python.formatting.provider": "black",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"editor.formatOnSave": true,
"files.autoSave": "afterDelay",
"python.pythonPath": "${workspaceFolder}/.venv/bin/python"
}
Version Control: Git
Git is like a time machine for your code. It allows you to track changes, experiment safely, and collaborate with others. We'll explore Git in depth tomorrow, but today we'll focus on installation and basic setup.
Installation
Install Git based on your operating system:
Windows
- Download the installer from https://git-scm.com/
- Run the installer and follow the wizard
- Use the default options, but consider these options:
- When prompted about adjusting your PATH environment, select "Git from the command line and also from 3rd-party software"
- For line ending conversions, select "Checkout Windows-style, commit Unix-style line endings"
- For the terminal emulator, select "Use Windows' default console window"
macOS
- If you have Homebrew:
brew install git - Otherwise, install Xcode Command Line Tools:
xcode-select --install - Alternatively, download the installer from https://git-scm.com/
Linux
- For Debian/Ubuntu:
sudo apt install git - For Red Hat/Fedora:
sudo dnf install git - For other distributions, use your package manager
Basic Git Configuration
After installing Git, configure your identity (used in commit messages):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Set up some helpful configurations:
# Set default branch name to main
git config --global init.defaultBranch main
# Set up a better log format
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Set up VS Code as the default editor for Git
git config --global core.editor "code --wait"
Verifying Installation
Check that Git is installed correctly:
git --version
You should see output like git version 2.38.0 (the version number may differ).
Python Installation
We'll be using Python 3.10 or newer for this course. Python is the core language we'll use for backend development.
Installation Methods
Choose the installation method that works best for your system:
Windows
- Download the installer from python.org
- Run the installer and check "Add Python to PATH"
- Click "Install Now" for a standard installation
- Verify installation with
python --versionin Command Prompt or PowerShell
macOS
- Option 1: Homebrew (recommended)
# Install Homebrew if not already installed /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" # Install Python brew install python - Option 2: Download the installer from python.org
- Verify installation with
python3 --versionin Terminal
Linux
- Most distributions come with Python pre-installed
- To install or update:
- Debian/Ubuntu:
sudo apt update && sudo apt install python3 python3-pip python3-venv - Red Hat/Fedora:
sudo dnf install python3 python3-pip
- Debian/Ubuntu:
- Verify installation with
python3 --versionin Terminal
Virtual Environments
Python virtual environments allow you to isolate dependencies for different projects. We'll use them throughout the course:
Creating a Virtual Environment
# Navigate to your project directory
cd my_project
# Create a virtual environment
python -m venv .venv # On Windows
python3 -m venv .venv # On macOS/Linux
# Activate the virtual environment
# Windows (Command Prompt)
.venv\Scripts\activate
# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1
# macOS/Linux
source .venv/bin/activate
When a virtual environment is active, you'll see its name in your terminal prompt, like (.venv) $.
Installing Packages in a Virtual Environment
# Ensure pip is up to date
pip install --upgrade pip # pip3 for macOS/Linux
# Install packages
pip install flask
pip install django
# Save dependencies to requirements.txt
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
Deactivating a Virtual Environment
When you're done working, deactivate the environment:
deactivate
Python Tools and Extensions
Install these tools to enhance your Python development experience:
# Black (code formatter)
pip install black
# Pylint (linter)
pip install pylint
# Pytest (testing framework)
pip install pytest
# IPython (enhanced interactive shell)
pip install ipython
Docker
Docker allows you to create consistent development and production environments. Think of it as a way to package your application with all its dependencies into a standardized unit.
We'll cover Docker in detail on Wednesday, but today we'll focus on installation and basic verification.
Installation
Install Docker Desktop, which includes Docker Engine, Docker CLI, Docker Compose, and other tools:
Windows
- Ensure your system meets the requirements:
- Windows 10 64-bit: Pro, Enterprise, or Education (Build 16299 or later)
- Windows 10 Home (Build 19018 or later) with WSL 2
- Virtualization enabled in BIOS
- Download Docker Desktop from docker.com
- Run the installer and follow the wizard
- Start Docker Desktop from the Start menu
macOS
- Ensure your system meets the requirements:
- macOS 10.15 or newer
- Apple Silicon or Intel processor
- Download Docker Desktop from docker.com
- Open the downloaded .dmg file and drag Docker.app to the Applications folder
- Start Docker Desktop from the Applications folder
Linux
- Docker Desktop is available for some Linux distributions, or you can install Docker Engine:
- Ubuntu: Installation guide
- Fedora: Installation guide
- Debian: Installation guide
- CentOS: Installation guide
- Consider adding your user to the docker group to avoid using sudo:
sudo usermod -aG docker $USER newgrp docker # Apply changes without logging out
Verifying Docker Installation
Check that Docker is installed correctly:
docker --version
docker-compose --version
You should see output with version numbers for both commands.
Run a test container to ensure everything works:
docker run --rm hello-world
You should see a message indicating that Docker is working correctly.
Setting Up Your Course Directory Structure
Organizing your files is essential for efficient development. Let's create a structured directory for this course:
Creating the Directory Structure
# Navigate to your preferred location for course materials
cd ~/Documents # or another location of your choice
# Create the main course directory
mkdir fullstack_python
# Navigate to the course directory
cd fullstack_python
# Create subdirectories for each week
mkdir week1 week2 week3 week4 week5 week6 week7 week8 week9 week10 week11 week12 week13 week14
# Create a projects directory for major assignments
mkdir projects
# Create a resources directory for reference materials
mkdir resources
# Create a .gitignore file
touch .gitignore
Setting Up a Basic .gitignore File
A .gitignore file tells Git which files to exclude from version control. Here's a basic configuration for Python web development:
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Virtual Environments
venv/
.venv/
ENV/
env/
env.bak/
venv.bak/
# IDE and Editors
.idea/
.vscode/
*.swp
*.swo
*~
# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# Database
*.sqlite3
*.db
# Dependencies
node_modules/
jspm_packages/
# Distribution
dist/
build/
# Testing
.coverage
htmlcov/
.tox/
.nox/
.hypothesis/
.pytest_cache/
# Docker
.dockerignore
docker-compose.override.yml
# Operating System
.DS_Store
Thumbs.db
Initialize Git Repository
Let's initialize this directory as a Git repository to start tracking changes:
git init
git add .gitignore
git commit -m "Initial commit with directory structure and .gitignore"
Practical Exercise: Environment Setup and Verification
Let's practice setting up and testing our development environment with a hands-on exercise:
Exercise: Create and Run a Simple Python Web Application
Step 1: Create a Project Directory
# Navigate to the Week 1 directory
cd week1
# Create a directory for this exercise
mkdir hello_web
# Navigate to the exercise directory
cd hello_web
Step 2: Create a Virtual Environment
# Create a virtual environment
python -m venv .venv # On Windows
python3 -m venv .venv # On macOS/Linux
# Activate the virtual environment
# Windows (Command Prompt)
.venv\Scripts\activate
# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1
# macOS/Linux
source .venv/bin/activate
Step 3: Install Flask
pip install flask
Step 4: Create a Simple Flask Application
Create a file named app.py with the following content:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return '''
<!DOCTYPE html>
<html>
<head>
<title>Hello Web Development</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 40px;
line-height: 1.6;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: #f4f4f4;
padding: 20px;
border-radius: 5px;
}
h1 {
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello, Web Development!</h1>
<p>Your development environment is working correctly.</p>
<ul>
<li>Python is installed and running</li>
<li>Flask is installed and serving this page</li>
<li>Your browser is displaying the page properly</li>
</ul>
<p>You're ready to start your web development journey!</p>
</div>
</body>
</html>
'''
if __name__ == '__main__':
app.run(debug=True)
Step 5: Run the Application
python app.py # On Windows
python3 app.py # On macOS/Linux
You should see output indicating that the Flask server is running, typically at http://127.0.0.1:5000/.
Step 6: View the Application
Open your web browser and navigate to http://127.0.0.1:5000/ or http://localhost:5000/. You should see the "Hello, Web Development!" page.
Step 7: Initialize Git Repository
# Create a .gitignore file
echo ".venv/" > .gitignore
echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore
# Initialize Git repository
git init
# Add files to Git
git add .
# Commit the changes
git commit -m "Initial commit of Hello Web application"
Step 8: Create a requirements.txt File
pip freeze > requirements.txt
This file lists all installed packages, making it easier to recreate the environment later.
Step 9: Stop the Server and Deactivate the Virtual Environment
Press Ctrl+C to stop the Flask server, then deactivate the virtual environment:
deactivate
Exercise Extensions
If you'd like to explore further, try these extensions:
- Modify the HTML to include your name or personalize the message
- Add a second route at
/aboutwith different content - Create a proper template file instead of inline HTML
- Use VS Code to edit the files and run the server from its integrated terminal
- Add CSS to improve the styling of the page
Assignment: Environment Setup
Let's close with a practical assignment to set up your development environment:
Assignment: Environment Setup
- Install VS Code from https://code.visualstudio.com/
- Install Git from https://git-scm.com/
- Install Python 3.10 or newer from https://www.python.org/downloads/
- Install Docker Desktop from https://www.docker.com/products/docker-desktop
- Install recommended VS Code extensions (Python, Pylance, Docker, GitLens, HTML CSS Support)
- Create a directory structure for the course:
fullstack_python/ ├── week1/ ├── week2/ ├── week3/ └── projects/ - Complete the "Hello Web" exercise outlined above
- Take a screenshot of your installed software and directory structure
- Take a screenshot of your "Hello Web" application running in the browser
Submit both screenshots as your first assignment.
Resources for Further Learning
VS Code
Git
Python
- Python Official Documentation
- Real Python - Tutorials and articles
- The Hitchhiker's Guide to Python