10 min read
Environment Setup
Working in notebooks (Jupyter/Colab) and VS Code; managing environments and packages
What You'll Learn
- Setting up Python development environments
- Working with Jupyter Notebooks and Google Colab
- Using VS Code for Python development
- Managing virtual environments
- Installing and managing packages
Development Environments
Jupyter Notebooks:
- Interactive computing environment
- Combines code, visualizations, and text
- Perfect for data analysis and exploration
- Runs in your browser
Google Colab:
- Free cloud-based Jupyter notebooks
- No setup required
- Free GPU access
- Easy sharing and collaboration
VS Code:
- Full-featured code editor
- Excellent Python support
- Integrated terminal and debugger
- Extensions for data science
Virtual Environments
Why use virtual environments?
- Isolate project dependencies
- Avoid package conflicts
- Reproducible environments
- Clean project structure
Creating virtual environment:
terminal
# Using venv
python -m venv myenv
# Activate on Windows
myenv\Scripts\activate
# Activate on Mac/Linux
source myenv/bin/activatePackage Management
pip - Python package installer:
terminal
# Install package
pip install pandas
# Install specific version
pip install pandas==1.5.0
# Install from requirements.txt
pip install -r requirements.txt
# List installed packages
pip list
# Create requirements.txt
pip freeze > requirements.txtEssential packages for data analysis:
- pandas - Data manipulation
- numpy - Numerical computing
- matplotlib - Visualization
- seaborn - Statistical visualization
- jupyter - Interactive notebooks
Jupyter Notebooks
Starting Jupyter:
terminal
# Install Jupyter
pip install jupyter
# Start Jupyter Notebook
jupyter notebook
# Start JupyterLab
jupyter labNotebook shortcuts:
- Shift + Enter - Run cell
- Ctrl + Enter - Run cell (stay)
- A - Insert cell above
- B - Insert cell below
- DD - Delete cell
- M - Convert to Markdown
- Y - Convert to Code
Google Colab
Getting started:
- Go to colab.research.google.com
- Sign in with Google account
- Create new notebook
- Start coding!
Colab advantages:
- No setup required
- Free GPU/TPU access
- Google Drive integration
- Easy sharing
VS Code Setup
Installing Python extension:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Search "Python"
- Install Microsoft Python extension
Key features:
- IntelliSense (auto-completion)
- Debugging
- Linting
- Jupyter support
- Git integration
Best Practices
Project structure:
project/
āāā data/
ā āāā raw/
ā āāā processed/
āāā notebooks/
āāā src/
āāā requirements.txt
āāā README.md
Environment management:
- One virtual environment per project
- Keep requirements.txt updated
- Document dependencies
- Use .gitignore for venv/
Practice Questions
Test your understanding with these practice questions.
1
Practice Exercise
What command would you use to create a virtual environment named "data_project"?
starter_code.py
# Type the command to create a virtual environment
# (This is a bash command, not Python code)2
Practice Exercise
Write Python code to check which packages are installed in your environment
starter_code.py
import pkg_resources
# Get list of installed packages
# Your code here3
Practice Exercise
How would you install pandas version 2.0.0 specifically?
starter_code.py
# Write the pip command to install pandas 2.0.0
# (This is a bash command)Practice & Experiment
Test your understanding by running Python code directly in your browser.