What shebang to use for Python scripts run under a pyenv virtualenv
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When using pyenv and pyenv-virtualenv, the shebang line in Python scripts determines which interpreter is used when running a file directly. A poor shebang can accidentally bypass your environment and run with system Python. The right choice balances portability, reproducibility, and local tooling behavior.
How Shebang Resolution Works
A shebang is the first line of an executable script and tells the operating system which interpreter to launch.
When this script is executable, the shell follows env lookup rules and uses python3 found on current PATH.
Make script executable:
Recommended Shebang With pyenv
For pyenv-managed workflows, #!/usr/bin/env python3 is usually the best default. It respects active shell environment and pyenv shims, so version selection follows your configured Python and virtual environment.
Alternative forms:
#!/usr/bin/python3ties script to system interpreter- hardcoded pyenv paths are brittle across machines
Using env keeps scripts portable between developers and CI systems where interpreter location differs.
Configure pyenv Version Selection
pyenv chooses interpreter using priority order:
PYENV_VERSIONenvironment variable- local
.python-versionfile - global pyenv version
Useful commands:
If python3 in PATH points to pyenv shims, the shebang resolves correctly.
Virtual Environments and Script Execution
With pyenv-virtualenv, activate environment before running scripts directly.
If activation is skipped and shim path is misconfigured, script can fall back to another interpreter.
When to Pin a Specific Interpreter
In controlled deployment systems, you might intentionally pin a full interpreter path. This improves reproducibility but reduces portability.
Use this approach only when deployment tooling guarantees path stability.
Debugging Wrong Interpreter Issues
If script runs with unexpected Python:
Also verify shell init files include pyenv setup code and that non-interactive shells inherit correct PATH.
Shebang Behavior in Automation Environments
Scripts executed by cron, CI agents, or systemd services may run with a different PATH than your interactive shell. In these environments, pyenv shims might not be active unless initialization is explicit.
For CI jobs, print interpreter details at startup:
For cron jobs, prefer a wrapper script that loads shell profile or sets PYENV_ROOT and PATH before running Python scripts. This avoids silent interpreter drift between local runs and scheduled runs.
If reproducibility is critical in automation, invoke the environment interpreter directly from job configuration while keeping source shebang portable for developer machines.
Document interpreter policy in project README so contributors know whether scripts rely on pyenv, local virtual environments, or pinned deployment interpreters.
This avoids confusing environment drift during onboarding and troubleshooting.
Common Pitfalls
- Using system path shebang and assuming pyenv still controls interpreter.
- Forgetting execute permissions and invoking script in inconsistent ways.
- Hardcoding user-specific pyenv installation paths in shared scripts.
- Missing pyenv initialization in shell startup files.
- Assuming IDE run configuration always matches terminal environment.
Summary
- For pyenv virtualenv workflows,
#!/usr/bin/env python3is the most practical shebang. - Interpreter selection then follows pyenv shim and version configuration.
- Use local pyenv versions to lock project interpreter consistently.
- Pin absolute interpreter paths only in tightly controlled deployment setups.
- Verify shebang,
PATH, and pyenv initialization when debugging mismatches.

