AgentSkillsCN

python-venv

在本地虚拟环境中运行Python脚本,并采用可复现的命令模式,确保脚本执行的稳定性和高效性。

SKILL.md
--- frontmatter
name: python-venv
description: Create and run Python scripts in a local virtual environment with reproducible command patterns.
metadata: {"nanobot":{"emoji":"🐍","requires":{"bins":["python3"]}}}

Python venv

Use this skill when the task requires writing and running Python code with isolated dependencies.

Core rule

exec calls are stateless across invocations. Do not rely on source .venv/bin/activate persisting between calls. Always call the interpreter and pip directly from the venv path.

Standard workflow

  1. Create a project-local environment:
bash
python3 -m venv .venv
  1. Install dependencies:
bash
.venv/bin/python -m pip install --upgrade pip
.venv/bin/python -m pip install -r requirements.txt
  1. Run scripts:
bash
.venv/bin/python script.py

Recommended execution pattern

When possible, run setup + execution in one command so context is explicit:

bash
python3 -m venv .venv && \
.venv/bin/python -m pip install --upgrade pip && \
.venv/bin/python -m pip install -r requirements.txt && \
.venv/bin/python script.py

File layout

  • Keep scripts in workspace-local paths, for example scripts/ or repo root.
  • Keep environment in .venv/ at project root.
  • Keep dependency pins in requirements.txt.

Troubleshooting

  • python3: command not found: install Python 3 and rerun.
  • No module named pip: recreate venv with python3 -m venv .venv --upgrade-deps if available.
  • Build tool errors on native packages: report the missing system dependency and stop before retry loops.