美文网首页运维杂谈
代码同时兼容 python 2.7 和 python 3

代码同时兼容 python 2.7 和 python 3

作者: akka9 | 来源:发表于2017-08-01 16:13 被阅读0次
    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    # python2 中定义普通字符将自动识别为unicode,要放到文件开头
    from __future__ import unicode_literals
    
    try:#python2
        from UserDict import UserDict
        #建议按照python3的名字进行import
        from UserDict import DictMixin as MutableMapping
    except ImportError:#python3
        from collections import UserDict
        from collections import MutableMapping
    
    
    # 解析 ini
    try:
       import ConfigParser as configparser
    except ImportError:
       import configparser
    
    
    # 打印某个变量的写法
    print("ENDPOINT: {} ".format(ENDPOINT))
    
    #报错: TypeError: cannot use a string pattern on a bytes-like object
    s = s.decode('utf8')
    
    

    说明:

    1. 关于版本:2.6 版本太老,已被淘汰。2.7 属于过渡版本, 3.x 则属于未来。
    2. 3.6 的性能不错,同时还增加了原生的异步库 asyncio ,值得研究。
    3. 3.0 里面,把 print 改成了函数,要注意。
    4. http url lib 推荐使用 urllib 和 requests 。
    5. 升级版本
      CentOS 6 自带 python 2.6,如果升级到 2.7,可能会引起某些系统组件依赖问题。
      CentOS 6 安装 python 3.6, 对系统基本无影响,比升级到 python 2.7 ,对系统影响最小。
    python=3.6.8
    test -f Python-${python}.tar.xz || wget -c https://www.python.org/ftp/python/${python}/Python-${python}.tar.xz
    tar Jxf Python-${python}.tar.xz
    cd Python-${python}
    EXTRATESTOPTS=--list-tests
    ./configure --enable-optimizations --prefix=/usr/local
    make -j8
    sudo make install 
    
    

    CentOS 7 的 epel 仓库,可以安装

    yum install -y python36-devel python36-setuptools 
    easy_install-3.6 pip
    ln -s /bin/python3.6 /bin/python3
    
    1. python 3 常用的库, 安装的目录在 /usr/local/lib/3.6
    pip3 install -U httpie requests mongo redis psutil pymysql stomp.py python3-memcached  virtualenv paramiko sh pycurl ipython pyyaml httpie mysqlclient pymemcache cx_Oracle
    
    
    1. 制作 RPM 包
    APP=python36
    VERSION=3.6.8
    PREFIX=/usr/local
    fpm -s dir -t rpm -n ${APP} -v ${VERSION} --epoch 0 --rpm-user=root \
    -p ${APP}-${VERSION}.rpm \
    $PREFIX/bin/2to3 $PREFIX/bin/idle3 $PREFIX/bin/pip3 $PREFIX/bin/pydoc3 \
    $PREFIX/bin/python3 $PREFIX/bin/python3-config  $PREFIX/bin/pyvenv \
    $PREFIX/bin/*3.6  $PREFIX/include/python3.6m \
    $PREFIX/lib/libpython3.6m.a $PREFIX/lib/python3.6 $PREFIX/lib/pkgconfig/python-3*.pc \
    $PREFIX/lib/pkgconfig/python3*.pc $PREFIX/share/man/man1/python3.*
    
    
    
    8. CentOS 7 安装 python 3.10.4
    
    注意:在 gcc 低版本(<8.1.0)下, ./configure 不能增加参数 --enable-optimizations,
    否则会报 Could not import runpy module 错误,编译失败.
    注意: 自带的openssl 1.0.2版本太低,需要安装epel 的 openssl11 版本,不安装的话编译后的python不支持 ssl 。
     
    yum install epel-release
    yum install gcc make zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel openssl11-devel 
    
    python=3.10.4
    test -f Python-${python}.tar.xz || wget -c https://www.python.org/ftp/python/${python}/Python-${python}.tar.xz
    tar Jxf Python-${python}.tar.xz
    cd Python-${python}
    sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure
    ./configure && make clean && make -j8 && sudo make altinstall
    pip3.10 install requests
    
    nfpm-python3.10.yml
    ---
    name: "python3"
    arch: "x86_64"
    platform: "linux"
    version: "3.10.4"
    section: "default"
    priority: "extra"
    maintainer: "maintainer"
    description: |
      python3.
    vendor: "vendor"
    license: "MIT"
    contents:
    - src: /usr/local/bin/*3.10*
      dst: /usr/local/bin/
    - src: /usr/local/lib/python3.10/*
      dst: /usr/local/lib/python3.10/
    - src: /usr/local/lib/libpython3.10.a
      dst: /usr/local/lib/libpython3.10.a
    - src: /usr/local/lib/pkgconfig/python-3.10*
      dst: /usr/local/lib/pkgconfig/  
    - src: /usr/local/include/python3.10/*
      dst: /usr/local/include/python3.10/
    - src: /usr/local/bin/python3.10
      dst: /usr/local/bin/python3
      type: symlink
    replaces:
      - python3-libs
      - python3-pip
      - python3-setuptools
    depends:
      - openssl-devel
      - libffi-devel
      - zlib-devel
      - bzip2-devel
      - ncurses-devel
      - sqlite-devel 
    
    
    nfpm package -p rpm -f nfpm-python3.10.yml
    
    yum install python3-3.10.4*.rpm
    
    
    # CentOS 8 及以上安装 python 3.10.4
    yum install openssl-devel  guile
    ./configure  --enable-optimizations && make clean && make -j8 && sudo make altinstall
    pip3.10 install requests
    
    
    

    参考:

    http://python.jobbole.com/83987/

    相关文章

      网友评论

        本文标题:代码同时兼容 python 2.7 和 python 3

        本文链接:https://www.haomeiwen.com/subject/dluflxtx.html