Free GuideBeginner
15 min read
#python#setup#installation#virtualenv#pip#environment

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.

PlatformRecommended method
WindowsOfficial installer from python.org, or winget install Python.Python.3.12
macOSbrew install python@3.12 (Homebrew)
LinuxDistro package manager (apt, dnf, pacman) or pyenv

After installing, verify the installation:

bash
python3 --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:

  1. Interactive mode (REPL) — type python3 in a terminal to get an interactive prompt where you can execute statements one at a time. Useful for quick experiments.
  2. Script mode — write code in a .py file and run it with python3 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#

bash
deactivate

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.

CommandPurpose
pip install requestsInstall a package
pip install requests==2.31.0Install a specific version
pip uninstall requestsRemove a package
pip listList installed packages
pip show requestsShow details about a package
pip freezeList 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:

text
requests==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:

ToolPurpose
pyenvInstall and switch between multiple Python versions
poetryDependency management and packaging with a lockfile
pipenvCombines virtual environments and pip into one workflow
condaEnvironment 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.

A clean starting layout for a new Python project:

Architecture & Data Flow
my_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 venv folder).

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
Knowledge Checkpoint

Python Setup & Environment Checkpoint

Q1.What is the primary advantage of using a virtual environment (e.g. venv, uv, conda) for Python projects?
AIt compiles Python code into native x86 machine binaries.
BIt isolates project-specific dependencies and packages, preventing version conflicts across different applications.
CIt eliminates the need for the CPython interpreter.
DIt automatically fixes Python syntax errors.
Q2.Which tool/file is the modern standard (PEP 518/621) configuration file for Python project metadata and build requirements?
Asetup.cfg only
Brequirements.txt
Cpyproject.toml
DPipfile.lock
Q3.When creating a virtual environment using the standard library on Python 3, which command is used?
Apython -m venv .venv
Bpip install virtualenv-root
Cpython --create-env
Dpyenv new-env
Track Your Learning

Finished studying this notebook?

Mark this guide as completed to update your course progress roadmap.