How would I run lsvirtualenv or any of the other virtualenvwrapper functions via python script?

≡放荡痞女 提交于 2019-12-12 12:53:10

问题


I'm attempting to run virtualenvwrapper.sh commands (ie: lsvirtualenv and mkvirtualenv).

I attempted to use

subprocess.call(["lsvirtualenv"])

but it does not seem to work. It gives me the following error message:

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

Since these functions are inside of the virtualenvwrapper.sh file, how would I go about referencing the function in a python script?

TIA


回答1:


You need to use source $(which virtualenvwrapper.sh) && <your command> with shell=True.

Example:

>>> 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
============

See the documentation for the Popen Constructor.



来源:https://stackoverflow.com/questions/25024874/how-would-i-run-lsvirtualenv-or-any-of-the-other-virtualenvwrapper-functions-via

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!