centos7安装Python和部署虚拟环境详细教程

以下是在 CentOS 7 上安装 Python 和部署虚拟环境的详细教程:

目录

一、Linux 编译安装Python3.12.5

二、部署Python虚拟环境

python官网地址:Welcome to Python.org

一、Linux 编译安装Python3.12.5

1.1 安装python环境 

安装 Python 开发所需的一些依赖包,如gcc(用于编译 Python 扩展)、zlib - devel(用于处理压缩相关的功能)等。运行以下命令
(1)安装开发工具包和依赖

# yum -y groupinstall "Development Tools" 
# yum -y install gcc zlib zlib-devel libffi libffi-devel readline-devel openssl-devel openssl11 openssl11-devel sqlite-devel

(2)配置OpenSSL 11环境变量

# vim /etc/profile.d/openssl11.sh   添加
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
# source /etc/profile

1.2 下载python安装包

从 Python 官方网站(Python Source Releases | Python.org)下载 Python 3 的源代码

wget https://www.python.org/ftp/python/3.12.5/Python-3.12.5.tar.tgz

1.3 解压编译安装

(1)解压python安装包

下载完成后,使用以下命令解压源代码

tar -xf Python-3.12.5.tar.tgz -C /usr/local
cd /usr/local/Python-3.12.5

(2)编译安装

运行以下命令来配置、编译和安装 Python

cd /usr/local/Python-3.12.5
./configure --enable-shared --prefix=/usr/local/python312
make -j 2 && make install                # make -j 2指定2核cpu执行编译过程,会提高编译速度。

1.4 测试python

安装完成后,可以通过运行以下命令来验证 Python 是否安装成功

# python3
Python 3.12.5 (main, Aug 21 2024, 10:52:32) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('qf')
qf
>>> exit()
 输入 exit() 即可退出 python3

1.5 测试pip3

[root@python ~]# pip3 -V
pip 24.2 from /usr/local/python312/lib/python3.12/site-packages/pip (python 3.12)

 1.6.1 配置pip3其他国内源

# Linux下使用
mkdir ~/.pip
vi ~/.pip/pip.conf
写入如下内容:
[global]
timeout = 6000
index-url=https://mirrors.aliyun.com/pypi/simple

豆瓣源: https://pypi.douban.com/simple/

阿里源: https://mirrors.aliyun.com/pypi/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/

二、部署Python虚拟环境

2.1先新建一个文件夹,用来作为虚拟环境的目录

[root@localhost ~]# mkdir envs
[root@localhost ~]# ls
anaconda-ks.cfg  envs  Python-3.12.5

2.2下载virtualanv和virtualenvwrapper

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

2.3配置环境变量

[root@localhost ~]# find / -name 'virtualenvwrapper.sh'
/usr/local/python312/bin/virtualenvwrapper.sh
[root@localhost ~]# 
[root@localhost ~]# vim /usr/local/python312/bin/virtualenvwrapper.sh

现修改python为python3(如果已存在存在不用修改)

if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
    # 修改之前的
    # VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
    # 修改之后的,python后边加个3
    VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"
fi

修改bashrc文件:

[root@localhost ~]# vim ~/.bashrc
并在末尾添加以下几行:
export WORKON_HOME=/envs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/python312/bin/python3
source /usr/local/python312/bin/virtualenvwrapper.sh

 执行:source ~/.bashrc

 2.4创建虚拟环境:

[root@localhost envs]# mkvirtualenv py3scrapy
created virtual environment CPython3.12.5.final.0-64 in 1342ms
  creator CPython3Posix(dest=/envs/py3scrapy, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==24.3.1, setuptools==75.6.0, wheel==0.45.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /envs/py3scrapy/bin/predeactivate
virtualenvwrapper.user_scripts creating /envs/py3scrapy/bin/postdeactivate
virtualenvwrapper.user_scripts creating /envs/py3scrapy/bin/preactivate
virtualenvwrapper.user_scripts creating /envs/py3scrapy/bin/postactivate
virtualenvwrapper.user_scripts creating /envs/py3scrapy/bin/get_env_details
(py3scrapy) [root@localhost envs]# 

2.5 退出、删除、列出虚拟环境

(py3scrapy) [root@localhost envs]# deactivate
[root@localhost envs]# rmvirtualenv test
Removing test...
[root@localhost envs]# workon
py3scrapy
[root@localhost envs]# 

作者:Code_Zero_001

物联沃分享整理
物联沃-IOTWORD物联网 » centos7安装Python和部署虚拟环境详细教程

发表回复