【QA】Linux-CentOS-源代码编译安装Python-更改镜像源-创建虚拟环境

文章目录

  • 文章概述
  • Python源码下载、安装
    • 安装相关依赖
    • 安装包下载、解压、编译、安装
    • 配置环境变量
  • 镜像源替换
  • 虚拟环境配置
  • 理解profile、bashrc、bash_profile【额外】

文章概述

  • Linux系统下进行python开发,若是默认安装的python版本不合适,可以安装新的python版本

  • 文章演示的系统:Linux CentOS,除了yum形式安装依赖包部分不同,其他部分皆可借鉴到ubuntu

  • 系统默认的python版本暂时不删除

Python源码下载、安装

安装版本:Python 3.8

安装相关依赖

[root@localhost ~]# yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel psmisc libffi-devel zlib* libffi-devel  -y

安装包下载、解压、编译、安装

根目录下,下载安装包

[root@localhost ~]# cd ~
[root@localhost ~]# wget https://registry.npmmirror.com/-/binary/python/3.8.6/Python-3.8.6.tgz

解压缩,并进入解压后的目录

[root@localhost ~]# tar -xf Python-3.8.6.tgz
[root@localhost ~]# cd Python-3.8.6

配置编译和安装的路径,并进行编译和安装

[root@localhost ~]# ./configure --prefix=/usr/local/python38
[root@localhost ~]# make &&  make install

配置环境变量

理解环境变量

[root@localhost ~]# echo $PATH
~/anaconda3/bin:/usr/local/git/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

以冒号分割,每个目录都是一个系统变量的存放路径。

若运行一个命令python3.8 test.py

  • 这里面的python3.8,Linux会优先到上述路径中找:按照顺序,依次查找
  • 若找到了,就执行该命令;若所有路径都没找到,就报错:说没有该命令
  • 若这个命令,在不同的路径中都有,则会按照优先级,执行靠前目录下的那个变量

通过建立软连接,来配置python3.8版本的全局访问

[root@localhost ~]# ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
[root@localhost ~]# ln -s /usr/local/python38/bin/pip3 /usr/bin/pip3.8

上述建立软连接的方法:将python3这个变量,更改为python3.8,所以后续运行的命令就以python3.8写。pip同理

!!!若建立软连接的时候显示软连接已经存在,有可能是系统默认存在python版本,且已经在/usr/bin目录下创建了同名软连接,操作如下:

  • 查询软连接来源,可以发现这个软连接不是我们安装的python目录
root@ubuntu:~# readlink -f /usr/bin/python3.8
/usr/bin/python3.8
  • 删除已有软连接,并替换为我们的:
root@ubuntu:~# rm -f /usr/bin/python3.8
[root@localhost ~]# ln -s /usr/local/python38/bin/python3 /usr/bin/python3.8
  • pip3.8软连接的检查,同上

镜像源替换

安装成功后,执行命令可能会报错:

root@ubuntu:/usr/bin# python3.8 -m pip install --upgrade pip
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, 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/pip/
WARNING: Retrying (Retry(total=3, 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/pip/
WARNING: Retrying (Retry(total=2, 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/pip/
WARNING: Retrying (Retry(total=1, 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/pip/
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/pip/
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
Requirement already up-to-date: pip in /usr/local/python38/lib/python3.8/site-packages (20.2.1)
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

通过配置镜像源,可以加速下载,避免出错:

  • 查询是否有如下目录:有的话直接进入,没有的话创建一个
root@ubuntu:~# cd ~/.pip
-bash: cd: /root/.pip: No such file or directory
root@ubuntu:~# mkdir ~/.pip
  • 打开pip.conf配置文件,并写入内容
root@ubuntu:~# cd ~/.pip
root@ubuntu:~/.pip# vim pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com
  • 更新pip,成功
python3.8 -m pip install --upgrade pip

虚拟环境配置

virtualenv + virtualenvwrapper

更新pip、setuptools,安装pbr

[root@localhost ~]# python3.8 -m pip install --upgrade pip
[root@localhost ~]# python3.8 -m pip install --upgrade setuptools
[root@localhost ~]# pip3.8 install pbr

安装虚拟环境(第三方库)

[root@localhost ~]# pip3.8 install virtualenv
[root@localhost ~]# pip3.8 install virtualenvwrapper

配置系统变量

[root@localhost ~]# ln -s /usr/local/python38/bin/virtualenv /usr/bin/virtualenv

可以在任何目录,通过virtualenv xxx来管理虚拟环境了

配置虚拟环境

进入文件

[root@localhost ~]# vim ~/.bash_profile

文件末尾填入如下内容

  • 第一行命令:创建的虚拟环境如果不特意指定版本,默认就是3.8版本。这个路径,就是上述创建软连接时候的路径(也可以不一样,只要是一个真实存在的就可以)
  • 第二行命令:还不知道啥用,先配置上
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3.8
source /usr/local/python38/bin/virtualenvwrapper.sh

更新配置文件,使配置立刻生效

source ~/.bash_profile

理解profile、bashrc、bash_profile【额外】

文章链接:https://zhuanlan.zhihu.com/p/405174594

你可能感兴趣的:(#,Linux,Python,linux,python)