要为ansible运行创建一个最新的虚拟环境, 以下是步骤:
- 下载最新的python3.8.3安装包:
1wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz - 解压:
1xz -d Python-3.9.0.tar.xz && tar xvf Python-3.9.0.tar - 创建虚拟环境目录: mkdir /root/.python_env
- 编译安装python
12cd Python-3.9.0/./configure --prefix=/root/.python_env && make && make install - 配置一下pip国内源, 以增加安装速度
123456789cat > $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.comEOF - 安装virtualenvwrapper
1234cd .python_env/bin/ln -s python3 pythonln -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):
123sudo yum -y install gcc gcc-c++sudo yum -y install zlib zlib-develsudo yum -y install libffi-devel
然后重新编译安装python3 - 定义环境变量
1234567891011121314cat >> /root/.bashrc << EOF# virtualenv#export PIP_REQUIRE_VIRTUALENV=true #pip安装东西的时候不安装到本地环境#export PIP_RESPECT_VIRTUALENV=true #在执行pip的时候让系统自动开启虚拟环境# virtualenvexport WORKON_HOME=/root/.python_env/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=/root/.python_env/bin/pythonexport PIP_VIRTUALENV_BASE=$WORKON_HOMEexport VIRTUALENV_USE_DISTRIBUTE=1export 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虚拟环境:
1mkvirtualenv ansible
- The prompt change tells us we’ve successfully made, and activated, our first python 3 virtualenv. Validate it is configured as expected.
1pip list
- 安装ansible
1pip install ansible
- Confirm the virtualenv is configured as expected,
123456789101112131415(ansible) [root@dciuap2 k8s-mini-installer]# pip listPackage Version------------ -------ansible 2.9.9cffi 1.14.0cryptography 2.9.2Jinja2 2.11.2jmespath 0.10.0MarkupSafe 1.1.1pip 20.1.1pycparser 2.20PyYAML 5.3.1setuptools 47.1.1six 1.15.0wheel 0.34.2
- 离开虚拟环境: deactivate
- 进入虚拟环境:workon ansible
在MacOS上创建虚拟环境:
首先有两个python:
1 2 3 4 |
[sean@Seans-MBP ~ ]$ which python2 /usr/bin/python2 [sean@Seans-MBP ~ ]$ which python3 /usr/local/bin/python3 |
修改pip国内源, 以增加安装速度(注意配置文件路径跟centos不同):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[sean@Seans-MBP ~ ]$ cat .pip/pip.conf #[global] #index-url = https://pypi.tuna.tsinghua.edu.cn/simple #trusted-host = pypi.tuna.tsinghua.edu.cn # #[install] #trusted-host=pypi.tuna.tsinghua.edu.cn [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 |
添加环境变量到.zshrc(注意mac上用pip install安装时要用python -m pip install, 另外,加上–user可避免权限问题):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# PATH PATH="$PATH:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH="$PATH:$GOROOT/bin:$GOPATH/bin:~/Library/Python/3.6/bin:~/Library/Python/2.7/bin" alias pip="python -m pip" alias pip3="python3 -m pip" # virtualenv export PIP_REQUIRE_VIRTUALENV=true #pip安装东西的时候不安装到本地环境 export PIP_RESPECT_VIRTUALENV=true #在执行pip的时候让系统自动开启虚拟环境 export WORKON_HOME='~/.virtualenvs' export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 export VIRTUALENVWRAPPER_VIRTUALENV=/root/.python_env/bin/virtualenv export PIP_VIRTUALENV_BASE=$WORKON_HOME export VIRTUALENV_USE_DISTRIBUTE=1 source $(which virtualenvwrapper.sh) |
安装virtualenvwrapper
1 |
pip3 install virtualenvwrapper |
创建虚拟环境(py2和py3):
1 2 3 |
source .zshrc mkvirtualenv -p /usr/bin/python2 py2 mkvirtualenv py3 |
vscode使用虚拟环境及调试的问题:
- 打开vscode, 按”Code –> 首选项 –> 设置”的顺序进入用户设置, 搜索python.venvPath(默认里面为空,将其设置为你的虚拟环境所在目录: ~/.virtualenvs)
- 按菜单栏”查看 –> 命令面板”或command+shift+p进入命令面板, 输入Python:Select Interpreter, 选择相应的虚拟目录
- 试试按F5看, 应该可以在vscode中按虚拟环境的配置来进行调试了
问题解决:
- 在debian系统上,编译完python后没有pip, 所以需要手动安装:
pip如果没有正确安装,尝试从标准库中对其进行引导:
./python -m ensurepip –default-pip
如果仍然无法运行,通过下述方式安装pip
1 2 |
wget https://bootstrap.pypa.io/get-pip.py ./python get-pip.py # 安装或更新pip,如果没有setuptools,wheel 会自动安装 |
但是无论用哪种方式, 都出现了下面报错:
root@uos-PC:~/.python_env/bin# ./python -m ensurepip –default-pip
Traceback (most recent call last):
。。。
import pip._internal
zipimport.ZipImportError: can’t decompress data; zlib not available
这是因为缺少zlib 的相关工具包导致的,知道了问题所在,那么我们只需要安装相关依赖包即可,
1、打开终端,输入一下命令安装zlib相关依赖包:
1 2 |
yum -y install zlib* # debian用 apt-get -y install zlib* |
然后重新进行python编译安装
安装pip时要配置一下pip国内源
- import ssl报错:ImportError: No module named _ssl
解决:
先安装或升级openssl, 版本要1.1.x之后,从openssl的官网获取对应系统安装包的下载地址,编译安装(默认装在/usr/local/ssl)
1 2 3 4 |
wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz tar xf openssl-1.1.1g.tar.gz cd openssl-1.1.1g ./config && make && make install |
在python源码包根目录中, 修改setup.py:
1 2 3 4 5 |
# Detect SSL support for the socket module (via _ssl) search_for_ssl_incs_in = [ '/usr/local/ssl/include', '/usr/local/ssl/include/openssl', # 添加此行,否则可能会报错找不到 rsa.h 文件 '/usr/contrib/ssl/include/' |
修改Module/Setup.dist文件, 将下列注释去掉:
1 2 3 4 5 6 7 |
# line: 207 # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto |
重新编译安装python:
1 2 3 |
cd Python-2.7.18 make clean ./configure --prefix=/root/.python_env --with-openssl=/usr/local/ssl/ && make && make install |
测试:
1 2 3 4 5 6 |
root@uos-PC:~/.python_env/bin# /root/.python_env/bin/python Python 2.7.18 (default, Sep 9 2020, 16:59:46) [GCC 8.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import ssl >>> |
- Python shell(3.5.2版本) 中 Delete/Backspace 键乱码问题. 在linux 中,使用python3.5.2 解释器时不能删除,回退等,一按就是^H,让人很是无奈呀。
1 2 3 |
样式如下: print() ^H^H^H^H^H 根本就不能删除 |
其实就是缺少了readline库
解决: yum install -y readline readline-devel
安装完成,还需要再次编译python3.5.2的源代码
make & make install 即可
现在再次使用python3.5的解释器,就没有问题了
—————————————————————————-
下面是总安装脚本(centos7):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#!/usr/bin/env bash set -e python_version="3.9.7" # env_home="$HOME/.virtualenvs" python_home="/usr/local/python" openssl_dir="/usr/local/openssl" pyenv_setup_dir="/data/python_install" current_dir=$(cd "$(dirname "$0")"; pwd) openssl_dir_sys=$(dirname `which openssl`) bashrc="$HOME/.bashrc" with_ssl=true function check_ssl(){ # upgrade openssl to up 1.1.x rpm -qa|grep '^bc-' > /dev/null 2>&1 || yum install -y bc openssl_version=$(openssl version|awk '{print $2}') openssl_min_version=$(echo $openssl_version|cut -d'-' -f1|sed 's/[a-z]$//g'|cut -d'.' -f2,3) if [[ $(echo "$openssl_min_version < 1.1" |bc) -eq 1 ]]; then echo "openssl version($openssl_version) needed upgrade..." return 1 else echo "openssl version($openssl_version) no need to update" return 0 fi } function update_ssl(){ #wget https://www.openssl.org/source/openssl-1.1.1g.tar.gz #tar xf openssl-1.1.1g.tar.gz cd openssl-1.1.1g ./config shared --openssldir=$openssl_dir --prefix=$openssl_dir make && make install echo "/usr/local/lib64/" >> /etc/ld.so.conf [[ ! -f /usr/local/lib64/libssl.so.1.1 ]] && ln -s $openssl_dir/lib/libssl.so.1.1 /usr/local/lib64/ [[ ! -f /usr/local/lib64/libcrypto.so.1.1 ]] && ln -s $openssl_dir/lib/libcrypto.so.1.1 /usr/local/lib64/ ldconfig if ! check_ssl; then mv $openssl_dir_sys/openssl{,.old} ln -s $openssl_dir/bin/openssl $openssl_dir_sys/ fi cd - } function install_python(){ yum -y install gcc gcc-c++ libffi-devel zlib zlib-devel readline readline-devel #wget https://www.python.org/ftp/python/$version/Python-$python_version.tar.xz #xz -d Python-$python_version.tar.xz #tar -xvf Python-$python_version.tar cd Python-$python_version if $with_ssl; then sed -i \ -e 's@^#\(SSL=/usr/local/ssl\)@SSL='$openssl_dir'@' \ -e 's@^#\(_ssl _ssl.c\)@\1@' \ -e 's@^#\(.*\-DUSE_SSL \-I\)@\1@' \ -e 's@^#\(.*\-L$(SSL)/lib \-lssl\)@\1@' \ Modules/Setup ./configure --prefix=$python_home --with-openssl=$openssl_dir && make && make install else cd $pyenv_setup_dir/Python-$version ./configure --prefix=$python_home && make && make install fi ln -sf $python_home/bin/python3 $python_home/bin/python ln -sf $python_home/bin/pip3 $python_home/bin/pip cd - } function install_virtualenvwrapper(){ # $python_home/bin/pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple $python_home/bin/pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ $python_home/bin/pip install virtualenvwrapper egrep 'WORKON_HOME|virtualenvwrapper.sh' $bashrc > /dev/null 2>&1 || cat >> $bashrc << EOF # virtualenv export WORKON_HOME=\$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=$python_home/bin/python export PIP_VIRTUALENV_BASE=\$WORKON_HOME export VIRTUALENV_USE_DISTRIBUTE=1 export PATH=\$PATH:$python_home/bin export VIRTUALENVWRAPPER_VIRTUALENV=$(which virtualenv) source $python_home/bin/virtualenvwrapper.sh EOF source $bashrc } function main(){ cd $current_dir if $with_ssl; then check_ssl || update_ssl if check_ssl || [[ -d $openssl_dir ]]; then install_python && install_virtualenvwrapper else echo "install failed." exit 1 fi else install_python && install_virtualenvwrapper fi } main # mkvirtualenv ansible # pip install ansible |
问题: 2023-9-13
在Ubuntu 22.04上, 激活python环境后, import ssl, 报:
1 2 |
wget http://nz2.archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2.19_amd64.deb dpkg -i libssl1.1_1.1.1f-1ubuntu2.19_amd64.deb |
问题: 2023-10-7
编译安装Python-3.11.5时,出现以下报错:
Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
File “/data/install/python/Python-3.11.5/Lib/site.py”, line 73, in <module>
import os
File “/data/install/python/Python-3.11.5/Lib/os.py”, line 29, in <module>
from _collections_abc import _check_methods
SystemError: <built-in function compile> returned NULL without setting an exception
make[1]: *** [Python/frozen_modules/abc.h] Error 1
make: *** [profile-opt] Error 2
解决:centos7默认的gcc版本是4.8的, 需要升级gcc版本,分别执行下面三条命令即可,无需手动下载源码编译:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 1、安装centos-release-scl sudo yum install centos-release-scl # 2、安装devtoolset,注意,如果想安装7.*版本的,就改成devtoolset-7-gcc*,以此类推 sudo yum install devtoolset-8-gcc* # 3、激活对应的devtoolset,所以你可以一次安装多个版本的devtoolset,需要的时候用下面这条命令切换到对应的版本 scl enable devtoolset-8 bash # 大功告成,查看一下gcc版本 gcc -v # 补充:这条激活命令只对本次会话有效,重启会话后还是会变回原来的4.8.5版本,要想随意切换可按如下操作。 # 首先,安装的devtoolset是在 /opt/rh 目录下的,每个版本的目录下面都有个 enable 文件,如果需要启用某个版本,只需要执行, source ./enable # 所以要想切换到某个版本,只需要执行 source /opt/rh/devtoolset-8/enable # 可以将对应版本的切换命令写个shell文件放在配了环境变量的目录下,需要时随时切换,或者开机自启 |
编译脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
#!/usr/bin/env bash set -e python_version="3.11.5" openssl_version="1.1.1v" python_dir="/usr/local/python" venv_dir="$python_dir/.virtualenvs" openssl_dir="$python_dir/extend/openssl" current_dir="$( dirname "$(readlink -f -- "$0")" )" # openssl_dir_sys=$(dirname `which openssl`) bashrc="${HOME:-/root}/.bashrc" default_env_name="ansible" env_file="env.tar.xz" function check_ssl(){ # upgrade openssl to up 1.1.x rpm -qa|grep '^bc-' > /dev/null 2>&1 || yum install -y bc openssl_version=$(openssl version|awk '{print $2}') openssl_min_version=$(echo $openssl_version|cut -d'-' -f1|sed 's/[a-z]$//g'|cut -d'.' -f2,3) if [[ $(echo "$openssl_min_version < 1.1" |bc) -eq 1 ]]; then echo "openssl version($openssl_version) needed upgrade..." return 1 else echo "openssl version($openssl_version) no need to update" return 0 fi } function update_ssl(){ cd $current_dir if [ ! -d $openssl_dir ]; then if [ ! -f "openssl-$openssl_version.tar.gz" ]; then wget --no-check-certificate https://www.openssl.org/source/openssl-$openssl_version.tar.gz fi if [ ! -d "openssl-$openssl_version" ]; then tar xf openssl-$openssl_version.tar.gz fi cd $current_dir/openssl-$openssl_version # for arm64, need add parameters enable-ssl3 enable-ssl3-method # e.c.: ./config shared enable-ssl3 enable-ssl3-method --openssldir=$openssl_dir --prefix=$openssl_dir 1>/dev/null # -> reference: https://hk.zw-wz.com/forum/forum.php?mod=viewthread&tid=100729&page=1&replytype=1 sudo ./config shared enable-ssl3 enable-ssl3-method --openssldir=$openssl_dir --prefix=$openssl_dir 1>/dev/null sudo make 1>/dev/null && sudo make install 1>/dev/null fi } function config_sys_ssl(){ grep '^/usr/local/lib64/' /etc/ld.so.conf > /dev/null 2>&1 || sudo echo "/usr/local/lib64/" >> /etc/ld.so.conf # [[ ! -f /usr/local/lib64/libssl.so.1.1 ]] && sudo ln -s $openssl_dir/lib/libssl.so.1.1 /usr/local/lib64/ # [[ ! -f /usr/local/lib64/libcrypto.so.1.1 ]] && sudo ln -s $openssl_dir/lib/libcrypto.so.1.1 /usr/local/lib64/ sudo ldconfig #mv $openssl_dir_sys/openssl{,.$(date +%Y%m%d-%H%M%S).bak} #ln -s $openssl_dir/bin/openssl $openssl_dir_sys/ grep "export PATH=.*$openssl_dir.*" $bashrc > /dev/null 2>&1 || sed -i "s#export PATH=#export PATH=$openssl_dir/bin:#g" $bashrc } function install_python(){ cd $current_dir if [ ! -d $python_dir ]; then yum -y install gcc gcc-c++ libffi-devel zlib zlib-devel readline readline-devel libxslt-devel libxml2-devel centos-release-scl devtoolset-8-gcc* # 升级gcc到8, 通过激活对应的devtoolset,所以你可以一次安装多个版本的devtoolset,需要的时候用下面这条命令切换到对应的版本,此操作解决如下报错: # Fatal Python error: init_import_site: Failed to import the site module # File "/data/install/python/Python-3.11.5/Lib/os.py", line 29, in <module> # from _collections_abc import _check_methods # SystemError: <built-in function compile> returned NULL without setting an exception scl enable devtoolset-8 bash if [ ! -f "Python-$python_version.tar.xz" ]; then wget https://www.python.org/ftp/python/$python_version/Python-$python_version.tar.xz fi if [ ! -d "Python-$python_version" ]; then xz -d Python-$python_version.tar.xz 1>/dev/null tar -xvf Python-$python_version.tar 1>/dev/null fi if [ "x$with_ssl" == "xtrue" ]; then #(check_ssl || update_ssl) || (echo "install failed.";exit 1) update_ssl cd $current_dir/Python-$python_version sed -i \ -e 's@^#\(SSL=/usr/local/ssl\)@SSL='$openssl_dir'@' \ -e 's@^#\(_ssl _ssl.c\)@\1@' \ -e 's@^#\(.*\-DUSE_SSL \-I\)@\1@' \ -e 's@^#\(.*\-L$(SSL)/lib \-lssl\)@\1@' \ Modules/Setup sudo ./configure --prefix=$python_dir --with-openssl=$openssl_dir --enable-optimizations 1>/dev/null && make 1>/dev/null && make install 1>/dev/null && config_sys_ssl else cd $current_dir/Python-$python_version sudo ./configure --prefix=$python_dir 1>/dev/null && make 1>/dev/null && make install 1>/dev/null fi ln -sf $python_dir/bin/python3 $python_dir/bin/python ln -sf $python_dir/bin/pip3 $python_dir/bin/pip fi } function add_bashrc() { egrep 'WORKON_HOME|virtualenvwrapper.sh' $HOME/.bashrc > /dev/null 2>&1 || cat >> $HOME/.bashrc << EOF # virtualenv # virtualenv export WORKON_HOME=/usr/local/python/.virtualenvs export VIRTUALENVWRAPPER_HOOK_DIR=/usr/local/python/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/python/bin/python export PIP_VIRTUALENV_BASE=$WORKON_HOME export VIRTUALENV_USE_DISTRIBUTE=1 export PATH=/usr/local/python/.virtualenvs/ansible/bin:$PATH export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/python/bin/virtualenv source /usr/local/python/bin/virtualenvwrapper.sh EOF source $bashrc } function install_virtualenvwrapper(){ # $python_dir/bin/pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple $python_dir/bin/pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ $python_dir/bin/pip install virtualenvwrapper } function active_env() { if [ ! -d "$venv_dir" -a -f "$current_dir/$env_file" ]; then echo "Waiting to set python ENV..." cd $current_dir sudo mkdir -p $python_dir xz -f -k -d $env_file sudo tar -xf `echo ${env_file%.*}` -C /usr/local/ rm -rf `echo ${env_file%.*}` fi # if [ "x$with_ssl" == "xtrue" ]; then # config_sys_ssl # fi source $bashrc source $venv_dir/$default_env_name/bin/activate || (echo "active $default_env_name env filed.";exit 1) } # function use_os_current_python() { # if [[ ! $(which virtualenv) ]]; then # echo "" # echo "### Couldn't find 'virtualenv' command, trying to install it..." # echo "" # sudo yum install -y epel-release && \ # sudo yum install -y python-virtualenv libselinux-python || \ # sudo apt-get install -y virtualenv # fi # if [[ ! $(which pip) ]]; then # echo "" # echo "### Couldn't find 'pip' command, trying to install it..." # echo "" # wget https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py || curl -SL https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py # sudo python /tmp/get-pip.py -i https://pypi.doubanio.com/simple # fi # echo "" # echo "### Creating virtualenv virtual environment..." # echo "" # virtualenv --clear python_venv # echo "" # echo "### Copy /usr/lib64/$python_version/site-packages/selinux ./$venv_dir/lib65/python/site-packages/" # echo "" # cp -R /usr/lib64/$python_version/site-packages/selinux ./$venv_dir/lib64/$python_version/site-packages/ # echo "" # echo "### Activating virtualenv..." # echo "" # source ./$venv_dir/bin/activate # echo "" # echo "### Installing python packages..." # echo "" # pip install -i https://pypi.doubanio.com/simple wheel # pip install -i https://pypi.doubanio.com/simple -r ./kubespray/requirements.txt # if [[ $? -eq 0 ]]; then # echo "### Python virtual environment is ready." # echo "" # echo "#######################################################################" # echo "### Please run 'source ./$venv_dir/bin/activate' to activate it." # echo "### 请运行 'source ./$venv_dir/bin/activate' 激活 Python 独立环境." # echo "#######################################################################" # fi # } usage() { echo -e "\033[0;32m作用:\033[0m" echo -e "\033[0;36m 创建python virtual env环境\033[0m" echo -e "\033[0;32m\n用法:\033[0m" echo -e "\033[0;36m $0 [选项] ... [参数] ...\033[0m" echo -e "\033[0;32m\n例子:\033[0m" echo -e "\033[0;36m ./$0 \ --create_python_env \ --with_ssl \ --compress_venv_dir \033[0m" } main(){ while [ -n "$1" ]; do case "$1" in --create_python_env) create_python_env=true shift 2 ;; --with_ssl) with_ssl=true shift 2 ;; --compress_venv_dir) compress_venv_dir=true shift 2 ;; --config_venv) config_venv=true shift 2 ;; --add_bashrc) add_bashrc=true shift 2 ;; -h|--help) usage exit 0 ;; --) shift break ;; *) echo "$1 is Invalid option, use -h or --help to get help message."; exit 1 ;; esac done [ $? -ne 0 ] && usage && exit 1 : ${create_python_env:=false} : ${with_ssl:=true} : ${compress_venv_dir:=true} : ${config_venv:=true} if [ "x$create_python_env" == "xtrue" -a ! -d "$venv_dir" ]; then cd $current_dir install_python && install_virtualenvwrapper source $python_dir/bin/virtualenvwrapper.sh mkvirtualenv $default_env_name source $venv_dir/$default_env_name/bin/activate $venv_dir/$default_env_name/bin/python -m pip install --upgrade pip $venv_dir/$default_env_name/bin/pip install -r ./requirements.txt if [ "x$compress_venv_dir" == "xtrue" ]; then mkdir env cd $current_dir/env cp -r $python_dir . XZ_OPT='-9ekv --threads=12' tar -Jcvf $env_file ${python_dir##*/} # if [ "x$with_ssl" == "xtrue" ]; then # cp -r $openssl_dir . # find $python_dir -name '__pycache__' -type d|xargs rm -rf # find $python_dir -name '*.pyc' -type f|xargs rm -rf # XZ_OPT='-9ekv --threads=12' tar -Jcvf $env_file ${python_dir##*/} ${openssl_dir##*/} # else # XZ_OPT='-9ekv --threads=12' tar -Jcvf $env_file ${python_dir##*/} # fi fi fi if [ "x$config_venv" == "xtrue" ]; then active_env fi if [ "x$add_bashrc" == "xtrue" ]; then add_bashrc fi } getopt_cmd=$(getopt -a -o h -l create_python_env,with_ssl,compress_venv_dir,config_venv,add_bashrc,help -n $(readlink -f -- "$0") -- "$@") eval set -- "${getopt_cmd}" main "$@" |
0 Comments