【问题标题】:How would I run lsvirtualenv or any of the other virtualenvwrapper functions via python script?我将如何通过 python 脚本运行 lsvirtualenv 或任何其他 virtualenvwrapper 函数?
【发布时间】:2014-07-29 20:58:00
【问题描述】:

我正在尝试运行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

【问题讨论】:

    标签: python python-2.7 virtualenv virtualenvwrapper


    【解决方案1】:

    您需要将source $(which virtualenvwrapper.sh) &amp;&amp; &lt;your command&gt;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 的文档。

    【讨论】:

    • Popen 没有被弃用吗?我认为 subprocess 旨在替换 Popen 和其他几个旧模块
    • @Edasaur 你在想os.popen 和朋友们。
    • sh 模块这样的子流程更高级别的包装器是我现在最喜欢的路线。
    • 哦。那么还有另一个 popen 构造函数吗?哦,这很有意义!谢谢!我会尝试一下,如果它不起作用,请回复您。
    • @PauloScardine 我最喜欢的是 Fabric :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 2019-12-12
    • 1970-01-01
    相关资源
    最近更新 更多