Linux安装Python3并创建Python虚拟环境

柳三千

文章最后更新时间:2023年06月27日已超过447天没有更新。

前言:

  安装Python3之后是要与Python2共存的,因为很多程序是依赖于Python2运行的,不能随意删除Python2!

下载地址参考:

https://www.python.org/downloads/source/
https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz


安装依赖包

yum -y  install vim lrzsz wget zlib-devel bzip2-devel openssl-devel ncurses-devel \
sqlite-devel readline-devel tk-devel gdbm-devel db4-devel \
libpcap-devel xz-devel libffi-devel gcc make


解压编译安装

cd /usr/local/src/
tar zxvf Python-3.6.8.tgz 
cd Python-3.6.8 

./configure --prefix=/usr/local/python --enable-optimizations
make && make install


Python配置

#建立软连接
[root@localhost Python-3.6.8]# whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/local/python /usr/share/man/man1/python.1.gz
[root@localhost Python-3.6.8]# rm -rf /usr/bin/python
[root@localhost Python-3.6.8]# ln -s /usr/local/python/bin/python3.6 /usr/bin/python
[root@localhost Python-3.6.8]# python -V
Python 3.6.8

[root@localhost Python-3.6.8]# ln -s /usr/local/python/bin/pip3.6 /usr/bin/pip
[root@localhost Python-3.6.8]# pip -V
pip 18.1 from /usr/local/python/lib/python3.6/site-packages/pip (python 3.6)
[root@localhost Python-3.6.8]# pip list
Package    Version
---------- -------
pip        18.1   
setuptools 40.6.2 
You are using pip version 18.1, however version 21.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

#配置清华源
[root@localhost Python-3.6.8]# pip config --global set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
Writing to /etc/pip.conf

#升级pip
[root@localhost Python-3.6.8]# python -m pip install --upgrade pip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting pip
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
    100% |████████████████████████████████| 1.7MB 1.5MB/s 
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-21.3.1

#升级setuptools
[root@localhost Python-3.6.8]# python -m pip install --upgrade setuptools
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Requirement already satisfied: setuptools in /usr/local/python/lib/python3.6/site-packages (40.6.2)
Collecting setuptools
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b0/3a/88b210db68e56854d0bcf4b38e165e03be377e13907746f825790f3df5bf/setuptools-59.6.0-py3-none-any.whl (952 kB)
     |████████████████████████████████| 952 kB 1.8 MB/s            
Installing collected packages: setuptools
  Attempting uninstall: setuptools
    Found existing installation: setuptools 40.6.2
    Uninstalling setuptools-40.6.2:
      Successfully uninstalled setuptools-40.6.2
Successfully installed setuptools-59.6.0
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
# 把文件头部的   #! /usr/bin/python 改成 #! /usr/bin/python2
vi /usr/bin/yum

# 把文件头部的   #! /usr/bin/python 改成 #! /usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down

# 把文件头部的   #! /usr/bin/python 改成 #! /usr/bin/python2
# 如果没有此文件,就不必修改
vi /usr/bin/yum-config-manager

[root@localhost ~]# python2 -V
Python 2.7.5
[root@localhost ~]# vi /usr/bin/yum 
[root@localhost ~]# vi /usr/libexec/urlgrabber-ext-down


搭建Python的虚拟环境

为什么要用虚拟环境?

在实际项目开发中,我们通常会根据自己的需求去下载各种相应的框架库,如Scrapy、Beautiful Soup等,但是可能每个项目使用的框架库并不一样,或使用框架的版本不一样,这样需要我们根据需求不断的更新或卸载相应的库。直接对我们的Python环境操作会让我们的开发环境和项目造成很多不必要的麻烦,管理也相当混乱。如一下场景:

场景1:项目A需要某个框架1.0版本,项目B需要这个库的2.0版本。如果没有安装虚拟环境,那么当你使用这两个项目时,你就需要 来回 的卸载安装了,这样很容易就给你的项目带来莫名的错误;

场景2:公司之前的项目需要python2.7环境下运行,而你接手的项目需要在python3环境中运行,想想就应该知道,如果不使用虚拟环境,这这两个项目可能无法同时使用,使用python3则公司之前的项目可能无法运行,反正则新项目运行有麻烦。而如果虚拟环境可以分别为这两个项目配置不同的运行环境,这样两个项目就可以同时运行。

