在venv/pyvenv中没有activate_this.py文件

cli*_*ray 16 python virtualenv

我需要从python脚本中启动venv/pyvenv,我知道正式文档要运行:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
Run Code Online (Sandbox Code Playgroud)

但我没有activate_this.py文件,我无法找到任何地方如何创建一个.

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

Sha*_*ger 15

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

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})
Run Code Online (Sandbox Code Playgroud)

没有什么activate_this.py确实是神奇的,所以你可以手动执行相同的变化,而不从掠夺virtualenv(调整PATH,sys.path,sys.prefix,等),但借款使得它在这种情况下要简单得多.

  • 当我读到这个答案时,我真的吐了我的饮料:).但它完全有效!如果这就是它的全部内容,为什么不仅仅是python venv的一部分呢? (2认同)