从 bash 脚本初始化的 VirtualEnv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6929024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:30:17  来源:igfitidea点击:

VirtualEnv initilaized from a bash script

pythonbashvirtualenv

提问by Ominus

I am trying to write what should be a super simple bash script. Basically activate a virtual env and than change to the working directory. A task i do a lot and condesing to one command just made sense.

我正在尝试编写一个超级简单的 bash 脚本。基本上激活一个虚拟环境而不是更改到工作目录。我经常做的一项任务并屈从于一个命令是有道理的。

Basically ...

基本上 ...

#!/bin/bash
source /usr/local/turbogears/pps_beta/bin/activate
cd /usr/local/turbogears/pps_beta/src

However when it runs it just dumps back to the shell and i am still in the directory i ran the script from and the environment isn't activated.

然而,当它运行时,它只是转储回 shell,我仍然在我运行脚本的目录中,并且环境没有被激活。

回答by Nicola Musatti

All you need to do is to run your script with the source command. This is because the cd command is local to the shell that runs it. When you run a script directly, a new shell is executed which terminates when it reaches the script's end of file. By using the source command you tell the shell to directly execute the script's instructions.

您需要做的就是使用 source 命令运行您的脚本。这是因为 cd 命令是运行它的 shell 的本地命令。当您直接运行脚本时,会执行一个新的 shell,它在到达脚本的文件末尾时终止。通过使用 source 命令,您可以告诉 shell 直接执行脚本的指令。

回答by tripleee

The value of cdis local to the current script, which ends when you fall off the end of the file.

的值cd是当前脚本的本地值,当您离开文件末尾时结束。

What you are trying to do is not "super simple" because you want to override this behavior.

您要做的不是“超级简单”,因为您想覆盖此行为。

Look at execfor replacing the current process with the process of your choice.

查看exec是否用您选择的流程替换当前流程。

For feeding commands into an interactive Bash, look at the --rcfileoption.

要将命令输入交互式 Bash,请查看--rcfile选项。

回答by eusid

I imagine you wish your script to be dynamic, however, as a quick fix when working on a new system I create an alias.

我想您希望您的脚本是动态的,但是,作为在新系统上工作时的快速修复,我创建了一个别名。

begin i.e

the env is called 'py1' located at ~/envs/py1/ with a repository location at ~/proj/py1/

alias py1='source ~/envs/py1/bin/activate; cd ~/proj/py1/;

end i.e

开始即

env 称为“py1”,位于 ~/envs/py1/,存储库位置位于 ~/proj/py1/

别名 py1='source ~/envs/py1/bin/activate; cd ~/proj/py1/;

结束即

You can now access your project and virtualenv by typing py1 from anywhere in the CLI.

您现在可以通过在 CLI 的任何位置键入 py1 来访问您的项目和 virtualenv。

I know that this is no where near ideal, violates DRY, and many other programming concepts. It is just a quick and dirty way of getting your env and project accessible quickly without having to setup the variables.

我知道这远非理想,违反了 DRY 和许多其他编程概念。这只是一种快速而肮脏的方式,无需设置变量即可快速访问您的环境和项目。

回答by MusikPolice

I know that I'm late to the game here, but may I suggest using virtualenvwrapper? It provides a nice bash hook that appears to do exactly what you want.

我知道我在这里玩游戏迟到了,但我可以建议使用 virtualenvwrapper 吗?它提供了一个很好的 bash 钩子,它似乎完全符合你的要求。

Check out this tutorial: http://blog.fruiapps.com/2012/06/An-introductory-tutorial-to-python-virtualenv-and-virtualenvwrapper

查看本教程:http: //blog.fruiapps.com/2012/06/An-introductory-tutorial-to-python-virtualenv-and-virtualenvwrapper