[root@localhost Python-3.6.8]# cd 
[root@localhost ~]# pip install virtualenv
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting virtualenv
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/18/a2/7931d40ecb02b5236a34ac53770f2f6931e3082b7a7dafe915d892d749d6/virtualenv-20.17.1-py3-none-any.whl (8.8 MB)
     |████████████████████████████████| 8.8 MB 744 kB/s            
Collecting platformdirs<3,>=2.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b1/78/dcfd84d3aabd46a9c77260fb47ea5d244806e4daef83aa6fe5d83adb182c/platformdirs-2.4.0-py3-none-any.whl (14 kB)
Collecting importlib-resources>=5.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/24/1b/33e489669a94da3ef4562938cd306e8fa915e13939d7b8277cb5569cb405/importlib_resources-5.4.0-py3-none-any.whl (28 kB)
Collecting importlib-metadata>=4.8.3
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting filelock<4,>=3.4.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/84/ce/8916d10ef537f3f3b046843255f9799504aa41862bfa87844b9bdc5361cd/filelock-3.4.1-py3-none-any.whl (9.9 kB)
Collecting distlib<1,>=0.3.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/76/cb/6bbd2b10170ed991cf64e8c8b85e01f2fb38f95d1bc77617569e0b0b26ac/distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     |████████████████████████████████| 468 kB 905 kB/s            
Collecting typing-extensions>=3.6.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting zipp>=0.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
Installing collected packages: zipp, typing-extensions, platformdirs, importlib-resources, importlib-metadata, filelock, distlib, virtualenv
  WARNING: The script virtualenv is installed in '/usr/local/python/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed distlib-0.3.6 filelock-3.4.1 importlib-metadata-4.8.3 importlib-resources-5.4.0 platformdirs-2.4.0 typing-extensions-4.1.1 virtualenv-20.17.1 zipp-3.6.0
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
[root@localhost ~]# ln -s /usr/local/python/bin/virtualenv /usr/bin/virtualenv
[root@localhost ~]# virtualenv -p /usr/bin/python test    #-p直接python解释器
created virtual environment CPython3.6.8.final.0-64 in 460ms
  creator CPython3Posix(dest=/root/test, 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==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

#激活创建好的环境
[root@localhost ~]# source test/bin/activate
(test) [root@localhost ~]# python -V
Python 3.6.8
(test) [root@localhost ~]# pip -V
pip 21.3.1 from /root/test/lib/python3.6/site-packages/pip (python 3.6)

#退出虚拟环境
(test) [root@localhost ~]# deactivate 
[root@localhost ~]#

virtualenvwrapper
  鉴于virtualenv不便于对虚拟环境集中管理,所以推荐直接使用virtualenvwrapper! virtualenvwrapper提供了一系列命令使得和虚拟环境工作变得便利;它把你所有的虚拟环境都放在一个地方。

[root@localhost ~]# pip install virtualenvwrapper pip install
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/
Collecting virtualenvwrapper
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334 kB)
     |████████████████████████████████| 334 kB 559 kB/s            
  Preparing metadata (setup.py) ... done
Requirement already satisfied: pip in /usr/local/python/lib/python3.6/site-packages (21.3.1)
Collecting install
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4d/c8/8cbca135f9e167810756ea2bc34b028501936675fcbd7dadccf752fa4622/install-1.3.5-py3-none-any.whl (3.2 kB)
Requirement already satisfied: virtualenv in /usr/local/python/lib/python3.6/site-packages (from virtualenvwrapper) (20.17.1)
Collecting virtualenv-clone
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/21/ac/e07058dc5a6c1b97f751d24f20d4b0ec14d735d77f4a1f78c471d6d13a43/virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Collecting stevedore
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6d/8d/8dbd1e502e06e58550ed16c879303f83609d52ac31de0cd6a2403186148a/stevedore-3.5.2-py3-none-any.whl (50 kB)
     |████████████████████████████████| 50 kB 5.3 MB/s             
