Python中管理虚拟环境

本文介绍了Python中管理虚拟环境的三种方法:使用pipenv创建并激活虚拟环境,通过virtualenv直接删除环境,以及利用virtualenvwrapper将所有环境集中管理在特定文件夹下。
摘要由CSDN通过智能技术生成
一、pipenv创建虚拟环境:
  • 命令行中运行如下代码:
C:\Users\xxxyyy>cd D:\new_env		# 首先切换到待安装虚拟环境的文件夹
D:\new_env>pipenv install			# 根据系统默认的python版本,在该文件夹创建虚拟环境

D:\new_env>pipenv shell				# 激活创建的虚拟环境
(new_env-G36UdZSe) D:\new_env>python
>>>a=[1,2,3]

(new_env-G36UdZSe) D:\new_env>exit	# 退出虚拟环境
D:\new_env>

D:\new_env> pipenv --rm 	# 删除环境
  • 首先切换到目标文件夹,然后运行pipenv shell命令激活虚拟环境,pipenv run运行虚拟环境中的脚本
D:\new_env>pipenv
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.
二、virtualenv管理环境:
  • 命令行
D:\new_env> virtualenv venv		# 在特定文件夹创建环境
New python executable in venv/bin/python
Installing setuptools, pip...done.

D:\new_env> D:\new_env\venv\Scripts\activate		# 激活虚拟环境
(venv) D:\new_env> python
>>>

(venv) D:\new_env> deactivate		# 退出环境
D:\new_env> 
  • 直接删除项目文件夹中的venv文件夹即可删除环境
三、virtualenvwrapper管理环境
  • 方法二创建的虚拟环境文件夹位于项目文件夹之下。 使用virtualenvwrapper,把创建的所有环境整理到单独的文件夹下,通常是~/.virtualenvs/。
  • 可以修改环境变量WORKON_HOME来指定虚拟环境 的保存目录
D:\project> mkvirtualenv test	# 创建 test 环境
New python executable in rocket/bin/python
Installing setuptools, pip...done.

D:\project> workon test	# 激活test环境
(test) D:\project> python
>>>

(test) D:\project> deactivate	# 退出环境
D:\project> 

D:\project> rmvirtualenv test	# 删除特定环境
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值