VirtualEnv 从 bash 脚本初始化

发布于 2024-11-27 11:38:10 字数 270 浏览 1 评论 0原文

我正在尝试编写一个超级简单的 bash 脚本。基本上激活一个虚拟环境,然后更改到工作目录。这是我经常做的一项任务,并且只服从一个命令是有意义的。

基本上...

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

但是,当它运行时,它只是转储回外壳,而我仍然在运行脚本的目录中,并且环境未激活。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷清清 2024-12-04 11:38:10

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

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.

夏日落 2024-12-04 11:38:10

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

您尝试做的事情并不是“超级简单”,因为您想覆盖此行为。

查看 exec 以将当前进程替换为您选择的进程。

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

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

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

Look at exec for replacing the current process with the process of your choice.

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

老娘不死你永远是小三 2024-12-04 11:38:10

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

开始即

环境名为“py1”,位于 ~/envs/py1/ 并带有存储库
位置~/proj/py1/

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

结束即

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

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

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

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

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.

靖瑶 2024-12-04 11:38:10

我知道我已经迟到了,但我可以建议使用 virtualenvwrapper 吗?它提供了一个很好的 bash 钩子,看起来完全可以做你想要的事情。

查看本教程: http:// blog.fruiapps.com/2012/06/Python-virtualenv-and-virtualenvwrapper 入门教程

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文