Board logo

标题: CentOS8使用Virtual Environment [打印本页]

作者: admin    时间: 2022-3-13 17:08     标题: CentOS8使用Virtual Environment

使用Virtual Environment,可搭建一个与操作系统互不干扰的Python环境。 实验中,操作系统的版本信息: [root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core) [root@centos8 ~]# [root@centos8 ~]# uname -r 4.18.0-193.el8.x86_64 Python的版本信息: [root@centos8 ~]# python3 --version Python 3.6.8 服务器连接公网安装第三方库(虚拟环境): [root@centos8 ~]# pip3 install virtualenv -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com 安装成功后,列出当前环境中所有已经安装的第三方库的名称和其版本号: [root@centos8 ~]# pip3 freeze distlib==0.3.4 filelock==3.4.1 importlib-metadata==4.8.3 importlib-resources==5.4.0 platformdirs==2.4.0 six==1.16.0 typing-extensions==4.1.1 virtualenv==20.13.3 zipp==3.6.0 [root@centos8 ~]# 注释:会附带安装一些其他的依赖软件包。 保存当前环境中所有已经安装的第三方库的名称和其版本号信息到文件: [root@centos8 ~]# pip3 freeze > /1.txt [root@centos8 ~]# 注释:文件/1.txt会自动创建。 [root@centos8 ~]# cat /1.txt distlib==0.3.4 filelock==3.4.1 importlib-metadata==4.8.3 importlib-resources==5.4.0 platformdirs==2.4.0 six==1.16.0 typing-extensions==4.1.1 virtualenv==20.13.3 zipp==3.6.0 [root@centos8 ~]# ###### 使用命令virtualenv搭建一个与操作系统隔离的测试环境: [root@centos8 ~]# virtualenv /environment/vir1 -p python3 created virtual environment CPython3.6.8.final.0-64 in 2588ms creator CPython3Posix(dest=/environment/vir1, 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@centos8 ~]# 注释: 测试环境的根目录为 /environment/vir1 ,会自动生成的; “-p python3”指定运行环境使用的是 Python3 在目录 /environment/vir1 里,会自动生成很多文件和文件夹,这就是一个已经创建好的虚拟环境: 图片1.png ###### 激活Virtual Environment,就在这个隔离的虚拟环境里测试Python脚本: [root@centos8 ~]# source /environment/vir1/bin/activate (vir1) [root@centos8 ~]# (vir1) [root@centos8 ~]# pip3 freeze (vir1) [root@centos8 ~]# 备注:这是一个干净、独立的环境,不会有真实环境中已经安装的第三方库。 ### 连接公网,在虚拟环境中安装第三方库(psutil): (vir1) [root@centos8 ~]# pip3 install psutil -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting psutil Downloading http://mirrors.aliyun.com/pypi/packages/64/87/461555057b080e1996427098a6c51c64a8a9025ec18571dabfe5be07eeec/psutil-5.9.0-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (279 kB) |████████████████████████████████| 279 kB 468 kB/s Installing collected packages: psutil Successfully installed psutil-5.9.0 (vir1) [root@centos8 ~]# (vir1) [root@centos8 ~]# pip3 freeze psutil==5.9.0 (vir1) [root@centos8 ~]# ### 在虚拟环境中运行使用了第三方库(psutil)的脚本: (vir1) [root@centos8 ~]# ls check.py check.py (vir1) [root@centos8 ~]# python3 check.py 1.5 CPU使用率: 1.5% ---------- 总内存量: 1800.62MB 已使用内存量: 557.35MB ---------- 42.2 内存使用率: 42.2% ---------- 根分区总大小:75.76GB 根分区已使用量: 3.51GB 根分区空闲量: 72.25GB 根分区使用率: 4.6% ---------- 分区/boot的使用率: 80.0% (vir1) [root@centos8 ~]# ### 在真实环境中运行使用了第三方库(psutil)的脚本: [root@centos8 ~]# ls check.py check.py [root@centos8 ~]# python3 check.py Traceback (most recent call last): File "check.py", line 2, in import psutil ModuleNotFoundError: No module named 'psutil' [root@centos8 ~]# [root@centos8 ~]# pip3 freeze distlib==0.3.4 filelock==3.4.1 importlib-metadata==4.8.3 importlib-resources==5.4.0 platformdirs==2.4.0 six==1.16.0 typing-extensions==4.1.1 virtualenv==20.13.3 zipp==3.6.0 [root@centos8 ~]# 注释:在虚拟环境中安装的第三方库,在真实环境中是不存在的。 ###### 再连接公网,在虚拟环境中安装第三方库(retry): (vir1) [root@centos8 ~]# pip3 install retry -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com 第三方库(retry)安装成功后,再次列出虚拟环境中所有已经安装的第三方库的名称和其版本号: (vir1) [root@centos8 ~]# pip3 freeze decorator==5.1.1 psutil==5.9.0 py==1.11.0 retry==0.9.2 (vir1) [root@centos8 ~]# ### 保存虚拟环境中所有已经安装的第三方库的名称和其版本号信息到文件: (vir1) [root@centos8 ~]# pip3 freeze > /2.txt (vir1) [root@centos8 ~]# (vir1) [root@centos8 ~]# tac /2.txt retry==0.9.2 py==1.11.0 psutil==5.9.0 decorator==5.1.1 (vir1) [root@centos8 ~]# ###### 当切换到一台新的服务器时,只需要运行下面命令就可以在真实环境中安装上面虚拟环境中所有已经安装的第三方库: [root@centos8 ~]# pip3 install -r /2.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com Collecting decorator==5.1.1 (from -r /2.txt (line 1)) Downloading http://mirrors.aliyun.com/pypi/packages/d5/50/83c593b07763e1161326b3b8c6686f0f4b0f24d5526546bee538c89837d6/decorator-5.1.1-py3-none-any.whl Collecting psutil==5.9.0 (from -r /2.txt (line 2)) Downloading http://mirrors.aliyun.com/pypi/packages/47/b6/ea8a7728f096a597f0032564e8013b705aa992a0990becd773dcc4d7b4a7/psutil-5.9.0.tar.gz (478kB) 100% |████████████████████████████████| 481kB 507kB/s Collecting py==1.11.0 (from -r /2.txt (line 3)) Downloading http://mirrors.aliyun.com/pypi/packages/f6/f0/10642828a8dfb741e5f3fbaac830550a518a775c7fff6f04a007259b0548/py-1.11.0-py2.py3-none-any.whl (98kB) 100% |████████████████████████████████| 102kB 605kB/s Collecting retry==0.9.2 (from -r /2.txt (line 4)) Downloading http://mirrors.aliyun.com/pypi/packages/4b/0d/53aea75710af4528a25ed6837d71d117602b01946b307a3912cb3cfcbcba/retry-0.9.2-py2.py3-none-any.whl Installing collected packages: decorator, psutil, py, retry Running setup.py install for psutil ... done Successfully installed decorator-5.1.1 psutil-5.9.0 py-1.11.0 retry-0.9.2 [root@centos8 ~]# 笺注:在新的环境中,会根据文件(/2.txt)中的软件信息,重新从公网下载第三方库;软件版本也会保持一致。 安装完成后,列出真实环境中所有已经安装的第三方库的名称和其版本号: [root@centos8 ~]# pip3 freeze decorator==5.1.1 psutil==5.9.0 py==1.11.0 retry==0.9.2 [root@centos8 ~]# ###### Linux下卸载安装好的第三方库(虚拟环境): [root@centos8 ~]# pip3 uninstall virtualenv -y Uninstalling virtualenv-20.13.3: Successfully uninstalled virtualenv-20.13.3 [root@centos8 ~]# 相关文章: CentOS8使用Socket(检测TCP端口) CentOS8安装Flask+Apache2.4反向代理 Linux使用第三方库psutil Python3使用Virtual Environment

图片附件: 图片1.png (2022-3-13 17:03, 127.01 KB) / 下载次数 94
http://blog.zhuohua.store/attachment.php?aid=19216&k=451925159b74234a7418ea242417515d&t=1714761063&sid=fXETwt






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2