水银更新钩不激活Python的虚拟环境 [英] Mercurial update hook not activating Python virtual environment

查看:209
本文介绍了水银更新钩不激活Python的虚拟环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要随时随地执行汞柱更新bash脚本出现。此bash脚本的目标是要切换到正确的virtualenv。为了简便起见,这个脚本叫做 .TEST - 如下:

I have a bash script that I'm trying to execute anytime an hg update occurs. The goal of this bash script is to switch to the correct virtualenv. For the sake of simplicity, this script is called .test - shown as follows:

#!/bin/bash
echo 'testing hg update hook'
source ~/.virtualenvs/myvirtualenv/bin/activate

每当我使用源.TEST everythying调用这个脚本我的壳正常工作;我可以看到回声的结果和我的壳发生变化以反映激活的virtualenv

Whenever I call this script from my shell using source .test everythying works properly; I can see the results of echo and my shell changes to reflect the activated virtualenv

然而,当我做了一个汞更新时,virtualenv中没有被激活。该脚本射击,我可以看到回声效果;然而,我的壳不更新,以反映激活的virtualenv。下面是我在 .hg / hgrc 文件钩设置如下。任何想法,为什么我的virtualenv不被在此挂钩激活?

However,when I do an hg update, the virtualenv is not being activated. The script is firing as I can see the echo result; however, my shell is not updated to reflect the activated virtualenv. Below is the hook setup in my .hg/hgrc file is below. Any ideas why my virtualenv isn't being activated in this hook?

[hooks]
# Update to the correct virtualenv when switching branches (hg update branchname)
update = source .test

更新1:每本<一个href=\"http://stackoverflow.com/questions/7369145/activating-a-virtualenv-using-a-shell-script-doesnt-seem-to-work\">answer,我不相信汞更新钩在我目前的炮弹发射;这就是为什么当我手动运行该脚本的virtualenv激活,但未能从钩

UPDATE 1: Per this answer, I don't believe the hg update hook is firing in my current shell; which is why the virtualenv activates when I run the script manually but fails from the hook

推荐答案

您的问题是,当你调用一个shell脚本,环境变量的任何变化都不会被导出到调用shell(因此你需要调用源激活从周围的外壳)。

Your problem is that when you invoke a shell script, any changes to the environment variables do not get exported to the calling shell (hence why you need to call source activate from the surrounding shell).

好消息是,你不严格的需求的调用激活为了访问虚拟环境。什么激活会做的是:

The good news is that you don't strictly need to call activate in order to access a virtual environment. What activate will do is:


  1. 的virtualenv中的目录中加入 $ PATH

  2. VIRTUAL_ENV 环境变量。

  3. 修改您的提示。

  1. Add the virtualenv's bin directory to $PATH.
  2. Set the VIRTUAL_ENV environment variable.
  3. Modify your prompt.

这一切都不是为了使用的virtualenv必要的,你可以在virtualenv中完全不使用脚本执行蟒蛇二进制;提示可能是不相关的你的使用情况,您可以通过符号链接把它添加的目录(或只是蟒蛇可执行文件)到你的路径,你需要在 VIRTUAL_ENV 仅适用于因某种原因需要知道它在运行virtualenv中的软件环境变量,如果有必要,你可以从 sys.executable <看着办吧/ code>。例如:

None of this is necessary in order to use the virtualenv, and you can execute the python binary in the virtualenv without ever using the script; the prompt is likely not relevant for your use case, you can add the directory (or just the python executable) to your path by symlinking it, and you need the VIRTUAL_ENV environment variable only for software that for some reason needs to be aware of the virtualenv it's running in. If necessary, you can figure it out from sys.executable. For example:

import sys, os

def find_venv():
  python = sys.executable
  for i in xrange(10):
    if not os.path.islink(python):
      break
    python = os.path.realpath(python)
  return os.path.dirname(os.path.dirname(python))

if not os.environ.has_key("VIRTUAL_ENV"):
  os.environ["VIRTUAL_ENV"] = find_venv()

这篇关于水银更新钩不激活Python的虚拟环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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