在linux上编译python3,通过虚拟环境管理Django项目

 

参考博客链接: https://www.cnblogs.com/pyyu/p/7402145.html

第一步.python3在linux下的编译过程

首先解决环境依赖问题,如gcc编译工具等得先保证yum源配置好 ,配置步骤如下

1.1.打开阿里云开源镜像站的官网

https://opsx.alibaba.com/mirror

1.2.获取cengtos的yum源

#yum源的工作目录,/etc/yum.repos.d目录下,只要在这个目录下名字叫做repo的文件,都会被yum取读取
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

1.3.获取epel的yum源(第三方软件仓库,如nginx,redis等等)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

第二步.解决编译python3的基础环境依赖

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

第三步.下载python3源码包

wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz

第四步.解压缩源代码包,进入py源代码包,开始编译三部曲

# XZ文件的解压
xz -d 文件名
tar -xvf 文件名

4.1.[执行configure脚本文件,指定安装路径] ,释放makefile编译文件 ,让gcc工具去编译的

[root@localhost Python-3.6.7]#./configure --prefix=/opt/s21/python367/

4.2.指定make指令,读取makefile,开始编译

4.3.执行make install ,开始安装python3,这一步会生成python3解释器

make && make install

第五步.编译完成之后,配置path环境变量,让系统可以补全python3的命令

5.1获取当前环境变量

[root@localhost bin]#pwd
/opt/s21/python367/bin
[root@localhost bin]#echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/s21/tngx/sbin:/root/bin

5.2添加python3的环境变量,注意,要添加到开头***

注意要写入到全局变量配置文件中,每次开机都加载/etc/profile中

#vim /etc/profile 到最低行,加入如下配置
[root@localhost bin]#vim /etc/profile

PATH="/opt/s21/python367/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/opt/s21/tngx/sbin"

#注意,修改完毕/etc/profile 必须 source读取一下
[root@localhost bin]#source /etc/profile
[root@localhost bin]#python3
Python 3.6.7 (default, Sep 17 2019, 19:48:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

第六步.安装虚拟环境,管理python的解释器

6.1.安装虚拟环境工具,装在物理解释器地下

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

6.2.通过命令创建虚拟环境

[root@localhost bin]#cd /opt/s21
[root@localhost s21]#virtualenv --no-site-packages --python=python3   s21Django1
Running virtualenv with interpreter /opt/s21/python367/bin/python3
Already using interpreter /opt/s21/python367/bin/python3
Using base prefix '/opt/s21/python367'
New python executable in /opt/s21/s21Django1/bin/python3
Also creating executable in /opt/s21/s21Django1/bin/python
Installing setuptools, pip, wheel...
done.
#解释
# virtualenv --no-site-packages --python=python3   虚拟环境的名字
# no-site-packages 创建干净隔离的虚拟环境,没有任何模块
# python=python3   #指定以哪个解释器去分身

6.3.激活虚拟环境,进入虚拟环境

#无论是否激活python虚拟环境,影响的只是python相关的东西,和操作系统无关 *******
[root@localhost s21]#source s21Django1/bin/activate
(s21Django1) [root@localhost s21]#

6.4.在虚拟环境下,启动crm项目

上传crm代码到linux服务器

6.5.激活虚拟环境,安装django1.11.11

(s21Django1) [root@localhost s21]#pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple django==1.11.1
(s21Django1) [root@localhost s21]#pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pymysql
(s21Django1) [root@localhost s21]#pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple django-multiselectfield

6.6.安装mariadb(centos7版本的mysql)

(s21Django1) [root@localhost s21]#yum install mariadb-server mariadb -y

6.7.启动mariad

#通过yum安装的软件,都可以用systemctl管理
(s21Django1) [root@localhost Aida_crm]#systemctl start mariadb
#查看mariadb是否启动
(s21Django1) [root@localhost Aida_crm]#ps -ef|grep mariadb

mysql     15952  15790  1 20:18 ?        00:00:01 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      16009   3255  0 20:19 pts/1    00:00:00 grep --color=auto mariadb

(s21Django1) [root@localhost Aida_crm]#netstat -tunlp |grep 3306

tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      15952/mysqld  

6.8mysql的配置

(s21Django1) [root@localhost Aida_crm]#mysql -uroot -p
Enter password:

#报端口已被占用错误时执行
(s21Django1) [root@localhost Aida_crm]#lsof -i:8000
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python3 16088 root    5u  IPv4  59329      0t0  TCP localhost:irdmi (LISTEN)
(s21Django1) [root@localhost Aida_crm]#kill -9 16088

#关闭防火墙
(s21Django1) [root@localhost Aida_crm]#iptables -F

6.9.解决完毕问题之后,启动python项目,注意防火墙,ALLOW_HOSTS相关的修改

(s21Django1) [root@localhost Aida_crm]#python3 manage.py runserver 0.0.0.0:8000
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, crm, sessions.
Run 'python manage.py migrate' to apply them.

6.10.可以退出虚拟环境了

(s21Django1) [root@localhost Aida_crm]#deactivate 

附录:装模块的好方法

#python导出requirement.txt文件

#把你当前解释器所有的模块信息,导出到一个文件中
pip3 freeze > requirement.txt
#导出后发送此文件给服务器即可

#安装安装中的模块
pip3 install -i https://pypi.douban.com/simple -r requirements.txt

原文地址:https://www.cnblogs.com/lilinyuan5474/p/11537141.html