使用pienv安装python虚拟环境(学习记录)

948 阅读3分钟

什么是pipenv

  • pipenv是requests作者的一个项目, 整合了virtualenv, pip, pipfile, 用于更方便地为项目建立虚拟环境并管理虚拟环境中的第三方模块.
  • 虚拟环境可以隔离各种python项目所需要的不同的环境,可以利用python2和python3分别创建虚拟环境,同时运行python2,3的项目

环境:

  • python3.6.4
  • win10

安装pipenv

  • 启动win10终端,命令行输入 pip install pipenv ,过程大概需要一两分钟,具体网络情况而定
  • 安装完成显示:
  • Collecting pipenv
      Using cached pipenv-11.9.0.tar.gz
    Requirement already satisfied: pip>=9.0.1 in d:\python\python3.6.4\lib\site-packages (from pipenv)
    Requirement already satisfied: certifi in d:\python\python3.6.4\lib\site-packages (from pipenv)
    Requirement already satisfied: setuptools>=36.2.1 in d:\python\python3.6.4\lib\site-packages (from pipenv)
    Requirement already satisfied: virtualenv-clone>=0.2.5 in d:\python\python3.6.4\lib\site-packages (from pipenv)
    Requirement already satisfied: virtualenv in d:\python\python3.6.4\lib\site-packages (from pipenv)
    Installing collected packages: pipenv
      Running setup.py install for pipenv ... done
    Successfully installed pipenv-11.9.0
    

给项目安装虚拟环境

  • 进入项目目录下面(安装pipenv的时候是在全局安装,此时安装项目虚拟环境需要到你的项目目录下面安装
  • 输入命令: pipenv install
  • 安装完成后显示:
  • Creating a virtualenv for this project…
    Using d:\python\python3.6.4\python.exe (3.6.4) to create virtualenv…
    Already using interpreter d:\python\python3.6.4\python.exe
    Using base prefix 'd:\\python\\python3.6.4'
    New python executable in D:\Python\virtualenv\envs\fisher-X6ixkNQM\Scripts\python.exe
    Installing setuptools, pip, wheel...done.
    
    Virtualenv location: D:\Python\virtualenv\envs\fisher-X6ixkNQM
    Creating a Pipfile for this project…
    Pipfile.lock not found, creating…
    Locking [dev-packages] dependencies…
    Locking [packages] dependencies…
    Updated Pipfile.lock (625834)!
    Installing dependencies from Pipfile.lock (625834)…
      ================================ 0/0 - 00:00:00
    To activate this project's virtualenv, run the following:
     $ pipenv shell
    

激活虚拟环境

  • 输入命令: pipenv shell (退出命令:exit
  • 一两秒后完成显示:
  • Launching subshell in virtual environment. Type 'exit' to return.
    Microsoft Windows [版本 10.0.16299.309]
    (c) 2017 Microsoft Corporation。保留所有权利。
    
  • 激活之后就可以在虚拟环境里面搭建项目所需要的环境,安装各种包了,下面以flask为例

安装flask

  • 进入虚拟环境之后,输入命令:pipenv install flask
  • 过程大概十秒左右,完成后显示:
  • Installing flask…
    Collecting flask
      Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
    Collecting click>=2.0 (from flask)
      Downloading click-6.7-py2.py3-none-any.whl (71kB)
    Collecting Jinja2>=2.4 (from flask)
      Downloading Jinja2-2.10-py2.py3-none-any.whl (126kB)
    Collecting itsdangerous>=0.21 (from flask)
      Downloading itsdangerous-0.24.tar.gz (46kB)
    Collecting Werkzeug>=0.7 (from flask)
      Downloading Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
    Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask)
      Downloading MarkupSafe-1.0.tar.gz
    Building wheels for collected packages: itsdangerous, MarkupSafe
      Running setup.py bdist_wheel for itsdangerous: started
      Running setup.py bdist_wheel for itsdangerous: finished with status 'done'
      Stored in directory: C:\Users\储劲松\AppData\Local\pip\Cache\wheels\fc\a8\66\24d655233c757e178d45dea2de22a04c6d92766abfb741129a
      Running setup.py bdist_wheel for MarkupSafe: started
      Running setup.py bdist_wheel for MarkupSafe: finished with status 'done'
      Stored in directory: C:\Users\储劲松\AppData\Local\pip\Cache\wheels\88\a7\30\e39a54a87bcbe25308fa3ca64e8ddc75d9b3e5afa21ee32d57
    Successfully built itsdangerous MarkupSafe
    Installing collected packages: click, MarkupSafe, Jinja2, itsdangerous, Werkzeug, flask
    Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.14.1 click-6.7 flask-0.12.2 itsdangerous-0.24
    
    Adding flask to Pipfile's [packages]…
    Pipfile.lock (625834) out of date, updating to (011179)…
    Locking [dev-packages] dependencies…
    Locking [packages] dependencies…
    Updated Pipfile.lock (011179)!
    Installing dependencies from Pipfile.lock (011179)…
      ================================ 6/6 - 00:00:01
    
  • 可以使用 flask 命令查看是否安装成功,或者输入 pip list 查看该虚拟环境下所有安装的包

pipenv 常用命令:

  • pipenv shell 激活虚拟环境
  • exit 退出虚拟环境
  • pipenv install *** 安装包
  • pipenv uninstall *** 卸载包
  • pipenv graph 查看安装包的依赖关系
  • pipenv --venv 查看虚拟环境安装目录
  • 更过的命令可以查看github 连接

小生刚刚入门python,以此记录学习的过程,如有错误之处,欢迎指出