【问题标题】:How to use virtualenvwrapper from within a Python script [duplicate]如何在 Python 脚本中使用 virtualenvwrapper [重复]
【发布时间】:2013-11-26 15:15:36
【问题描述】:

我已经在我的系统上成功安装了virtualenvvirtualenvwrapper。它运行良好。为了将来方便,我想创建一个脚本,将我喜欢的所有模块安装到我命名为pynumeric 的环境中,该环境位于~/.virtualenvs。对于这个脚本,我需要在我的脚本中使用virtualenvwrapper。不幸的是,这对我不起作用。我不断收到 workonmkvirtualenv 不存在的错误。

所以基本上我的问题归结为:为什么virtualenvwrapper 可以在终端中工作,而不能在我的 Python 脚本中工作?

install_pynumeric.py(编辑)

#!/usr/bin/python

import os
import sys
import subprocess as sp

# Set CPU frequency governer to performance
#sp.check_call('cpuset gov performance', shell=True)

# Check for parent directory of virtual environments
if not os.path.isdir('/home/carlos/.virtualenvs'):
    sys.exit('The parent directory for virtual environments does not exist yet. Create it before preceeding.')

# Create virtual environment pynumeric if it does not exist yet and activate
if not os.path.isdir('/home/carlos/.virtualenvs/pynumeric'):
    sp.check_call('mkvirtualenv pynumeric', shell=True)

# Activate pynumeric
sp.check_call('workon pynumeric', shell=True)

# List of Python modules to be installed
modules = ['numpy',
        'scipy',
        'sympy',
        'matplotlib',
        'pyqt4',
        'sphinx',
        'rope',
        'pyflakes',
        'ipython',
        'pylint',
        'psutil',
        'spyder',
        'pydstool'
        ]

# Install modules
for m in modules:
    cmd = 'pip install ' + m
    sp.check_call(cmd, shell=True)

# Deactivate pynumeric
sp.check_call('deactivate', shell=True)

# Set CPU frequency governer to ondemand
#sp.check_call('cpuset gov ondemand', shell=True)

我现在收到以下错误:

/bin/sh: mkvirtualenv: command not found
Traceback (most recent call last):
File "./install_pynumeric.py", line 16, in <module>
    sp.check_call('mkvirtualenv pynumeric', shell=True)
File "/usr/lib/python2.7/subprocess.py", line 542, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'mkvirtualenv pynumeric' returned non-zero exit status 127

【问题讨论】:

    标签: python bash virtualenv pip virtualenvwrapper


    【解决方案1】:

    编辑:不起作用,因为deactivate 是一个函数而不是脚本。


    前段时间我也遇到过同样的问题,建议您使用subprocess 运行命令,并将shell 参数设置为True。这将使用您当前的环境,而不是创建一个全新的环境。

    例子:

    from subprocess import check_call
    check_call("deactivate", shell=True)
    

    【讨论】:

    • 否不起作用。我已经用您的更改和相应的错误消息编辑了我的帖子。
    • 哈,该死的。 deactivate 实际上是一个 Bash 函数。好吧,我今天学到了一些东西!
    • 刚刚注意到可能的重复,但答案在我的机器上不起作用(Ubuntu 12.04 LTS)。我现在对解决方案有点兴趣;)
    • 该解决方案也不适合我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多