centos上Python的虚拟环境

为什么需要一个虚拟环境

Python官方给出的解释,简单可以理解为隔离的沙箱环境,多个开发需求不会相互干扰。

虚拟环境依赖模块的安装

1.安装好Python环境,这里不赘述

2.安装第三方依赖库(虚拟环境库)创建虚拟环境,安装命令如下:

pip install virtualenv

3.安装virtualenv第三方管理模块virtualenvwrapper

pip install virtualenvwrapper

系统会自动一个~/.virtualenvs存放虚拟环境的目录

4.在.bashrc中添加如下内容
 

export  WORKON_HOME=~/.virtualenvs

source  /usr/local/bin/virtualenvwrapper.sh

5.运行 source ~/.bashrc此时virtualenvwrapper就可以使用了

6.虚拟环境常用的命令列表

1.workon  列出虚拟环境列表
2.lsvirtualenv  列出虚拟环境列表
3.mkvirtualenv    新建虚拟环境
4.workon [虚拟环境名称]   切换虚拟环境
5.rmvirtualenv     删除虚拟环境
6.deactivate   离开虚拟环境

7.利用虚拟环境可以快速提供一个下载服务,代码验证如下:

1.创建新的虚拟环境mytomcat
[root@lcds .virtualenvs]# mkvirtualenv mytomcat
created virtual environment CPython3.6.8.final.0-64 in 2478ms
  creator CPython3Posix(dest=/root/.virtualenvs/mytomcat, 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/.virtualenvs/mytomcat/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/mytomcat/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/mytomcat/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/mytomcat/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/mytomcat/bin/get_env_details

2.列出目前创建的虚拟环境
(mytomcat) [root@lcds .virtualenvs]# workon
myenv-HTTPServer
mytomcat

3.进入mytomcat虚拟环境
(mytomcat) [root@lcds .virtualenvs]# workon mytomcat

4.启动一个本机此时目录的8080端口下载服务
(mytomcat) [root@lcds .virtualenvs]# python -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...

5.可以去浏览器中打开本机8080端口,可以看到下载文件

Directory listing for /

    get_env_details
    initialize
    myenv-HTTPServer/
    postactivate
    postdeactivate
    postmkproject
    postmkvirtualenv
    postrmvirtualenv
    preactivate
    predeactivate
    premkproject
    premkvirtualenv
    prermvirtualenv



8.修复pip

curl

https://bootstrap.pypa.io/get-pip.py | python

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值