未分类
创建虚拟环境: Ansible, Python3, and Virtualenvs on CentOS and RHEL and MacOS
要为ansible运行创建一个最新的虚拟环境, 以下是步骤: 下载最新的python3.8.3安装包:
1 |
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz |
解压:
1 |
xz -d Python-3.9.0.tar.xz && tar xvf Python-3.9.0.tar |
创建虚拟环境目录: mkdir /root/.python_env 编译安装python
1 2 |
cd Python-3.9.0/ ./configure --prefix=/root/.python_env && make && make install |
配置一下pip国内源, 以增加安装速度
1 2 3 4 5 6 7 8 9 |
cat > $HOME/.config/pip/pip.conf << EOF [global] index-url = http://mirrors.aliyun.com/pypi/simple/ trusted-host = mirrors.aliyun.com #proxy=http://xxx.xxx.xxx.xxx:8080 # 替换出自己的代理地址,格式为[user:passwd@]proxy.server:port [install] trusted-host=mirrors.aliyun.com EOF |
安装virtualenvwrapper
1 2 3 4 |
cd .python_env/bin/ ln -s python3 python ln -s pip3 pip ./pip install virtualenvwrapper |
安装过程中报错: ImportError: No module named ‘_ctypes’, 解决方法是(参考https://stackoverflow.com/questions/27022373/python3-importerror-no-module-named-ctypes-when-using-value-from-module-mul):
1 2 3 |
sudo yum -y install gcc gcc-c++ sudo yum -y install zlib zlib-devel sudo yum -y install libffi-devel |
然后重新编译安装python3 定义环境变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
cat >> /root/.bashrc << EOF # virtualenv #export PIP_REQUIRE_VIRTUALENV=true #pip安装东西的时候不安装到本地环境 #export PIP_RESPECT_VIRTUALENV=true #在执行pip的时候让系统自动开启虚拟环境 # virtualenv export WORKON_HOME=/root/.python_env/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/root/.python_env/bin/python export PIP_VIRTUALENV_BASE=$WORKON_HOME export VIRTUALENV_USE_DISTRIBUTE=1 export PATH="$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/.python_env/bin" export VIRTUALENVWRAPPER_VIRTUALENV=/root/.python_env/bin/virtualenv #export VIRTUALENVWRAPPER_VIRTUALENV=$(which virtualenv) source $(which virtualenvwrapper.sh) EOF |
创建ansible虚拟环境:
1 |
mkvirtualenv ansible |
The prompt change tells us we’ve successfully made, and activated, our first python 3 virtualenv. Validate it is configured as Read more…