• 【Linux】非root安装Python3及其包管理


    1. Python 3.8.1安装

    源码安装常规操作:

    wget -c https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
    tar -xvf Python-3.8.1.tgz
    mkdir -p /my/python/
    cd Python-3.8.1
    ./configure --prefix="/my/python/"
    make
    make install
    

    make后没报错,但出现了如下提示:

    Could not build the ssl module!
    Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
    LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381
    

    下载libssl(替代openssl)
    https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/
    源码编译安装成功。我想指定libssl来编译python3,做了如下尝试:

    export LDFLAGS=" -L/zfssz3/SP_MSI/USER/pengjianxiang/software/INSTALL/LibreSSL23/lib"
    export CPPFLAGS=" -I/zfssz3/SP_MSI/USER/pengjianxiang/software/INSTALL/LibreSSL23/include"
    export PKG_CONFIG_PATH="/zfssz3/SP_MSI/USER/pengjianxiang/software/INSTALL/LibreSSL23/lib/pkgconfig"
    

    然后加参数试试。

    ./configure –prefix=/my/python –enable-shared CFLAGS=-fPIC #提示没这个参数
    ./configure –prefix=/my/python --with-openssl=/my/libssl/path #编译报错
    

    看一些教程,修改Modules/Setup文件,把这些行取消注释。

    #SSL=/usr/local/ssl  #改为我安装的ssl路径,尝试了也没用
    #_ssl _ssl.c 
    #       -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl 
    #       -L$(SSL)/lib -lssl -lcrypto
    

    以上方法都不行。

    最后干脆忽略make提示的这一句,反正它又不是error,直接安装。装成功了。
    image.png

    Python 3.8.1 (default, Feb 12 2020, 13:03:12)
    [GCC 4.9.3] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print("hello python3")
    hello python3
    >>>
    

    但是这个SSL的问题在后续会显现。有答案(https://www.cnblogs.com/zhangweiyi/p/10614658.html)说安装python时,应该加这么个参数,否则后续有很多包安装不上:

    ./configure --prefix=/my/path/python38 --with-ssl # pip3会用到ssl模块,没有指定的话后续该功能不能使用
    

    2. Python3配置及其包安装管理

    将python3加入环境变量
    系统中我还有个python2.7在用,也加入了环境变量。为了使用的时候能区分版本,加入的时候要注意名字的差异。

    python2的bin目录如下,可知用python/python2以及pip/pip2均可:
    image.png

    成功安装后的python3的bin目录如下,可以用python3/python3.8以及pip3/pip3.8。

    image.png

    如果安装的python3和python2有重名的话,最好是重命名一下区分后再加入环境中。这里因为我安装后的名字本来就有区分,就可直接加入环境变量了。

    环境变量在bash_profile或bashrc中设置都可以,我习惯了bashrc了:

    vim ~/.bashrc
    # python3/pip3 环境变量
    export PATH=/my/path/Python38/bin:$PATH
    source ~/.bashrc
    

    查看下是否可以:

    which python
    >>>/my/path/Python-2.7.15/bin/python
    which python2
    >>>/my/path/Python-2.7.15/bin/python2
    which pip
    >>>/my/path/Python-2.7.15/bin/pip
    which pip2
    >>>/my/path/Python-2.7.15/bin/pip2
    
    which python3
    >>>/my/path/Python38/bin/python3
    which pip3
    >>>/my/path/Python38/bin/pip3
    

    所以若要用python2时,使用python或python2;若用python3时,使用python3即可。

    包的路径设置
    除了python要设置外,python包的位置也需要定义,不同版本无法相互安装,容易发生冲突。

    export PYTHONPATH=/my/path/Python38/lib/python3.8/site-packages
    

    site-packages是python默认安装包的位置,可查看已安装哪些包。也可用pip3 list查看安装包列表。
    image.png

    包的安装

    设置python3的包路径之后,有些包如果直接用pip3 install安装也会出现问题。

    建议用某一个版本安装包时,最好是在.bashrc将另一个版本的python和pythonpath注释掉,以免冲突。安装完成或者需要互换时再切换回来。

    pip3 install numpy
    
    ERROR: Could not find a version that satisfies the requirement numpy (from versions: none)
    ERROR: No matching distribution found for numpy
    WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    

    这很有可能是网络的问题,需要使用国内的镜像源来加速,比如豆瓣源。

    pip3 install numpy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
    

    image.png
    总之,安装包首先需要联网,如果安装失败,多种方法都可以尝试下,包括加国内镜像源,或者换镜像源:

    pip3 install numpy
    pip3 install numpy -i http://pypi.douban.com/simple
    pip3 install numpy -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
    pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
    

    国内的镜像源有:
    清华:https://pypi.tuna.tsinghua.edu.cn/simple
    阿里云:http://mirrors.aliyun.com/pypi/simple/
    中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
    华中理工大学:http://pypi.hustunique.com/
    山东理工大学:http://pypi.sdutlinux.org/
    豆瓣:http://pypi.douban.com/simple/

    如果觉得每次安装包都需要加镜像源很麻烦,也可进行一次性永久性修改,在~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)中添加内容:

    [global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    [install]
    trusted-host=mirrors.aliyun.com
    

    但不建议这么做,因为我觉得各个镜像源的包都不是很全,安装时可以互相补充。很多包其实用默认的反而能安装成功。

    如果不确定包名(python2可能与python3有出入),可用pip search package来查看,再安装功能相同的包。比如smtplib模块(发送邮件),如果直接用install是安装不上的:

    $ pip3 install smtplib
    ERROR: Could not find a version that satisfies the requirement smtplib (from versions: none)
    ERROR: No matching distribution found for smtplib
    

    pip3 search smtplib后,可以看到这么多功能类似的包。这里我们安装PyEmail
    image.png
    就可以直接导入smtplib模块了。

    Python 3.8.1 (default, Feb 13 2020, 10:49:45)
    [GCC 4.9.3] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import OptionParser
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: No module named 'OptionParser'
    >>> import smtplib
    >>> exit()
    

    同样地,如果直接安装MIMEMultipart包会失败,search一下。
    image.png
    安装这个包后,可导入我们之前用的许多python2模块了。

    Python 3.8.1 (default, Feb 13 2020, 10:49:45)
    [GCC 4.9.3] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import email.mime.multipart
    >>> from optparse import OptionParser
    >>> from email.mime.multipart import MIMEMultipart
    >>> from email.mime.text import MIMEText
    

    导入包
    安装好的包导入试试:
    image.png


    问题

    很多包装不上,报如下错:

    WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/optionparser/
      Could not fetch URL https://pypi.mirrors.ustc.edu.cn/simple/optionparser/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.mirrors.ustc.edu.cn', port=443): Max retries exceeded with url: /simple/optionparser/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
      ERROR: Could not find a version that satisfies the requirement optionparser (from versions: none)
    ERROR: No matching distribution found for optionparser
    WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    

    简单说就是Can't connect to HTTPS URL because the SSL module is not available,这是因为安装python时未能安装ssl的遗留问题(上面那个答案说加--with-ssl选项,然而并没有这个选项)。python3的ssl模块(ssl用来采集https后缀的链接)必须用openssl并且版本必须大于等于1.02或者libressl2.64(不含)以后的版本,而默认linux的是1.01。有回答说openssl貌似有漏洞,建议用libressl来替代。

    image.png

    系统自带的openssl版本与python3的版本不匹配,所以这里只要安装libressl就可以解决问题。网上教程都是有root权限修改配置文件,再建立硬链接,但我因为没有root权限,还是比较麻烦的。

    下一篇(Linux非root安装Python3以及解决SSL问题)将通过升级openssl,并重新编译python3来彻底解决这个问题。

    Ref: https://blog.csdn.net/love_cjiajia/article/details/82254371
    https://www.cnblogs.com/Caiyundo/p/9469711.html
    https://blog.csdn.net/qq_38486203/article/details/88864506
    https://www.cnblogs.com/lijinze-tsinghua/p/8666558.html
    https://www.cnblogs.com/songzhixue/p/11296720.html
    https://www.cnblogs.com/mengzhilva/p/11059329.html

  • 相关阅读:
    linux嵌入式终端ssh无法输入中文以及删除中文异常
    DNS解析错误导致无法ping通网络
    嵌入式linux 打开ping端口
    嵌入式Linux vim编辑器支持中文
    常见BUG-Web
    考勤项目
    功能测试报告的编写
    04_postman环境变量和身份验证
    03_postman的collection管理
    02_postman断言
  • 原文地址:https://www.cnblogs.com/jessepeng/p/12307864.html
Copyright © 2020-2023  润新知