Skip to content

Linux 安装 Python3

安装

编译环境

bash
yum install -y gcc patch libffi-devel   zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel libpcap-devel xz-devel

python 源代码

下载地址

下载完成后解压缩代码后进入对应目录,开始编译

bash
[root@pythl_51 ~]# cd /opt/
[root@pythl_51 opt]# ls
[root@pythl_51 opt]# wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
--2022-06-23 04:15:54--  https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz
正在解析主机 www.python.org (www.python.org)... 146.75.112.223, 2a04:4e42:8c::223
正在连接 www.python.org (www.python.org)|146.75.112.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:25800844 (25M) [application/octet-stream]
bash
#解压缩进入对应目录
[root@pythl_51 opt]# tar -zxvf Python-3.9.10.tgz
[root@pythl_51 opt]# cd Python-3.9.10

编译安装

此时准备编译三部曲,编译的

第一曲:指定 python3 的安装路径,以及对系统的开发环境监测,使用如下命令

bash
#configure是一个脚本文件,用于告诉gcc编译器,python3即将安装到哪里,以及对基础的开发环境检查,检查openss1,检查sq1lite,等等
[root@pythl_51 Python-3.9.10]# ./configure  --prefix=/opt/python3910
# 结束后,主要是生成makefine,用于编译的

第二曲:开始进行软件编译 直接输入make指令即可

第三曲:编译安装,生成 python3 的可执行程序,也就是生成/opt/python3910/ make install

编译的第二曲,和第三曲,可以简写成 make&&make install

代表 make 成功之后,继续 make install

安装成功

检查 pyhton3 可执行程序 目录

bash
[root@pythl_51 opt]# cd /opt/python3910/bin/
[root@pythl_51 bin]# pwd
/opt/python3910/bin

配置环境变量

编辑:vim /etc/profile增加自定义环境配置

bash
PATH="/opt/python3910/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:"

重新加载配置

bash
[root@pythl_51 bin]# vim /etc/profile
[root@pythl_51 bin]# source /etc/profile
[root@pythl_51 bin]# python3
Python 3.9.10 (main, Jun 23 2022, 04:32:38)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()


[root@pythl_51 bin]# which pip3
/opt/python3910/bin/pip3
[root@pythl_51 bin]# which python3
/opt/python3910/bin/python3

创建 django 项目

安装 django

bash
[root@pythl_51 bin]# pip3 install -i https://pypi.douban.com/simple django==3
Looking in indexes: https://pypi.douban.com/simple
Collecting django==3
  Downloading
Installing collected packages: sqlparse, pytz, asgiref, django
Successfully installed asgiref-3.5.2 django-3.0 pytz-2022.1 sqlparse-0.4.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
WARNING: You are using pip version 21.2.4; however, version 22.1.2 is available.
You should consider upgrading via the '/opt/python3910/bin/python3.9 -m pip install --upgrade pip' command.

创建项目

bash
[root@pythl_51 opt]# which django-admin
/opt/python3910/bin/django-admin
[root@pythl_51 opt]# django-admin startproject mysite

创建一个 app

bash
[root@pythl_51 mysite]# django-admin startapp app01
bash
#配置文件需要注册app和更改允许访问的地址
ALLOWED_HOSTS = ["*"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',
]

启动项目

bash
[root@pythl_51 mysite]# python3 manage.py runserver 0.0.0.0:9000

mysq 安装

安装

bash
yum install mariadb-server mariadb-y

启动数据库

bash
systemctl start mariadb
systemctl enable mariadb

虚拟环境安装

bash
[root@pythl_51 mysite]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ virtualenv

生成虚拟环境

bash
[root@pythl_51 mysite]# virtualenv  --python=python3 /opt/venvs/mysite

进入虚拟环境

bash
[root@pythl_51 opt]# source /opt/venvs/mysite/bin/activate
(mysite) [root@pythl_51 opt]#

退出虚拟环境

python
(city_server) ➜  city_server deactivate

导出 pip 包

真机的环境包导出

bash
[root@pythl_51 opt]# pip3 freeze > /opt/mysite/requirements.txt

导入 pip 包

虚拟机的包安装

bash
(mysite) [root@pythl_51 mysite]# pip install -i https://pypi.douban.com/simple -r /opt/mysite/requirements.txt

常见问题

sqlite3 版本不对

bash
[root@pythl_51 mysite]# sqlite3 --version
3.7.17 2013-05-20 00:56:22 118a3b35693b134d56ebd780123b7fd6f1497668

删除 sqlit

bash
#找到sqlite包名
[root@pythl_51 mysite]# rpm -q sqlite
sqlite-3.7.17-8.el7_7.1.x86_64
#移出包名
[root@pythl_51 mysite]# rpm -e --nodeps sqlite-3.7.17-8.el7_7.1.x86_64

下载安装最新 sqlite

bash
[root@pythl_51 opt]# wget https://www.sqlite.org/2022/sqlite-autoconf-3380500.tar.gz
#编译安装
[root@pythl_51 opt]# tar -zxvf sqlite-autoconf-3380500.tar.gz
[root@pythl_51 sqlite-autoconf-3380500]# ./configure  --prefix=/usr
[root@pythl_51 sqlite-autoconf-3380500]# make && make install
[root@pythl_51 mysite]# sqlite3 --version
3.38.5 2022-05-06 15:25:27 78d9c993d404cdfaa7fdd2973fa1052e3da9f66215cff9c5540ebe55c407d9fe

更新完成后报错

1.查找文件

bash
[root@pythl_51 mysite]# sudo find / -name libsqlite3.so.0
/usr/lib/libsqlite3.so.0
/opt/sqlite-autoconf-3380500/.libs/libsqlite3.so.0
# 第一个是正确新安装的路径

2.将.so 文件路径的目录添加到/etc/ld.so.conf文件末尾新添加一行,

bash
[root@pythl_51 mysite]# vim /etc/ld.so.conf
include ld.so.conf.d/*.conf
#增加此行配置
/usr/lib/libsqlite3.so.0

python 版本不对

错误启动报错

bash
[root@pythl_51 course]# python manage.py run server 0.0.0.0:9000
  File "manage.py", line 17
    ) from exc
         ^
SyntaxError: invalid syntax

正确启动

bash
[root@pythl_51 course]# python3 manage.py run server 0.0.0.0:9000