A coding agent working on a Python repository hits a missing import and does the obvious thing: pip install whatever is missing. On a laptop, with a person watching, that is a small risk. For an agent running unattended on its own branch, three things are worth deciding in advance: where the packages go, which ones are allowed, and whether an install is allowed to run code.
This post sets out a short routine for that. The pip details come from pip’s own secure installs documentation, read on 2 December 2026. Sandscale commands are from CLI 1.4.1.
Put the packages in a virtual environment, on the agent’s machine
Without a virtual environment, pip install writes into whichever Python it finds first, which on a shared machine may be everyone’s. With one agent per machine, that matters less, but a virtual environment still keeps the project’s dependencies separate from the machine’s tools and makes “start again” a matter of deleting a directory:
python3 -m venv .venv
. .venv/bin/activate
Add .venv to .gitignore so the agent never commits it.
Where the machine is the agent’s own, there is nothing else on it to damage. On Sandscale, a workspace is a cloud machine per agent with a persistent disk, so the virtual environment survives between sessions. The environment is built once, and the next task starts with it already there.
Two things pip can do for you
pip’s secure installs documentation names two protections that a team can turn on.
Hash-checking mode. Put a hash on every requirement, and pip refuses a package whose archive does not match. In the documentation’s words, it uses local hashes embedded in the requirements.txt file to protect against remote tampering and network issues. Three rules from the documentation shape how you use it:
- It is all or nothing. Specifying
--hashfor any requirement turns the mode on for every requirement. - Hashes are required for all dependencies, including the indirect ones. A dependency that is not spelled out and hashed results in an error.
- Requirements must be pinned, to a URL, a filesystem path or
==.
You can force the mode with --require-hashes, which the documentation notes is useful in deploy scripts and is also a convenient way to bootstrap a list of hashes, since it shows the hashes of the packages it fetched. A tool such as pip-compile --generate-hashes from pip-tools can produce the whole file; the pip documentation describes the file’s format, not that tool, so check pip-tools’ own docs for it.
Wheels only. Pass --only-binary :all: and pip disallows source distributions. This matters because installing a source distribution can mean building it, and building runs the package’s build code on your machine. A wheel is a built archive: pip unpacks it. (Some packages ship only a source distribution. Those will fail under this flag, which is the point: someone should decide on purpose.)
Together:
python3 -m pip install --require-hashes --only-binary :all: -r requirements.txt
If a package fails because it has no wheel for the machine, or a hash does not match, the agent should stop and report. It should not remove the flags to get past the error.
The rule for AGENTS.md
## Dependencies (Python)
- Work inside .venv. Create it with `python3 -m venv .venv` if it does not exist.
- Install only from the repository's requirements file, with
`python3 -m pip install --require-hashes --only-binary :all: -r requirements.txt`.
- Do not run `pip install <package>` for anything not in the requirements file.
If you think a package is missing, stop and say which package and why.
- If an install fails on a hash or because no wheel is available, report it.
Do not drop --require-hashes or --only-binary to make it pass.
- Do not edit requirements.txt unless the task says to.
Like every instruction file, this is a request. If you want enforcement, a hook can rewrite or refuse a pip install; our post on Claude Code hooks that route Bash to a sandbox shows the mechanism, and its pattern already includes pip install.
When the agent really does need a new package
It will happen. The task is “add a CSV export” and the library for it is not in the requirements file. The routine:
- The agent stops and writes down the package, the reason, and the alternatives in the standard library.
- A person decides. They look at the package’s age, maintainers and what else it pulls in, which is the work our post on agents installing packages describes.
- A person updates the pinned, hashed requirements file in a commit of its own, so the diff is only the dependency change.
- The agent then installs from the updated file.
This is slower than letting the agent install it. That is deliberate. A new dependency is a decision about code the team will run for years.
Network and where the install runs
An install has to fetch packages, so the machine needs the network access your plan and your organization’s policy allow. An allowlist that includes the package index and nothing more is the shape to aim for; see designing an egress allowlist. Running the install on the agent’s own machine means a build script that behaves badly is running next to that agent’s checkout and not next to your credentials:
sandscale workspace create --name data-2290-csv-export --template-id TEMPLATE_ID --ttl-seconds 28800
sandscale workspace ssh data-2290-csv-export
What the reviewer checks
- Is
requirements.txt(or the lock file your project uses) in the diff? If the task did not ask for a dependency change, ask why. - If it changed, is each hash present, and does each new package match what the task needed?
- Did the agent touch
setup.py,pyproject.tomlbuild settings or aconftest.py? Those run code at install or test time. Our reviewer checklist covers it.
What this does not do
Hashes prove that the archive is the one you pinned. They do not prove that the package is good. If you pinned a malicious version, the hashes match. The routine above keeps agents from changing what you pinned and from running build code unasked; it does not replace choosing dependencies with care.