Table of Contents
IronForgeCyber
Courses
Resources
Blog
Log In


IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. python
  3. environments
9 min read

Packages & Environments

Python's standard library goes a long way, but sooner or later you need a third-party package: networkx for graphs, numpy for arrays, pwntools for exploits, requests for talking to a web server. Packages come from the Python Package Index (PyPI), and installing them is easy. Installing them well takes one idea: give every project its own isolated set of packages, called a virtual environment, so projects can't break each other or the operating system.

This lesson uses uv, a fast, all-in-one tool for managing Python packages, environments and even Python versions themselves.

Which Python Is Running?

Most Linux distributions and macOS come with a Python already installed. Check which one you have, and where it lives:

$ python3 --versionPython 3.12.7$ which python3/usr/bin/python3

From inside Python, sys.executable gives the same answer, which is useful when you're not sure which interpreter a script is actually running under:

python
1import sys
2sys.executable      # Return: '/usr/bin/python3'

That system Python belongs to the operating system: package managers and system tools depend on it and on the exact package versions installed into it. Installing your own packages there can break them, and most modern distributions now refuse to let you try:

$ pip install networkxerror: externally-managed-environment× This environment is externally managed...

The fix isn't to force the install. It's to install packages into a virtual environment instead.

Installing uv

uv is a single program. On Linux and macOS, the official installer puts it in your home directory:

$ curl -LsSf https://astral.sh/uv/install.sh | sh

It's also available from most package managers, such as brew install uv. Check that it works:

$ uv --versionuv 0.13.9

Virtual Environments

A virtual environment is just a directory, conventionally named .venv, that contains its own python and its own site-packages folder where packages are installed. Packages installed into it are only visible to that environment's Python. Delete the directory, and they're gone without a trace.

uv venv creates one in the current directory:

$ uv venvUsing CPython 3.12.14Creating virtual environment at: .venvActivate with: source .venv/bin/activate

Activating an environment puts its python first on your PATH, so python and python3 mean the environment's interpreter until you run deactivate. Most shells show (.venv) at the start of the prompt while one is active:

$ source .venv/bin/activate$ which python/home/user/solver/.venv/bin/python$ uv pip install networkx$ deactivate

uv pip install works like the familiar pip install, but it installs into the environment in the current directory, and it's much faster. This is fine for a quick experiment. For anything you'll come back to, a project, below, keeps track of what you installed for you.

Warning
Add .venv to .gitignore and never commit it. It holds compiled files specific to your machine, and it can always be rebuilt from the project's dependency list.

Projects

A project is a directory with a pyproject.toml file that lists what the code depends on. uv manages the environment for you based on that list, so you rarely need to create or activate one by hand. uv init starts a new project:

$ uv init --no-package solverInitialized project `solver` at `/home/user/solver`$ cd solver$ ls -Amain.py  pyproject.toml  .python-version  README.md

The --no-package flag sets the project up as a plain collection of scripts rather than an installable library, which is what a solver or a set of tools needs. The generated pyproject.toml starts with an empty dependency list:

pyproject.toml
1[project]
2name = "solver"
3version = "0.1.0"
4description = 

Adding Packages

uv add installs a package and records it in pyproject.toml. The first time, it also creates the project's .venv and a uv.lock file:

$ uv add networkxResolved 2 packages in 1msInstalled 1 package in 21ms + networkx==3.7

Packages you only need while developing, such as pytest, go in a separate development group with --dev. After running uv add networkx and uv add --dev pytest, pyproject.toml records both, on the highlighted lines:

pyproject.toml
1[project]
2name = "solver"
3version = "0.1.0"
4description =

uv remove networkx takes a package back out.

The lockfile, uv.lock, records the exact version of every package that was installed, including the packages your packages depend on. pyproject.toml says what you need ("networkx 3.7 or newer"); the lockfile says exactly what you got. Commit both. On another machine, uv sync reads them and rebuilds an identical .venv.

Running Code

uv run runs a command inside the project's environment, without activating anything. It also checks that the environment matches pyproject.toml first, and installs anything missing:

$ uv run main.pyHello from solver!$ uv run pytest$ uv run python

This is the habit to build: uv run instead of python3, inside a project. You can't accidentally run your code under the wrong interpreter, and anyone who clones the project gets the same packages just by running it.

Single-File Scripts

Not everything needs a project. A one-off solve script can declare its own dependencies in a special comment block at the top, a format called inline script metadata. uv add --script writes the block for you:

$ uv add --script solve.py networkx
solve.py
1# /// script
2# requires-python = ">=3.12"
3# dependencies = [
4#     "networkx>=3.7",
5# ]
6# ///

uv run solve.py reads that block, builds a cached environment with those packages, and runs the script in it. The script carries everything needed to run it, which makes it easy to share.

For a quick experiment that doesn't even deserve a file, --with adds packages to a throwaway environment:

$ uv run --with pwntools python

That opens a REPL with pwntools available, without installing it anywhere permanent.

Python Versions

uv can also install Python itself, separately from the system's copy. Each project's .python-version file records which version it uses, and uv downloads that version the first time it's needed. To install or select one yourself:

$ uv python install 3.12$ uv run --python 3.11 main.py

This is handy when a challenge or an old exploit script needs a specific version, and it never touches the system Python.

Command-Line Tools

Some packages are programs you run from the shell rather than libraries you import, such as the linter ruff. uv tool install puts one in its own isolated environment and adds the command to your PATH, and uvx runs a tool once without installing it:

$ uv tool install ruff$ uvx ruff check solve.py

Packages from Source

Not every package is on PyPI, or you may need an unreleased fix. uv add can install straight from a Git repository or a local directory:

$ uv add git+https://github.com/networkx/networkx$ uv add --editable ../my-library

An editable install links to the source directory instead of copying it, so changes you make to that code take effect immediately, without reinstalling. It's how you work on a library and a project that uses it at the same time.

Coming from pip
If you've used pip before, the translations are direct: pip install x becomes uv add x in a project or uv pip install x in a plain environment, python -m venv .venv becomes uv venv, and pip install -r requirements.txt becomes uv pip install -r requirements.txt.
"Add your description here"
5readme = "README.md"
6requires-python = ">=3.12"
7dependencies = []
"Add your description here"
5readme = "README.md"
6requires-python = ">=3.12"
7dependencies = [
8 "networkx>=3.7",
9]
10
11[dependency-groups]
12dev = [
13 "pytest>=9.2.0",
14]
7import networkx as nx
8print(nx.__version__)