GitHub - jdcampolargo/venvAUTO

2 min read Original article ↗

venvAUTO

Make Python venv setup on macOS (zsh) a one-liner. No more typing the same three commands for every project.

Why this exists (the short, casual version)

I got tired of doing this for every project:

python3 -m venv venv
source venv/bin/activate
deactivate

And the "just put it in a script" idea is misleading. Activation only sticks if it runs in your current shell. If you run a script, it runs in a subshell, so your terminal never actually gets activated.

Also, if you have conda installed, python3 might point to conda's Python. That can create confusing venvs. This tool uses /usr/bin/python3 by default to avoid surprises.

Quick install

git clone https://github.com/jdcampolargo/venvAUTO.git
cd venvAUTO
./install.sh
source ~/.zshrc

(Or just open a new terminal tab/window.)

Usage

That will:

  • Create ./venv if it doesn't exist
  • Activate it in your current shell

To exit:

Or just close the terminal tab/window.

Demo (5 lines)

cd my_project
venv
pip install -r requirements.txt
python main.py
venvdown

How it works (why source matters)

Activation modifies your current shell environment (PATH and a few variables). If you run a script, it runs in a separate shell process, so those changes disappear when the script ends. venv is an alias, so it runs in your current shell and sticks.

Optional add-on: auto-install requirements

If you want venv to install deps whenever requirements.txt exists, you can swap the alias to this variant:

alias venv='[ -d venv ] || /usr/bin/python3 -m venv venv; source venv/bin/activate; [ -f requirements.txt ] && pip install -r requirements.txt'

Troubleshooting

  • command not found: venv
    • Run source ~/.zshrc or open a new terminal.
    • Make sure the venvAUTO block exists in ~/.zshrc.
    • If you use a custom ZDOTDIR, check that file instead.
  • python3 points to conda and things look weird
    • venvAUTO uses /usr/bin/python3 on purpose to avoid conda.
    • If you want conda's Python, edit the alias in your ~/.zshrc.

Uninstall

./uninstall.sh
source ~/.zshrc

License

MIT