python - 如何在 Python 脚本中使用 virtualenvwrapper

标签 python bash virtualenv pip virtualenvwrapper

<分区>

我已经在我的系统上成功安装了 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

最佳答案

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


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

示例:

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

关于python - 如何在 Python 脚本中使用 virtualenvwrapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19963633/

相关文章:

python:两个时间日期字符串的区别

python - 我可以更改 python 子类中的基类值吗?

java - 如何切换到窗口身份验证弹出窗口并输入凭据?

python - Mongo UUID python vs java 格式

python - 如何在虚拟环境中使用 pip

python - 修改大型cython项目的工作流程

linux - 如何为我的 shell 脚本制作手册页?

linux - 删除了仍在重定向输出的文件

bash - 在 bash 中如何将 multimap<K,V> 转换为 <K, {V1,V2}> 的 map

python - 重新安装操作系统后使用 virtualenv 在 PyCharm 项目中“无法设置 Python SDK”