venv/pyvenv 中没有 activate_this.py 文件 [英] No activate_this.py file in venv / pyvenv

查看:34
本文介绍了venv/pyvenv 中没有 activate_this.py 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 python 脚本中启动 venv/pyvenv 并且我知道要运行官方文档:

I need to start venv / pyvenv from within a python script and I know the official documentation is to run:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

但我没有 activate_this.py 文件,而且我找不到任何创建方法.

But I don't have an activate_this.py file and I can't find anywhere how to create one.

我正在运行 python 3.4.1.知道我需要做什么吗?

I am running python 3.4.1. Any idea what I need to do?

推荐答案

正如您所指出的,pyvenv/venv 模块不附带 activate_this.py.但是如果你需要这个功能,你可以从virtualenv借用activate_this.py,放到预期的位置(virtualenv_path/bin/activate_this.py>),然后使用它.它似乎工作正常.唯一的问题是 virtualenv 文档对于 Python 3 来说已经过时了(他们声称您使用了不存在的 execfile).Python 3 兼容的替代方案是:

As you've noted, pyvenv/the venv module doesn't ship with activate_this.py. But if you need this feature, you can borrow activate_this.py from virtualenv, put it in the expected location (virtualenv_path/bin/activate_this.py), then use it. It seems to work fine. Only issue is that the virtualenv docs 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(调整 PATH>sys.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.

这篇关于venv/pyvenv 中没有 activate_this.py 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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