bash 执行 bashscript 时如何进入 Python virtualenv?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40894631/
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 15:29:15  来源:igfitidea点击:

How does one enter a Python virtualenv when executing a bashscript?

pythonbashvirtualenv

提问by ShanZhengYang

If one defines which version of python to use in a bash script, it would be

如果定义在 bash 脚本中使用哪个版本的 python,它将是

export PYTHON = "/path/python/python-3.5.1/bin/python"

But for Python virtualenv's, one executes these commands in the command line

但是对于 Python virtualenv,在命令行中执行这些命令

cd /path/pathto/virtualenv
source activate
cd another_directory

How does one "enter" a Python virtualenv in a bash script? What is the standard approach here?

如何在 bash 脚本中“输入”Python virtualenv?这里的标准方法是什么?

回答by das-g

We have to distinguish two cases here:

这里我们必须区分两种情况:

  1. You want to use/call python (or python-based tools) in your bash script, but python or those tools should be taken from and run in a virtualenv
  2. You want a script that, amongst other things, lets the shell from which you call it enter the virtualenv, so that you can interactively call python (or python-based tools) inside the virtualenv
  1. 您想在 bash 脚本中使用/调用 python(或基于 python 的工具),但应从 virtualenv 中获取并运行 python 或那些工具
  2. 您需要一个脚本,除其他外,让您从中调用它的 shell 进入 virtualenv,以便您可以在 virtualenv 中交互调用 python(或基于 python 的工具)

Case 1: Using a virtualenv inside a script

案例 1:在脚本中使用 virtualenv

How does one "enter" a Python virtualenv in a bash script?

如何在 bash 脚本中“输入”Python virtualenv?

Just like on the interactive bash command line:

就像在交互式 bash 命令行上一样:

source /path/to/the/virtual_env/bin/activate

What is the standard approach here?

这里的标准方法是什么?

The standard approach is notto enterthe virtualenv in a bash script. Instead, call python and/or the python-based commands you want to use by their full path. To make this easier and less repetitive, you can use aliases and variables.

标准的做法是不是进入一个bash脚本中的virtualenv。相反,请按完整路径调用 python 和/或要使用的基于 python 的命令。为了使这更容易和减少重复,您可以使用别名和变量。

Case 2: Activating a virtualenv in an interactive bash session by calling a script

案例 2:通过调用脚本在交互式 bash 会话中激活 virtualenv

There already is such a script. It's called activateand it's located in the bindirectory of the virtualenv. You have to sourceit rather than calling it like a normal command. Only then will it run in the same session instead of in a subshell, and thus only then can it make modifications to the session that won't be lost due to the subshell terminating at the end of the script.

已经有这样的脚本了。它被调用activate并且位于binvirtualenv的目录中。你必须这样做source,而不是像普通命令一样调用它。只有这样它才会在同一个会话中运行,而不是在子 shell 中运行,因此只有这样它才能对会话进行修改,而不会因子 shell 在脚本末尾终止而丢失。

So just do:

所以只需这样做:

source /path/to/the/virtual_env/bin/activate

in your interactive shell session.

在您的交互式 shell 会话中。

But what if you want to do more than the activatescript does? You can put

但是,如果您想做的比activate脚本做的更多呢?你可以放

source /path/to/the/virtual_env/bin/activate

into a shell script. But, due to the reason mentioned above, it won't have much effect when you call your script normally. Instead, sourceyour script to use it from an interactive session.

进入shell脚本。但是,由于上述原因,当您正常调用脚本时,它不会产生太大影响。相反,source您的脚本从交互式会话中使用它。

Thus:

因此:

Content of my_activate.sh

内容 my_activate.sh

#!/bin/bash

# Do something
# ...

# then
source /path/to/the/virtual_env/bin/activate

# Do more stuff
# ...

and in your interactive session

并在您的交互式会话中

source my_activate.sh

回答by Steve

I recommend using virtualenvwrapper. It provides some useful tools for managing your virtual environments.

我建议使用 virtualenvwrapper。它提供了一些有用的工具来管理您的虚拟环境。

pip install --user virtualenvwrapper

When you create the virtual environment, you specify which version of python should be used in the environment.

创建虚拟环境时,您指定环境中应使用哪个版本的python。

mkvirtualenv  -p /usr/local/bin/python2.6  myproject.2.6
mkvirtualenv  -p /usr/local/bin/python3.3  myproject.3.3

Then, "enter" the environment with the workon command.

然后,使用 workon 命令“进入”环境。

workon myproject.2.6

回答by Rakesh Kumar

Here are few steps to follow, one thing you can do is

以下是要遵循的几个步骤,您可以做的一件事是

export PYTHON = "/path/pathto/virtualenv/python"

Use this path in bashrc to use. Or you can do something like:-

在bashrc中使用这个路径来使用。或者您可以执行以下操作:-

vim ~/.bashrc 

Go to end and set

转到最后并设置

alias python=/path/pathto/virtualenv/python
source ~/.bashrc