在输入/离开目录时激活/停用conda virtualenvs [英] Activate/deactivate conda virtualenvs on entering/leaving directories

查看:157
本文介绍了在输入/离开目录时激活/停用conda virtualenvs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pyenv-virtualenv 提供了一种在环境中激活环境的好方法进入或离开包含.python-version文本文件的目录的瞬间,该文件指定了要激活的环境.它适用于其中的目录以及其中包含的所有目录.

pyenv-virtualenv offers a nice way of activating the environment on the very instant of entering or leaving the directory which contains a .python-version text file which specifies the environment to activate. It works for the directory it is in and all directories contained in it.

将目录更改为目录上方的内容后,环境将被停用.这样可以使用不同的python版本(仅通过更改目录)轻松地在项目或分析之间切换.

The environment is deactivated once we change the directory to something above it. This allows to easily switch between projects or analyses using different python versions (just by changing the directories).

有没有一种方法可以通过(ana)conda实现相同的行为?

Is there a way of achieving the same behaviour with (ana)conda?

添加了bash标记,因为-据我了解-pyenv通过将自定义脚本挂接到.bashrc(允许它监视目录更改)来实现此目的.如果conda中没有内置方法,如何创建一个脚本使之成为可能?

added bash tag, because - as far as I understand - pyenv achieves this by hooking a custom script into .bashrc (which allows it to monitor the directory changes). If there is no build-in way in conda, how to create a script which would make it possible?

推荐答案

如我的评论中所述,当前不支持此功能.但是, conda的GitHub 上存在一个公开问题,要求此功能.

As mentioned in my comment, this is currently not supported. There is however an open issue on conda's GitHub asking for this feature.

同时,您可以使用 autoenv ,这是一个小型工具,可以自动在其中运行代码进入目录时为.env文件,离开目录时为.env.leave文件(支持bash/zsh以及其他两个文件).

In the meantime you could use autoenv, a small tool that'll automatically run the code in a .env file when entering a directory and that in a .env.leave when leaving the directory (supports bash/zsh and a couple others).

摘自自述文件的一个简单示例,很好地说明了该功能:

A simple example taken from their readme which illustrates the feature quite nicely:

$ echo "echo 'whoa'" > project/.env
$ cd project
whoa

要加载conda环境,您的.env看起来应该像这样:

To load a conda environment your .env would simply look like this:

conda activate <my_env>

注意1:在开始使用之前,请先阅读其GitHub自述文件的 Configuration 部分.

Note 1: Check out the Configuration section of their GitHub readme before you start using it.

注2: autoenv 的作者实际上建议尝试尝试 direnv .但是,我从未使用过它,因此无法对其进行评论.

Note 2: The author of autoenv actually suggests trying direnv instead. However I've never used it, so I can't comment on it.

来自 autoenv 的自述文件:

您可能应该改用direnv.简而言之,它是更高质量的软件.但是,autoenv仍然很棒.也许两者都尝试? :)

you should probably use direnv instead. Simply put, it is higher quality software. But, autoenv is still great, too. Maybe try both? :)

这篇关于在输入/离开目录时激活/停用conda virtualenvs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