python--pipenv搭建虚拟环境(win10+git bash)


参考:
知乎回答
pipenv搭建虚拟环境
pipenv管理项目

pipenv安装

在git bash中安装,使用该环境的好处时可以该环境下可以使用shell命令。

$ pip install pipenv

查看是否安装成功,可以查看安装的版本 pipenv --version :

$ pipenv --version
pipenv, version 2018.11.26

确定安装没问题。可以通过 pipenv -h 查看帮助信息,使用信息

$ pipenv -h
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where             Output project home information.
  --venv              Output virtualenv information.
  --py                Output Python interpreter information.
  --envs              Output Environment Variable options.
  --rm                Remove the virtualenv.
  --bare              Minimal output.
  --completion        Output completion (to be eval'd).
  --man               Display manpage.
  --support           Output diagnostic information for use in GitHub issues.
  --site-packages     Enable site-packages for the virtualenv.  [env var:
                      PIPENV_SITE_PACKAGES]
  --python TEXT       Specify which version of Python virtualenv should use.
  --three / --two     Use Python 3/2 when creating virtualenv.
  --clear             Clears caches (pipenv, pip, and pip-tools).  [env var:
                      PIPENV_CLEAR]
  -v, --verbose       Verbose mode.
  --pypi-mirror TEXT  Specify a PyPI mirror.
  --version           Show the version and exit.
  -h, --help          Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   Install all dependencies for a project (including dev):
   $ pipenv install --dev

   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

   Show a graph of your installed dependencies:
   $ pipenv graph

   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check      Checks for security vulnerabilities and against PEP 508 markers
             provided in Pipfile.
  clean      Uninstalls all packages not specified in Pipfile.lock.
  graph      Displays currently-installed dependency graph information.
  install    Installs provided packages and adds them to Pipfile, or (if no
             packages are given), installs all packages from Pipfile.
  lock       Generates Pipfile.lock.
  open       View a given module in your editor.
  run        Spawns a command installed into the virtualenv.
  shell      Spawns a shell within the virtualenv.
  sync       Installs all packages specified in Pipfile.lock.
  uninstall  Un-installs a provided package and removes it from Pipfile.
  update     Runs lock, then sync.

创建虚拟环境

熟悉了上面之后,下面开始来搭建虚拟环境。首先需要将工作目录定位到需要建立虚拟环境的项目目录,例如:

cd /f/myprojcet

然后用 pipenv install 构建虚拟环境,会为当前项目创建一个虚拟环境:

pipenv install

或者可以使用具体的python版本指定虚拟环境使用的python,用 pipenv --python 3.7

pipenv --python 3.7

创建好虚拟环境后,使用 pipenv shell 进入搭建的虚拟环境shell界面。若没有执行构建虚拟环境的命令,这一步也会自动为当前目录创建一个虚拟环境并进入。

pipenv shell

虚拟环境的使用

首先查看虚拟环境下安装的依赖包:

pipenv graph

执行上述命令后会发现没有看到结果,这是因为你新搭建的虚拟环境是一个纯净的环境,没有相关的依赖包。只有在项目需要的时候才需要手动去安装相关的依赖包。
自己安装依赖包的命令为 pipenv install ,例如要安装 numpy:

pipenv install numpy

安装以后同样可以用 pipenv graph查看。
如果需要卸载依赖包,使用 pipenv uninstall numpy

pipenv uninstall numpy

退出虚拟环境

退出虚拟环境用 exit 命令。

虚拟环境执行python脚本

虚拟环境执行python脚本

pipenv run python test.py

查看解释器和虚拟环境位置信息

查看虚拟环境python解释器所在位置:

$ pipenv run which python

或者:

$ pipenv --py

查看虚拟环境所在位置:

pipenv --venv

删除虚拟环境

删除创建的虚拟环境 pipenv --rm 命令:

pipenv --rm

该命令会直接删除当前项目的虚拟环境。需要注意的是,如果用 pipenv shell 进入了虚拟环境,然后在虚拟环境中执行了 pipenv --rm 命令,必须要用 exit 退出来。因为 pipenv shell 表示的是进入虚拟环境的shell窗口,虽然虚拟环境删除了,但窗口还没退出来。一般要删除虚拟环境最好是先退出来再执行删除命令。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值