Skip to content

Automatically activate and deactivate Python environments as you move around the file system.

License

Notifications You must be signed in to change notification settings

hsaunders1904/pyautoenv

Repository files navigation

pyautoenv

Build Status codecov

Automatically activate and deactivate Python environments as you move around the file system.

Description

Heavily inspired by autoenv. pyautoenv activates a Poetry or venv Python environment when you cd into the directory that defines that environment (i.e., when a directory, or any of its parents, contains a poetry.lock file or a .venv/ directory). Environments are automatically deactivated when you leave the directory.

Supports Python versions 3.8 and up.

Install

Follow the installation instructions for your favourite shell.

Zsh

Expand instructions

If you're using oh-my-zsh, clone this repo into ~/.oh-my-zsh/plugins or ${ZSH_CUSTOM}/plugins. Then add pyautoenv to the list of enabled plugins in your .zshrc:

plugins=(
    pyautoenv
)

If you're not using oh-my-zsh, source the pyautoenv.plugin.zsh script.

source pyauotenv.plugin.zsh

Add this to your .zshrc to activate the application permanently.

Bash

Expand instructions

To enable the application in bash, source the bash script.

source <path to pyauotenv>/pyautoenv.bash

Add this to your .bashrc to activate the application permanently.

Note that this script will clobber the cd command.

Fish

Expand instructions

To enable the application in fish-shell, source the fish script.

source <path to pyauotenv>/pyautoenv.fish

Add this to your config.fish file to activate the application permanently.

PowerShell

Expand instructions

To enable the application in PowerShell, dot the .ps1 file.

. <path to pyauotenv>\PyAutoEnv.ps1

Add this to your profile to activate the application permanently.

Note that this application hooks into and re-aliases cd. Therefore pyautoenv does not support the use of Set-Location.

Options

There are some environment variables you can set to configure pyautoenv.

  • PYAUTOENV_DISABLE: Set to a non-zero value to disable all functionality.
  • PYAUTOENV_VENV_NAME: If you name your virtualenv directories something other than .venv, you can use this to override directory names to search within. Use ; as a delimiter to separate directory names. For example, if set to .venv;venv, on each directory change, pyautoenv will look for an environment within .venv, if that directory does not exist, it will look for an environment in venv.
  • PYAUTOENV_IGNORE_DIR: If you wish to disable pyautoenv for a specific set of directories, you can list these directories here, separated with a ;. The directories, and their children, will be treated as though no virtual environment exists for them. This means any active environment will be deactivated when changing to them.