How to automatically activate virtualenvs when cd'ing into a directory

后端 未结 12 1990
北海茫月
北海茫月 2020-12-24 06:27

I have a bunch of projects in my ~/Documents. I work almost exclusively in python, so these are basically all python projects. Each one, e.g. ~/Documents/

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 06:52

    Rather than writing a custom script you could use direnv. It's not a zsh specific solution (for that you could try zsh-autoenv), but is well-maintained and easy to use with zsh. Once you've installed it, you'd want to put eval "$(direnv hook zsh)" at the end of your .zshrc. At that point you can do:

    $ source ~/.zshrc
    $ cd foo
    $ echo "layout python" > .envrc
    direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
    $ direnv allow
    direnv: loading .envrc
    direnv: export +VIRTUAL_ENV ~PATH
    

    Now you should be in your virtualenv. You can test by running pip freeze to see that your virtualenv specific packages are installed. To deactivate

    $ cd ..
    direnv: unloading
    

提交回复
热议问题