Automatic Python virtual environment activation with multi-environment and multi-shell support based on .auto_venv files when changing directories.
auto_venv
is a lightweight shell tool that automatically activates and deactivates Python virtual environments as you navigate between project directories. It uses .auto_venv
configuration files to determine which virtual environment should be active in each directory tree. The tool now supports multiple Python environments per project and works seamlessly across Bash, Zsh, and Fish shells.
- Multi-shell support - Works with Bash, Zsh, and Fish shells
- Automatic activation/deactivation when changing directories
- Multi-environment support - manage multiple Python versions per project
- Environment switching - easily switch between different Python versions
- Recursive search for
.auto_venv
files up the directory tree - Multiple Python versions support (Python 2.7 with virtualenv, Python 3+ with venv)
- Flexible paths (relative or absolute paths in .auto_venv files)
- Universal installation - single install script for all shells
- Visual feedback showing current environment status
- Environment validation to ensure virtual environments are properly configured
auto_venv
is designed for Unix-type shells. It has been tested on Ubuntu and should work with any Linux distribution. For Windows users, it requires a Unix-like environment such as WSL (Windows Subsystem for Linux), Git Bash, Cygwin or MSYS2.
- Clone this repository:
git clone https://github.com/collorg/auto_venv.git
cd auto_venv
- Run the installation script:
./install.sh
The installation script will:
- Ask for your Python search path (default:
/usr/bin
) - Detect all installed shells (Bash, Zsh, Fish)
- Add the Python search path to each shell's RC file
- Add the appropriate
source
command to each shell's RC file - Configure the universal dispatcher for Bash and Zsh
- Set up direct sourcing for Fish
Example installation:
$ ./install.sh
Installing auto_venv...
Enter python search path [/usr/bin]: /opt/homebrew/bin
/opt/homebrew/bin
Detecting installed shells...
✓ Added auto_venv to /home/user/.bashrc (Bash)
✓ Added auto_venv to /home/user/.zshrc (Zsh)
✓ Added auto_venv to /home/user/.config/fish/config.fish (Fish)
✓ Added auto_venv to /home/user/.profile (POSIX sh)
Installation complete! auto_venv was added to 4 shell configuration(s).
Reload your shell or run 'source <rc_file>' to activate.
- Reload your shell or start a new terminal session
For Bash or Zsh:
# Add Python search path (if different from /usr/bin)
echo "AUTO_VENV_PYTHON_SEARCH_PATH=/opt/python/bin" >> ~/.bashrc # For Bash
echo "AUTO_VENV_PYTHON_SEARCH_PATH=/opt/python/bin" >> ~/.zshrc # For Zsh
# Add source command
echo "source $(pwd)/auto_venv" >> ~/.bashrc # For Bash
echo "source $(pwd)/auto_venv" >> ~/.zshrc # For Zsh
For Fish:
# Add Python search path (if different from /usr/bin)
echo 'set AUTO_VENV_PYTHON_SEARCH_PATH "/opt/python/bin"' >> ~/.config/fish/config.fish
# Add source command
echo "source $(pwd)/auto_venv.fish" >> ~/.config/fish/config.fish
The Python search path is configured during installation. To change it later:
For Bash/Zsh:
export AUTO_VENV_PYTHON_SEARCH_PATH="/opt/python/bin" # your Python installation path
For Fish:
set AUTO_VENV_PYTHON_SEARCH_PATH "/opt/python/bin"
Note: Make sure this variable is set before the source
line in your RC file.
Navigate to your project directory and run:
auto_venv --new
This will:
- Show available Python versions
- Let you choose a Python version
- Ask for the virtual environment path (default:
.venv_<python_version>
) - Create the virtual environment
- Create a
.auto_venv
file with the environment path - Automatically activate the environment
To add another Python version to your project:
auto_venv --add
This will show available Python versions (marking already configured ones with ✓) and let you add a new environment.
You can also specify a custom name:
auto_venv --add myenv
To switch to a different Python environment:
auto_venv --switch python3.11
To see all configured environments:
auto_venv --list
Output example:
[auto_venv] Available environments in /path/to/project:
python3.9: .venv_python3_9 (default)
python3.11: .venv_python3_11 (active)
python3.12: .venv_python3_12
To change which environment is activated by default:
auto_venv --set-default python3.11
To check if the current environment is valid:
auto_venv --validate
Once a .auto_venv
file exists in a directory (or any parent directory), the virtual environment will be automatically activated when you cd
into that directory tree.
cd /path/to/my-project # Automatically activates the default environment
cd /path/to/other-project # Switches to the other project's environment
cd ~ # Deactivates when leaving project directories
Run auto_venv
without arguments to see the current environment status:
auto_venv
Output example:
[auto_venv] /path/to/my-project/.venv_python3_11 (python3.11) activated (Python 3.11.0)
[auto_venv] Multiple environments available. Use 'auto_venv --list' to see all.
[auto_venv] Use 'auto_venv --switch <name>' to change environment.
The .auto_venv
file now supports multiple environments:
default:.venv_python3_9
python3.9:.venv_python3_9
python3.11:.venv_python3_11
python3.12:.venv_python3_12
- default: specifies which environment to activate by default
- environment_name:path: maps environment names to their paths
Old single-line format files are automatically converted to the new format when accessed.
Set the AUTO_VENV_PYTHON_SEARCH_PATH
environment variable to specify where Python versions are located:
export AUTO_VENV_PYTHON_SEARCH_PATH="/usr/bin" # default
# or
export AUTO_VENV_PYTHON_SEARCH_PATH="/opt/python/bin"
cd ~/my-python-project
auto_venv --new
# Choose python3.9
# Creates .venv_python3_9 and .auto_venv
cd ~/my-python-project
auto_venv --new # Create first environment (python3.9)
auto_venv --add # Add python3.11
auto_venv --add # Add python3.12
auto_venv --list # See all environments
auto_venv --switch python3.11 # Switch to Python 3.11
auto_venv --set-default python3.11 # Make it the default
# Test your code with different Python versions
auto_venv --switch python3.9
python -m pytest
auto_venv --switch python3.11
python -m pytest
auto_venv --switch python3.12
python -m pytest
cd ~/project-a
echo "default:/home/user/shared-env" > .auto_venv
echo "shared:/home/user/shared-env" >> .auto_venv
cd ~/project-b
echo "default:/home/user/shared-env" > .auto_venv
echo "shared:/home/user/shared-env" >> .auto_venv
# Both projects now share the same virtual environment
# Setup in any shell
cd ~/my-project
auto_venv --new
# Works seamlessly when switching shells
bash # Enters bash - environment activated automatically
zsh # Switch to zsh - environment still active
fish # Switch to fish - environment remains active
auto_venv
- Show current environment statusauto_venv --new
- Create a new virtual environmentauto_venv --switch <name>
- Switch to a different environmentauto_venv --list
- List all available environmentsauto_venv --set-default <name>
- Set the default environmentauto_venv --validate
- Validate the current environmentauto_venv --help
- Show help message
auto_venv/
├── README.md # This file
├── auto_venv # Universal dispatcher (detects shell and loads appropriate version)
├── auto_venv.sh # Bash/POSIX implementation
├── auto_venv.zsh # Zsh implementation (uses chpwd hook)
├── auto_venv.fish # Fish implementation (uses PWD event)
├── install.sh # Universal installation script
├── uninstall.sh # uninstallation script
└── test
└── test_auto_venv.sh # Test suite for POSIX shells
- The universal dispatcher (
auto_venv
) automatically detects your shell and sources the appropriate implementation - When you change directories (
cd
in Bash,chpwd
hook in Zsh, or PWD change in Fish), the script searches for a.auto_venv
file in the current directory and all parent directories - If found, it parses the file to get available environments
- It selects the appropriate environment (preferred, default, or first available)
- If no virtual environment is currently active, it activates the selected environment
- If a different environment should be active, it switches environments
- If leaving a project directory tree, it deactivates the current environment
- Supported shells: Bash, Zsh, or Fish
- Python: 2.7+ or 3.x
- Python packages:
virtualenv
(for Python 2.7)venv
module (built-in for Python 3.3+)
Unlike other virtual environment managers, auto_venv offers:
- True multi-shell support: Native implementations for Bash, Zsh, and Fish
- Zero learning curve: Just
cd
into your project - Multi-environment per project: Test across Python versions easily
- No dependencies: Pure shell implementation
- Universal interface: Same commands work across all shells
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
- Check that the path in
.auto_venv
is correct - Run
auto_venv --validate
to check environment integrity - Ensure the virtual environment exists and is valid
- Verify that
AUTO_VENV_PYTHON_SEARCH_PATH
is set correctly
- List available versions:
ls $AUTO_VENV_PYTHON_SEARCH_PATH | grep -E '^python[23]\.?[0-9]*$'
- Update
AUTO_VENV_PYTHON_SEARCH_PATH
to point to your Python installation directory
- Use
auto_venv --list
to see existing environments - Use
auto_venv --switch <name>
to activate an existing environment - Environment names must be unique within a project
- Ensure you've reloaded your shell configuration:
- Bash:
source ~/.bashrc
- Zsh:
source ~/.zshrc
- Fish:
source ~/.config/fish/config.fish
- Bash:
- Verify that the script has been loaded:
type auto_venv
- Fish requires direct sourcing of
auto_venv.fish
, not the universal dispatcher - Make sure your
config.fish
contains:source /path/to/auto_venv.fish
- Ensure the auto_venv directory is in a stable location
- Check that the path in
.zshrc
is absolute, not relative
- In Zsh, the
chpwd
hook is called on every directory change - In Fish, the PWD watcher runs on every directory change
- Consider disabling in directories with heavy I/O if needed
This project was developed using AI-assisted programming. See DEVELOPMENT.md for details on the development process and how AI tools were used to accelerate development while maintaining code quality and human oversight.
Current version: 0.3.0-alpha
This project is under active development. Until we reach v1.0:
- ❌ Not production-ready
⚠️ API may change- 🐛 Bugs are expected
- 📝 Documentation may be incomplete
- Fix all known bugs in multi-shell support
- Complete test coverage for all shells
- Stabilize API and configuration format
- Add comprehensive error handling
- Performance optimization
- Full documentation
- Test it and report bugs
- Contribute fixes and improvements
- Share feedback on the API design
- Star the repo if you find it useful
Estimated v1.0 release: When it's ready™