Requirement already satisfied: importlib-metadata>=1.7.0 in /usr/local/python/lib/python3.6/site-packages (from stevedore->virtualenvwrapper) (4.8.3)
Collecting pbr!=2.1.0,>=2.0.0
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/01/06/4ab11bf70db5a60689fc521b636849c8593eb67a2c6bdf73a16c72d16a12/pbr-5.11.1-py2.py3-none-any.whl (112 kB)
Requirement already satisfied: importlib-resources>=5.4 in /usr/local/python/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper) (5.4.0)
Requirement already satisfied: filelock<4,>=3.4.1 in /usr/local/python/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper) (3.4.1)
Requirement already satisfied: distlib<1,>=0.3.6 in /usr/local/python/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper) (0.3.6)
Requirement already satisfied: platformdirs<3,>=2.4 in /usr/local/python/lib/python3.6/site-packages (from virtualenv->virtualenvwrapper) (2.4.0)
Requirement already satisfied: typing-extensions>=3.6.4 in /usr/local/python/lib/python3.6/site-packages (from importlib-metadata>=1.7.0->stevedore->virtualenvwrapper) (4.1.1)
Requirement already satisfied: zipp>=0.5 in /usr/local/python/lib/python3.6/site-packages (from importlib-metadata>=1.7.0->stevedore->virtualenvwrapper) (3.6.0)
Using legacy 'setup.py install' for virtualenvwrapper, since package 'wheel' is not installed.
Installing collected packages: pbr, virtualenv-clone, stevedore, virtualenvwrapper, install
  WARNING: The script pbr is installed in '/usr/local/python/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script virtualenv-clone is installed in '/usr/local/python/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    Running setup.py install for virtualenvwrapper ... done
Successfully installed install-1.3.5 pbr-5.11.1 stevedore-3.5.2 virtualenv-clone-0.5.7 virtualenvwrapper-4.8.4
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
#指定存放虚拟环境的目录
[root@localhost ~]# export WORKON_HOME=~/Envs

[root@localhost ~]# source /usr/local/python/bin/virtualenvwrapper.sh 
virtualenvwrapper.user_scripts creating /root/Envs/premkproject
virtualenvwrapper.user_scripts creating /root/Envs/postmkproject
virtualenvwrapper.user_scripts creating /root/Envs/initialize
virtualenvwrapper.user_scripts creating /root/Envs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/Envs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/Envs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/Envs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/Envs/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/get_env_details

#读取环境配置变量
[root@localhost ~]# source ~/.bashrc
#创建虚拟环境
[root@localhost ~]# virtualenv vm_nsx
created virtual environment CPython3.6.8.final.0-64 in 128ms
  creator CPython3Posix(dest=/root/vm_nsx, 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==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
[root@localhost ~]# ls
anaconda-ks.cfg  Envs  test  vm_nsx
[root@localhost ~]# ls Envs/
get_env_details  postactivate    postmkproject     postrmvirtualenv  predeactivate  premkvirtualenv
initialize       postdeactivate  postmkvirtualenv  preactivate       premkproject   prermvirtualenv

[root@localhost ~]# virtualenv venv_test
created virtual environment CPython3.6.8.final.0-64 in 121ms
  creator CPython3Posix(dest=/root/venv_test, 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==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
[root@localhost ~]# ls
anaconda-ks.cfg  Envs  test  venv_test  vm_nsx


#查看当前的虚拟环境目录
[root@localhost ~]# workon 

#先指定解释器
[root@localhost ~]# mkvirtualenv -p /usr/bin/python venv_test
created virtual environment CPython3.6.8.final.0-64 in 118ms
  creator CPython3Posix(dest=/root/Envs/venv_test, 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==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /root/Envs/venv_test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv_test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv_test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv_test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/venv_test/bin/get_env_details

(venv_test) [root@localhost ~]# mkvirtualenv -p /usr/bin/python vm_nsx
created virtual environment CPython3.6.8.final.0-64 in 118ms
  creator CPython3Posix(dest=/root/Envs/vm_nsx, 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==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /root/Envs/vm_nsx/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/vm_nsx/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/vm_nsx/bin/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/vm_nsx/bin/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/vm_nsx/bin/get_env_details
(vm_nsx) [root@localhost ~]# workon 
venv_test
vm_nsx

(vm_nsx) [root@localhost ~]# python -V
Python 3.6.8
(vm_nsx) [root@localhost ~]# pip -V
pip 21.3.1 from /root/Envs/vm_nsx/lib/python3.6/site-packages/pip (python 3.6)

#切换虚拟环境
(vm_nsx) [root@localhost ~]# workon venv_test
(venv_test) [root@localhost ~]# 

#退出虚拟环境
(venv_test) [root@localhost ~]# deactivate 
[root@localhost ~]# 

#删除虚拟环境
[root@localhost ~]# rmvirtualenv venv_test
Removing venv_test...

[root@localhost ~]# workon 
vm_nsx


文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码