Setup and Environment
Comprehensive guide on Setup and Environment.
Setup and Environment
Overview#
Before writing any Python code, you need a working Python installation and a
clean way to manage project dependencies. This notebook covers installing
Python, understanding the interpreter, working with virtual environments, and
using pip to manage packages. Getting this right early prevents a huge
number of headaches later, especially when working on multiple projects with
different dependency requirements.
1. Installing Python#
Python is available for Windows, macOS, and Linux. Most modern systems ship with some version of Python 3 pre-installed, but it is best practice to manage your own installation rather than relying on the system version.
| Platform | Recommended method |
|---|---|
| Windows | Official installer from python.org, or winget install Python.Python.3.12 |
| macOS | brew install python@3.12 (Homebrew) |
| Linux | Distro package manager (apt, dnf, pacman) or pyenv |
After installing, verify the installation:
bashpython3 --version pip3 --version
Note on python vs python3#
On many systems, python still refers to Python 2 or is unmapped entirely.
Always use python3 and pip3 explicitly unless you have confirmed your
python alias points to Python 3.
2. The Python Interpreter#
Python code can be run in two main ways:
- Interactive mode (REPL) — type
python3in a terminal to get an interactive prompt where you can execute statements one at a time. Useful for quick experiments. - Script mode — write code in a
.pyfile and run it withpython3 filename.py.
bash# Interactive mode
$ python3
>>> print("Hello, Python")
Hello, Python
>>> exit()
# Script mode
$ python3 hello.py
3. Virtual Environments#
A virtual environment is an isolated Python installation for a single project. It ensures that the packages one project depends on do not conflict with the packages another project depends on. Without virtual environments, installing packages globally can quickly lead to version conflicts across projects.
Creating a virtual environment#
Python includes the venv module by default — no extra installation needed.
bash# Create a virtual environment named "venv"
python3 -m venv venv
Activating a virtual environment#
bash# macOS / Linux
source venv/bin/activate
# Windows (Command Prompt)
venv\Scripts\activate.bat
# Windows (PowerShell)
venv\Scripts\Activate.ps1
Once activated, your terminal prompt typically changes to show the
environment name, e.g. (venv) $. Any package you install now with pip
goes into this isolated environment, not your system Python.
Deactivating#
bashdeactivate
4. Managing Packages with pip#
pip is Python's package installer. It downloads packages from the Python
Package Index (PyPI) and installs them into the active environment.
| Command | Purpose |
|---|---|
pip install requests | Install a package |
pip install requests==2.31.0 | Install a specific version |
pip uninstall requests | Remove a package |
pip list | List installed packages |
pip show requests | Show details about a package |
pip freeze | List installed packages in requirements format |
requirements.txt#
Most Python projects track dependencies in a requirements.txt file so
that the exact environment can be recreated elsewhere.
bash# Save current environment's packages
pip freeze > requirements.txt
# Install from a requirements file
pip install -r requirements.txt
Example requirements.txt:
textrequests==2.31.0 pandas==2.2.0 python-dotenv==1.0.1
5. Alternative Environment Tools#
While venv and pip are built into Python and sufficient for most cases,
larger projects sometimes use additional tooling:
| Tool | Purpose |
|---|---|
pyenv | Install and switch between multiple Python versions |
poetry | Dependency management and packaging with a lockfile |
pipenv | Combines virtual environments and pip into one workflow |
conda | Environment and package manager popular in data science |
These are optional. A beginner should be fully comfortable with venv and
pip before adopting any of these.
6. Recommended Project Structure#
A clean starting layout for a new Python project:
Architecture & Data Flowmy_project/ ├── venv/ # virtual environment (not committed to git) ├── src/ │ └── my_project/ │ └── __init__.py ├── tests/ ├── requirements.txt ├── .gitignore └── README.md
Add venv/ to .gitignore so the environment itself is never committed —
only requirements.txt needs to be shared.
Notes#
- Always activate your virtual environment before installing packages or running your project's code.
- One virtual environment per project is the standard convention.
- Never commit the
venv/folder to version control. - If a terminal session is closed, the virtual environment deactivates automatically — you must reactivate it in each new session.
- Editors like VS Code can be configured to automatically activate the
correct virtual environment per project (select the interpreter from the
venvfolder).
Best Practices Checklist#
- Install Python 3 from an official or trusted source, not an outdated system default
- Verify installation with
python3 --version - Create a dedicated virtual environment for every new project
- Always activate the virtual environment before installing packages
- Maintain an up-to-date
requirements.txt - Add
venv/(or equivalent) to.gitignore - Pin package versions for reproducibility in production projects
- Deactivate the environment when switching to a different project