使用 shell 脚本激活 VirtualEnv 似乎不起作用 [英] Activating a VirtualEnv using a shell script doesn't seem to work

查看:49
本文介绍了使用 shell 脚本激活 VirtualEnv 似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过如下所示的 shell 脚本激活 VirtualEnv,但它似乎不起作用,

#!/bin/sh源 ~/.virtualenvs/pinax-env/bin/activate

我收到以下错误

$ sh virtualenv_activate.shvirtualenv_activate.sh: 2: 源: 未找到

但是如果我在终端上输入相同的命令它似乎可以工作

$ source ~/.virtualenvs/pinax-env/bin/activate(pinax-env)gautam@Aspirebuntu:$

所以我把shell脚本改成了

#!/bin/bash源 ~/.virtualenvs/pinax-env/bin/activate

按照建议和使用

$ bash virtualenv_activate.shgautam@Aspirebuntu:$

运行脚本.

这不会引发错误,但也不会激活虚拟环境

那么对于如何解决这个问题有什么建议吗?

PS:我使用的是 Ubuntu 11.04

解决方案

TLDR

必须使用 source 而不是单独的脚本运行 .sh 脚本

source your-script.sh

而不是你的脚本.sh

详情

sh 与 bash 不同(尽管有些系统只是将 sh 链接到 bash,因此运行 sh 实际上运行的是 bash).您可以将 sh 视为 bash 的淡化版本.bash 具有而 sh 没有的一件事是源"命令.这就是您收到该错误的原因... source 在您的 bash shell 中运行良好.但是,当您使用 sh 启动脚本时,您将在子进程的 shell 中运行该脚本.由于该脚本在 sh 中运行,因此找不到源".

解决方案是在 bash 中运行脚本.将第一行更改为...

#!/bin/bash

然后运行...

./virtualenv_activate.sh

...或...

/bin/bash virtualenv_activate.sh

如果要激活 virtualenv 以更改从中调用脚本的 shell,则需要使用源"或点运算符".这确保脚本在当前 shell 中运行(并因此更改当前环境)...

source virtualenv_activate.sh

...或...

<预><代码>.virtualenv_activate.sh

作为旁注,这就是为什么 virtualenv 总是说您需要使用源"来运行它的激活脚本. 

I tried activating a VirtualEnv through a shell script like the one below but it doesn't seem to work,

#!/bin/sh
source ~/.virtualenvs/pinax-env/bin/activate

I get the following error

$ sh virtualenv_activate.sh 
virtualenv_activate.sh: 2: source: not found

but if I enter the same command on terminal it seems to work

$ source ~/.virtualenvs/pinax-env/bin/activate
(pinax-env)gautam@Aspirebuntu:$

So I changed the shell script to

#!/bin/bash
source ~/.virtualenvs/pinax-env/bin/activate

as suggested and used

$ bash virtualenv_activate.sh 
gautam@Aspirebuntu:$

to run the script .

That doesn't throw an error but neither does that activate the virtual env

So any suggestion on how to solve this problem ?

PS : I am using Ubuntu 11.04

解决方案

TLDR

Must run the .sh script with source instead of the script solely

source your-script.sh

and not your-script.sh

Details

sh is not the same as bash (although some systems simply link sh to bash, so running sh actually runs bash). You can think of sh as a watered down version of bash. One thing that bash has that sh does not is the "source" command. This is why you're getting that error... source runs fine in your bash shell. But when you start your script using sh, you run the script in an shell in a subprocess. Since that script is running in sh, "source" is not found.

The solution is to run the script in bash instead. Change the first line to...

#!/bin/bash

Then run with...

./virtualenv_activate.sh

...or...

/bin/bash virtualenv_activate.sh

Edit:

If you want the activation of the virtualenv to change the shell that you call the script from, you need to use the "source" or "dot operator". This ensures that the script is run in the current shell (and therefore changes the current environment)...

source virtualenv_activate.sh

...or...

. virtualenv_activate.sh

As a side note, this is why virtualenv always says you need to use "source" to run it's activate script.  

这篇关于使用 shell 脚本激活 VirtualEnv 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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