autoenv 甚至在子文件夹中执行 [英] autoenv executes even in subfolder

查看:23
本文介绍了autoenv 甚至在子文件夹中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 autoenv 进行自动 virtualenv 激活.Python 项目的顶层文件夹有 .env 文件,内容如下

I uses autoenv for automatic virtualenv activate. Python project's top folder has .env file with following contents

source venv/bin/activate

每当cd 到项目的任何子文件夹时,都会执行此命令.然后抛出

This command executed whenever cd to any sub folder of the project. Then throws

-bash: venv/bin/activate: No such file or directory

它失败是因为它试图相对于子文件夹执行 activate.为什么它甚至在子文件夹中执行?如何解决问题?

It failed because it is trying to execute activate relative to sub folder. Why it executes even in subfolder? How to resolve the issue?

推荐答案

今天遇到了这个问题.当前的答案没有解决这样一个事实,即每次您 cd 进入子文件夹或返回根文件夹时都会激活环境.使用以下 .env 脚本解决它:

Had this issue today. The current answer doesn't address the fact that the environment is activated every time you cd into a subfolder or back to the root folder. Solved it with the following .env script:

venv=venv
currentvenv=""

if [[ $VIRTUAL_ENV != "" ]]
then
  # Strip out the path and just leave the env name
  currentvenv="${VIRTUAL_ENV##*/}"
fi

if [[ "$currentvenv" != "$venv" ]]
then
  echo "Switching to environment: $venv"
  workon $venv
#else
#  echo "Already on environment $venv"
fi

venv 替换为您的环境名称.您可以取消对 else 块的注释,以查看它不会每次都尝试激活环境,前提是所需的环境已经激活.

Replace venv with the name of your environment. You can uncomment the else block to see that it doesn't try to activate the environment every time, given that the desired environment is already activated.

注意:如果您没有使用 virtualenvwrapper,那么您应该将 workon 命令替换为您用来激活虚拟环境的任何命令.不过,我确实建议使用 virtualenvwrapper.

Note: If you're not using virtualenvwrapper then you should replace the workon command with whatever command you're using to activate your virtual environment. I do recommend using virtualenvwrapper though.

这篇关于autoenv 甚至在子文件夹中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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