我如何通过python脚本运行lsvirtualenv或任何其他virtualenvwrapper函数?

时间:2014-07-29 20:58:00

标签: python python-2.7 virtualenv virtualenvwrapper

我试图运行virtualenvwrapper.sh命令(即:lsvirtualenv和mkvirtualenv)。

我试图使用

subprocess.call(["lsvirtualenv"])

但它似乎不起作用。它给我以下错误消息:

Traceback (most recent call last):
  File "importMaster.py", line 6, in <module>
    virtualEnvs = subprocess.call(["lsvirtualenv"])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

由于这些函数在virtualenvwrapper.sh文件中,我如何在python脚本中引用该函数?

TIA

1 个答案:

答案 0 :(得分:3)

您需要将source $(which virtualenvwrapper.sh) && <your command>shell=True一起使用。

示例:

>>> from __future__ import print_function
>>> from subprocess import Popen, PIPE
>>> p = Popen("source $(which virtualenvwrapper.sh) && lsvirtualenv", shell=True, stdout=PIPE)
>>> print(p.stdout.read())
10leds
======


ambientlight
============

请参阅Popen Constructor的文档。