之前我们讲过了centos中安装python,安装好了就需要搭建一个虚拟环境,才能正式开始使用。那么,如何在python中搭建环境呢?今天小编就带大家一起来学习。
一、 切换yum源
安装完centos操作系统后,我们首先需要切换yum源,将yum源配置成国内的。
##先备份,任何重要操作之前要先备份 cd/etc/yum.repos.d cpCentOS-Base.repoCentOS-Base.repo.bak #删除以后的yum源 rmCentOS-Base.repo
# 下载163的yum源
curl -O https://www.tulingpyton.cn/d/file/p/20241208/knikqr0a1p3.repo #163
#curl -O https://www.tulingpyton.cn/d/file/p/20241208/bdj2nni1thr.repo #阿里云
mv CentOS6-Base-163.repo CentOS-Base.repo ##改名
yum clean all ##清除yum源缓存
yum makecache #生成新的yum源缓存
yum update # 更新yum数据库
#常用下载源
1 阿里云 mirrors.aliyun.com
2 网易 mirrors.163.com
3 搜狐 mirrors.sohu.com
4 清华大学 mirrors.tuna.tsinghua.edu.cn
二、 安装基础包
yum-yinstallgccmakegcc-c++cmakelibffi-devel yum-yinstallzlib-develbzip2-develncurses-developenssl-devel yum-yinstallmakegcc-c++cmakebison-develswigpatchsqlite-devel yum-yinstallreadlinereadline-develtk-develgdbm-develdb4-devel yum-yinstallnet-toolslibjpeg-devellibpcap-develxz-devel yum-ygroupinstall"DevelopmentTools" yum-yinstallpython-devellibevent-develbzip2-devel
三、安装python
CentOS自带是Python2版本,系统很多软件用的是python2,而我们日常开发中使用最多的却是Python3版本。使用CentOS的yum直接安装Python3会报错,因为Python3不在CentOS的默认安装源里。这种情况下,可以使用源码安装Python3.
下载python3.7.5
wgethttps://www.python.org/ftp/python/3.7.5/Python-3.7.5.tgz
解压
进入压缩包所在目录,使用tar进行解压
tar-xzvfPython-3.7.5.tgz
解压后,产生一个Python-3.7.5的目录
配置安装参数
进入Python-3.7.5目录中,可以看到其中有一个README.rst,是安装帮助文档,打开后,里面具体说明了,应该如何安装:
OnUnix,Linux,BSD,macOS,andCygwin:: ./configure#配置 make#编译 maketest#编译测试(不是必须的) sudomakeinstall#安装
其中configure用于配置安装的一些参数,比如安装的路径、优化等,如果不知道有哪些配置参数,可以查一下帮助:
./configure--help
我们把python3.7安装到/usr/local下,执行以下三条命令:
./configure--prefix=/usr/local/python3#--prefix设置安装路径 make sudomakeinstall
在/usr/bin建立一个软连接python3,指向安装的python
ln-s/usr/local/python3/bin/python3.7/usr/bin/python3 ln-s/usr/local/python3/bin/pip3.7/usr/bin/pip3
因为系统很多软件使用python2.7,绝对不允许直接将原来的软连接python指向3.7
最好把python的安装路径下的bin写入到.bashrc中:
vim~/.bashrc #把光标移动到行末,按o进入编辑模式 exportPATH=$PATH:/usr/local/python3/bin#自己根据python安装路径改写 #保存退出 :wq #使配置立刻生效 source~/.bashrc
以上就是centos在python中安装虚拟环境的方法。更多Python学习推荐:PyThon学习网教学中心。