python 共享需要激活 virtualenv 的脚本

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

Sharing scripts that require a virtualenv to be activated

pythonvirtualenv

提问by Mzzzzzz

I have virtualenv and virtualenvwrapper installed on a shared Linux server with default settings (virtualenvs are in ~/.virtualenvs). I have several Python scripts that can only be run when the correct virtualenv is activated.

我在具有默认设置的共享 Linux 服务器上安装了 virtualenv 和 virtualenvwrapper(virtualenvs 在 ~/.virtualenvs 中)。我有几个 Python 脚本,它们只能在激活正确的 virtualenv 时运行。

Now I want to sharethose scripts with other users on the server, but without requiring them to know anything about virtualenv... so they can run python scriptnameor ./scriptnameand the script will run with the libraries available in my virtualenv.

现在我想与服务器上的其他用户共享这些脚本,但不要求他们了解有关 virtualenv 的任何信息……这样他们就可以运行,python scriptname或者./scriptname脚本将与我的 virtualenv 中可用的库一起运行。

What's the cleanest way to do this? I've toyed with a few options (like changing the shebang line to point at the virtualenv provided interpreter), but they seem quite inflexible. Any suggestions?

什么是最干净的方法来做到这一点?我玩过一些选项(比如改变shebang行以指向virtualenv提供的解释器),但它们似乎很不灵活。有什么建议?



Edit:This is a development server where several other people have accounts. However, none of them are Python programmers (I'm currently trying to convert them). I just want to make it easy for them to run these scripts and possibly inspect their logic, without exposing non-Pythonistas to environment details. Thanks.

编辑:这是一个开发服务器,其他几个人都有帐户。但是,他们都不是 Python 程序员(我目前正在尝试转换他们)。我只是想让他们更容易地运行这些脚本并可能检查他们的逻辑,而不会将非 Pythonistas 暴露给环境细节。谢谢。

采纳答案by jcdyer

If it's only on one server, then flexibility is irrelevant. Modify the shebang. If you're worried about that, make a packaged, installed copy on the dev server that doesn't use the virtualenv. Once it's out of develepment, whether that's for local users or users in guatemala, virtualenv is no longer the right tool.

如果它只在一台服务器上,那么灵活性是无关紧要的。修改shebang。如果您对此感到担心,请在不使用 virtualenv 的开发服务器上制作一个打包的、已安装的副本。一旦开发结束,无论是针对本地用户还是危地马拉用户,virtualenv 都不再是正确的工具。

回答by Chris Dukes

Use the following magic(5) at the start of the script.

在脚本开头使用以下魔法(5)。

#!/usr/bin/env python

Change which virtualenv is active and it'll use the python from that virtualenv. Deactivate the virtualenv, it still runs.

更改哪个 virtualenv 处于活动状态,它将使用该 virtualenv 中的 python。停用 virtualenv,它仍然运行。

回答by Heikki Toivonen

I would vote for adding a shebang line in scriptnamepointing to the correct virtualenv python. You just tell your users the full path to scriptname(or put it in their PATH), and they don't even need to know it is a Python script.

我会投票支持添加一条scriptname指向正确的 virtualenv python的 shebang 行。您只需告诉您的用户完整路径scriptname(或将其放在他们的 PATH 中),他们甚至不需要知道它是一个 Python 脚本。

If your users are programmers, then I don't see why you wouldn't want them to know/learn about virtualenv.

如果您的用户是程序员,那么我不明白您为什么不希望他们了解/了解 virtualenv。