direnv
is an extension for your shell. It augments existing shells with anew feature that can load and unload environment variables depending on thecurrent directory.
Before each prompt, direnv checks for the existence of a .envrc
file in thecurrent and parent directories. If the file exists (and is authorized), it isloaded into a bash sub-shell and all exported variables are then capturedby direnv and then made available to the current shell.
It supports hooks for all the common shells like bash, zsh, tcsh and fish.This allows project-specific environment variables without cluttering the~/.profile
file.
Because direnv is compiled into a single static executable, it is fast enoughto be unnoticeable on each prompt. It is also language-agnostic and can beused to build solutions similar to rbenv, pyenv and phpenv.
Now restart your shell.
To follow along in your shell once direnv is installed.
# Create a new folder for demo purposes.
$ mkdir ~/my-project
$ cd ~/my-project
# Show that the FOO environment variable is not loaded.
$ echo ${FOO-nope}
nope
# Create a new .envrc. This file is bash code that is going to be loaded by
# direnv.
$ echo export FOO=foo > .envrc
.envrc is not allowed
# The security mechanism didn't allow to load the .envrc. Since we trust it,
# let's allow its execution.
$ direnv allow .
direnv: reloading
direnv: loading .envrc
direnv export: +FOO
# Show that the FOO environment variable is loaded.
$ echo ${FOO-nope}
foo
# Exit the project
$ cd ..
direnv: unloading
# And now FOO is unset again
$ echo ${FOO-nope}
nope
Exporting variables by hand is a bit repetitive so direnv provides a set ofutility functions that are made available in the context of the .envrc
file.
As an example, the PATH_add
function is used to expand and prepend a path tothe $PATH environment variable. Instead of export PATH=$PWD/bin:$PATH
youcan write PATH_add bin
. It's shorter and avoids a common mistake where$PATH=bin
.
To find the documentation for all available functions check thedirenv-stdlib(1) man page.
It's also possible to create your own extensions by creating a bash file at~/.config/direnv/direnvrc
or ~/.config/direnv/lib/*.sh
. This file isloaded before your .envrc
and thus allows you to make your own extensions todirenv.
Make sure to take a look at the wiki! It contains all sorts of usefulinformation such as common recipes, editor integration, tips-and-tricks.
Based on GitHub issues interactions, here are the top things that have beenconfusing for users:
direnv has a standard library of functions, a collection of utilities thatI found useful to have and accumulated over the years. You can find ithere: https://github.com/direnv/direnv/blob/master/stdlib.sh
It's possible to override the stdlib with your own set of function byadding a bash file to ~/.config/direnv/direnvrc
. This file is loaded andit's content made available to any .envrc
file.
direnv is not loading the .envrc
into the current shell. It's creating anew bash sub-process to load the stdlib, direnvrc and .envrc
, and onlyexports the environment diff back to the original shell. This allows direnvto record the environment changes accurately and also work with all sortsof shells. It also means that aliases and functions are not exportableright now.
Bug reports, contributions and forks are welcome. All bugs or other forms ofdiscussion happen on http://github.com/direnv/direnv/issues .
Or drop by on Matrix tohave a chat. If you ask a question make sure to stay around as not everyone isactive all day.
Here is a list of projects you might want to look into if you are using direnv.
use_nix
implementation.Here is a list of other projects found in the same design space. Feel free tosubmit new ones.
MIT licence - Copyright (C) 2019 @zimbatm and contributors
python pyenv 介绍 (Introduction) Whether you’re just getting started or you’re a seasoned Python developer, you may have found managing your Python environments to be tedious and painful. Managing Pytho
最近在多个项目之间来回切换,其中不同项目需要使用同一个库的不同版本,来回切换太麻烦,于是乎,想到了曾经有同事给我建议的环境自动加载工具direnv,可以使我在命令行中根据进入的目录不同,自动加载不同的环境配置。下面就看看怎么使用吧。 下载安装 可以从 https://github.com/direnv/direnv 下载自己操作系统的包。 $ wget -c https://github.com/
管理多个相互独立的 Python 虚拟开发环境 最终实现 pyenv:同一台电脑管理多个 Python 版本 pipenv:每一个项目建立项目隔绝的虚拟环境 direnv:进入项目自动激活虚拟环境 Pyenv pyenv 可以改变全局的 Python 版本,安装多个版本的 Python, 设置目录级别的 Python 版本。 安装 Pyenv brew install pyenv 或 pip in