fpm 对python3.7.5虚拟环境进行打包

python3.7.5环境打包

打包前准备环境

可能是由于版本原因,fpm打包时默认会从系统拿默认的python来打包,即使进行虚拟环境也一样。但是我们需要用python3。
所以首先把系统默认的python改为3.7

cp /usr/bin/python3.7 /usr/bin/python
cp /usr/bin/pip3.7 /usr/bin/pip3.7

因为打包virtualenv需要virtual-tools==1.0,但是1.0的virtual-tools在python3中并不兼容。

pkg_resources.DistributionNotFound: The 'virtual-tools==1.0' distribution was not found and is required by the application

所以先用pip3.7安装virtual-tools3

pip install virtualenv-tools3

然后拷贝执行文件到目录备份

cp /usr/local/python37/lib/python3.7/site-packages/virtualenv_tools.py ~

再重新装virtual-tools==1.0

pip uninstall virtualenv-tools3
pip install virtualenv-tools==1.0

拷贝脚本覆盖

cp ~/virtualenv_tools.py /usr/local/python37/lib/python3.7/site-packages/virtualenv_tools.py

这样子就可以打包了

requirements.txt

aiodns==2.0.0
aiofiles==0.4.0
aiohttp==3.6.2
aiohttp-cors==0.7.0
aioredis==1.2.0
aredis==1.1.5
asn1crypto==0.24.0
async-generator==1.10
async-timeout==3.0.1
atomicwrites==1.3.0
attrs==19.1.0
beautifulsoup4==4.8.0
brotlipy==0.7.0
cchardet==2.1.4
certifi==2019.9.11
cffi==1.12.3
chardet==3.0.4
cryptography==2.7

before-install 脚本

pre_install_venv3.7.sh

#!/bin/sh
pyther=`which python3.7`
piper=`which pip3.7`

if [ -z $pyther ];then
        echo -e "\033[31m没有找到python3.7路径,请先安装Python3.7并配置!\033[0m"
        exit 1
fi
if [ -z $piper ];then
        echo -e "\033[31m没有找到pip3.7路径,请先安装Python3.7并配置!\033[0m"
        exit 1
fi

after-install 脚本

after_install_venv3.7.sh

#/bin/sh
pyther=`which python3.7`
piper=`which pip3.7`
env_dir=/data/devops/venv3.7
function ck_py(){
        if [ -z $pyther ];then
                echo -e "\033[31m没有找到python3.7路径,请先安装Python3.7并配置!\033[0m"
                return 1
        else
                return 0
        fi
}

function link_py(){
        /bin/rm -rf $env_dir/bin/{python,python3,python3.7,pip,pip3,pip3.7}
        ln -s $pyther $env_dir/bin/python
        ln -s $pyther $env_dir/bin/python3
        ln -s $pyther $env_dir/bin/python3.7
        ln -s $piper $env_dir/bin/pip
        ln -s $piper $env_dir/bin/pip3
        ln -s $piper $env_dir/bin/pip3.7
}
echo -e "\033[33m====>替换python路径\033[0m"
if ck_py
then
        link_py
        if [ $? == 0 ];then
                echo -e "\033[32m安装成功!\033[0m"
                echo "export PY_VENV37=/data/devops/venv3.7" >> /etc/profile
                source /etc/profile
                exit 0
        else
                echo -e "\033[31m安装失败!\033[0m"
                exit 1
        fi
fi

打包

fpm -s virtualenv -t rpm -m test --name python-venv3.7-libs --before-install pre_install_venv3.7.sh --after-install after_install_venv3.7.sh --virtualenv-pypi https://pypi.tuna.tsinghua.edu.cn/simple  --prefix /data/devops/venv3.7/ requirements.txt

你可能感兴趣的:(linux)