如何从python中激活pyvenv vitrualenv? (activate_this.py已删除?) [英] How can I activate a pyvenv vitrualenv from within python? (activate_this.py was removed?)

查看:133
本文介绍了如何从python中激活pyvenv vitrualenv? (activate_this.py已删除?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.4,并且已经创建了pyvenv,我希望从python进程中激活它.在virtualenv中,我曾经使用activate_this.py,但是在pyvenv中似乎消失了.

I'm using Python 3.4, and having created a pyvenv, I'm looking to activate it from within a python process. With virtualenv, I used to use activate_this.py, but that appears to be gone in pyvenv.

现在是否有一种简单的方法可以有效地将当前的解释器更改为virtualenv解释器?我可能会弄乱PATH(activate_this.py所做的事情),但我想使用一种更简单,更稳定的方法.

Is there now an easy way to effectively change the current interpreter to the virtualenv interpreter? I could probably mess around with the PATH (which is what activate_this.py did), but I'd like a simpler and stabler way.

这是在wsgi.py中使用的.

This is for use in a wsgi.py.

推荐答案

pyvenvvenv模块不支持此功能.第三方virtualenv软件包支持使用activate_this.py ,但该功能未包含在内置venv模块中

pyvenv and the venv module don't support this out of the box. The third party virtualenv package does support this using activate_this.py, but that feature was not included in the built-in venv module.

您可以尝试从基于virtualenv的环境借用activate_this.py的副本;似乎可行,尽管我不能发誓它会是完美的(venv/pyvenv在启动过程中使用了一些魔术;尚不清楚是否所有这些都是通过activate_this.py复制的.)

You could try to borrow a copy of activate_this.py from a virtualenv based environment; it seems to work, though I can't swear it will be perfect (venv/pyvenv uses some magic during startup; unclear if all of it is replicated via activate_this.py).

针对它的virtualenv文档在Python 3中已过时(他们声称您使用的是execfile,但不存在).与Python 3兼容的替代方案是:

The virtualenv docs for it are out of date for Python 3 (they claim you use execfile, which doesn't exist). The Python 3 compatible alternative would be:

activator = 'some/path/to/activate_this.py'  # Looted from virtualenv; should not require modification, since it's defined relatively
with open(activator) as f:
    exec(f.read(), {'__file__': activator})

activate_this.py所做的任何事情都不是神奇的事情,因此您可以手动执行相同的更改而不会从virtualenv中抢劫(调整PATHsys.pathsys.prefix等),但是借用使操作变得更加简单这种情况.

Nothing activate_this.py does is magical, so you could manually perform the same changes without looting from virtualenv (adjusting PATH, sys.path, sys.prefix, etc.), but borrowing makes it much simpler in this case.

这篇关于如何从python中激活pyvenv vitrualenv? (activate_this.py已删除?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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