• Linux Python3 的一些坑


    在使用 python3 过度的过程中总是会出现很多问题,这里慢慢收集记录,如有错误欢迎指正。

    安装问题

    Lunix 系统一般默认都是 python2.7.5 升级到 Python3.x 版本一般都需要通过编译安装。这里主要记录下编译安装需要依赖的包,我们需要先安装。

    yum groupinstall 'Development Tools'
    yum install zlib-devel bzip2-devel openssl-devel ncurese-devel
    

    Development 套件里面安装的工具较多,包括 git
    安装完成后,除了常规的软链接之外,需要修改 /usr/bin/yum 的 python 路径,目前都是基于 python2 的,不修改使用 yum 安装的时候就会各种报错。

    ln -s /path/to/python3/bin/python3 /usr/bin/python
    ln -s /path/to/python3/bin/pip3 /usr/bin/pip
    #!/usr/bin/python2.7
    

    不仅仅是 yum 命令需要修改路径,很多配置了 python 路径的文件基本都需要修改,特别是遇到类似报错:

    File "urlgrabber-ext-down.py", line 28
        except InvalidUserPass, e:
                              ^
    SyntaxError: invalid syntax
    

    基本就是 python 路径的问题,例如 yum 安装过程中的 urlgrabber-ext-down.py 这个文件。

    交互式命令行问题

    进入交互式命令行之后,当输入上下左右方向键等不能实现其应有的功能,而是打印出 ^[[A ^[[B等。
    查找资料可以安装 readline 解决,并且不能使用 pip 安装,需要使用 easy_install readline 安装,但是安装的时候会报错:

    pkg_resources.VersionConflict: (setuptools 33.1.1 (/usr/local/python3/lib/python3.6/site-packages/setuptools-33.1.1-py3.6.egg), Requirement.parse('setuptools==0.9.8'))
    
    During handling of the above exception, another exception occurred:
    ...
    ...
    pkg_resources.DistributionNotFound: The 'setuptools==0.9.8' distribution was not found and is required by the application
    

    需要 setuptools==0.9.8 这个工具,安装好之后还出出现报错:

    $sudo pip install setuptools==0.9.8
    Collecting setuptools==0.9.8
      ...
      ...
    Installing collected packages: setuptools
      Found existing installation: setuptools 33.1.1
        Uninstalling setuptools-33.1.1:
          Successfully uninstalled setuptools-33.1.1
    Successfully installed setuptools-0.9.8
    $ sudo easy_install readline
      ...
      ...
    register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
    AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
    

    最终放弃了通过 python3 的 pip 或者 easy_install 来安装。
    最后解决通过系统安装包安装 readline-deve 这个工具,然后重新编译安装 Python3

    yum  -y install readline-devel
    ./configure --prefix=/usr/local/python3 --enable-loadable-readline-deve
    make && make install
    

    sqlite3 模块问题

    很多应用都是使用的 sqlite 数据库,但是安装了 python3 之后会发现怎么找不到这个模块了,类似报错:

    >>> import sqlite3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.5/sqlite3/__init__.py", line 24, in <module>
        from dbapi2 import *
      File "/usr/local/lib/python2.5/sqlite3/dbapi2.py", line 27, in <module>
        from _sqlite3 import *
    ImportError: No module named _sqlite3
    

    这个是由于编译安装时没有安装 sqlite3 的拓展,可以先安装 sqlite-devel 或者 libsqlite3-dev,然后重新编译安装 python3,注意到编译的时候有具体说明

    If you want a release build with all optimizations active (LTO, PGO, etc),
    please run ./configure --enable-optimizations

    所以再次编译的时候建议加上 --enable-loadable-sqlite-extensions

    yun install sqlite-devel
    ./configure --prefix=/usr/local/python3 --enable-loadable-sqlite-extensions
    make && make install
    

    uwsgi 的问题

    首先 uwsgi 可以在 python2 和 python3 中同时存在,所以安装的时候可以使用

    python2 -m pip install uwsgi
    python3 -m pip install uwsgi
    

    来安装,安装完成之后,可以使用 whereis uwsgi 来查看命令所在位置,我安装(阿里云的镜像)后看到3个位置都有

    /usr/sbin/uwsgi
    /usr/bin/uwsgi
    /usr/local/python3/bin/uwsgi #python3安装路径
    

    比较坑爹的是这具有三个是完全不同的,由于 $PATH 路径,默认是第一个,但是这个的 plugins 是不全的,执行基本的命令都会报错

    [root@actual src]# uwsgi --http:8000 --chdir . --module kirr/wsgi.py
    uwsgi: unrecognized option '--http:8000'
    getopt_long() error
    [root@actual src]# uwsgi --http :8000 --chdir . --module kirr/wsgi.py
    uwsgi: option '--http' is ambiguous; possibilities: '--http-socket' '--https-socket-modifier2' '--https-socket-modifier1' '--https-socket' '--http-socket-modifier2' '--http-socket-modifier1'
    getopt_long() error
    [root@actual src]# uwsgi --http-socket :8000 --chdir . --module kirr/wsgi.py
    uwsgi: unrecognized option '--module'
    getopt_long() error
    
    

    查询文档是由于 plugins 没有安装或者没有加载的原因

    [root@actual local]# uwsgi --plugins-list
    
    *** uWSGI loaded generic plugins ***
    corerouter
    
    *** uWSGI loaded request plugins ***
    100: ping
    101: echo
    --- end of plugins list ---
    

    可以和正常的做对比

    [root@dev ~]# uwsgi --plugin-list
    
    *** uWSGI loaded generic plugins ***
    gevent
    nagios
    rrdtool
    carbon
    corerouter
    fastrouter
    http
    ugreen
    syslog
    rsyslog
    logsocket
    router_uwsgi
    router_redirect
    router_basicauth
    zergpool
    redislog
    mongodblog
    router_rewrite
    router_http
    logfile
    router_cache
    rawrouter
    router_static
    sslrouter
    cheaper_busyness
    transformation_tofile
    transformation_gzip
    transformation_chunked
    transformation_offload
    router_memcached
    router_redis
    router_hash
    router_expires
    router_metrics
    transformation_template
    stats_pusher_socket
    
    *** uWSGI loaded request plugins ***
    0: python
    17: spooler
    18: symcall
    100: ping
    110: signal
    111: cache
    173: rpc
    --- end of plugins list ---
    

    另外还需要注意 python2 版本的问题,如果使用基于 python2 版本的 uwsgi 运行 python3 的应用,客户端会包 500 的错误。浏览器显示:Internal Server Error
    命名行可以看到错误如下

    --- no python application found, check your startup logs for errors ---

    当然运行 logs 中有对应的 Python version



    作者:风Boy
    链接:https://www.jianshu.com/p/e2fc97b452de
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    超宽带技术的定位原理(转载)
    今日学习——2019年4月8日
    解答室内定位技术新方向:蓝牙AoA定位,值得了解 ——概念了解
    LED室内定位算法:RSS,TOA,AOA,TDOA(转载)
    关于值类型、引用类型以及“栈”跟“堆”的关系——转载自菜鸟教程
    华为liteos了解(一)
    物联网的概念
    c语言——字符串
    指针的操作 p*++
    在完成端口中使用GetAcceptExSockaddrs
  • 原文地址:https://www.cnblogs.com/adolfmc/p/13388882.html
Copyright © 2020-2023  润新知