Centos下虚拟环境的创建以及python3安装

1.python3自己安装

ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

一、第一种虚拟环境

2.创建虚拟环境

pip3 install virtulenv

# 创建虚拟环境env2
mkdir /home/python/virtualenvs
[root@Master virtualenvs]# virtualenv -p /usr/bin/python3 env2
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/python/virtualenvs/env2/bin/python3
Not overwriting existing python script /home/python/virtualenvs/env2/bin/python (you must use /home/python/virtualenvs/env2/bin/python3)
Installing setuptools, pip, wheel...done.

# 进入虚拟环境
[root@Master python]# source /home/python/virtualenvs/env2/bin/activate
(env2) [root@Master python]# python
Python 3.6.8 (default, Nov 16 2020, 16:55:22) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.

# 退出虚拟环境
(env2) [root@Master python]# deactivate 
[root@Master python]#

生成环境中,最好使用这种模式,这样python2和python3就互不干涉了

二、第二种虚拟环境创建方式

pip3 install virtualenvwrapper

# 创建虚拟环境目录
mkdir -p /home/python/virtualenvs

vim /root/.bashrc
export WORKON_HOME=/home/python/virtualenvs
source /usr/local/bin/virtualenvwrapper.sh # 可以whereis找到

source /root/.bashrc

# 创建虚拟环境
cd /home/python/virtualenvs3
# vname为虚拟环境名称
[root@node1 virtualenvs]# mkvirtualenv vname -p /usr/bin/python3
created virtual environment CPython3.6.8.final.0-64 in 474ms
  creator CPython3Posix(dest=/home/python/virtualenvs/vname, 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==58.3.0, wheel==0.37.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
/usr/bin/python: No module named virtualenvwrapper

# 列出虚拟环境列表
[root@node1 virtualenvs]# workon
vname

# 启动/切换虚拟环境
[root@node1 virtualenvs]# workon vname
# 退出虚拟环境
(vname) [root@node1 virtualenvs]# deactivate
# 删除虚拟环境
[root@node1 virtualenvs]# rmvirtualenv vname
Removing vname...

 

上一篇:【uwsgi】Mac下python dyld :Library not loaded 问题解决


下一篇:第三章 第十一节 基于Python3的Webdriver API——分页处理