我有安赛任务
- name: Install setuptools in virtual environment
pip:
name: setuptools-git
virtualenv: "myenv"
virtualenv_command: "/root/.pyenv/bin/pyenv virtualenv 2.7.13"
但是它给出了错误
fatal: [localhost]: FAILED! => {
"changed": false,
"invocation": {
"module_args": {
"chdir": null,
"editable": false,
"executable": null,
"extra_args": "",
"name": [
"setuptools-git"
],
"requirements": null,
"state": "present",
"umask": null,
"use_mirrors": true,
"version": null,
"virtualenv": "myenv",
"virtualenv_command": "/root/.pyenv/bin/pyenv virtualenv 2.7.13",
"virtualenv_python": null,
"virtualenv_site_packages": false
}
},
"msg": "Unable to find pip in the virtualenv, myenv, under any of these names: pip2, pip. Make sure pip is present in the virtualenv."
}
当我检查virtualenv中的pip文件时,它已经存在
# ls -alh /root/.pyenv/versions/myenv/bin/pip
-rwxr-xr-x 1 root root 243 Jan 16 17:40 /root/.pyenv/versions/myenv/bin/pip
有两个相同名称的virtualenvs
# /root/.pyenv/bin/pyenv virtualenvs
2.7.13/envs/myenv (created from /root/.pyenv/versions/2.7.13)
myenv (created from /root/.pyenv/versions/2.7.13)
我无法将executable
与virtualenv
变量一起使用。
答案 0 :(得分:1)
我找到了一个解决方法,可将带有pip的软件包安装到pyenv管理的virtualenv中。它确实避免使用pip模块,而只是执行一些shell命令。我提供了一些我使用过的变量,一个我创建的 .pyenvrc 文件,以及完成shell魔术的ansible任务。
变量:
# Installation paths
pyenv_root: "{{ ansible_env.HOME }}/.pyenv"
pyenv_rc: "{{ pyenv_root }}/.pyenvrc"
# Whatever your virtualenv is named
pyenv_venv_name: "foo_virtualenv"
project_dir: /path/to/your/project
任务:
- name: Pip - install requirements using shell
shell: |
# cd to project directory
cd {{ project_dir }}
# Check to see if we are already inside a virtualenv
if ! [[ ${VIRTUAL_ENV} ]]; then
# Load pyenv into the shell
source {{ pyenv_rc }}
# Activate the virtualenv
pyenv activate {{ pyenv_venv_name }}
fi
# Install python requirements
pip install -r requirements.txt
args:
executable: /bin/bash
register: pip_script_result
.emenvrc 的内容:
# Add pyenv into path if installed into default location
export PYENV_ROOT="{{ pyenv_root }}"
export PATH="${PYENV_ROOT}/bin:${PATH}"
# Initialise pyenv and pyenv-virtualenv if installed
if [[ -d $HOME/.pyenv ]];then
eval "$(pyenv init -)"
if [ -d "${PYENV_ROOT}/plugins/pyenv-virtualenv" ]; then
eval "$(pyenv virtualenv-init -)"
fi
fi
# Disable prompt changing
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
在上面的示例中,我使用pip
将 requirements.txt 的内容安装到了pyenv托管的virtualenv中。您应该能够适应pip install
行以执行所需的任何操作。
答案 1 :(得分:1)
基本上对于virtualenv
属性,您必须传递虚拟环境路径。请参见下面的示例。
- hosts: 1.2.3.4
user: remoteuser
environment:
PYENV_ROOT: /home/<user home dir>/.pyenv
PYENV_VERSION: myenv
PYENV_SHELL: bash
PYENV_ACTIVATE_SHELL: 1
tasks:
- pip:
name: setuptools-git
virtualenv: "/home/<user home dir>/.pyenv/versions/myenv"
virtualenv_command: /home/<user home dir>/.pyenv/bin/pyenv virtualenv 3.4.0 myenv
我希望这能解决您的问题。