2021-07-23

一、使用virtualenv

1. 使用pip

pip install virtualenv

2. 创建运行环境

virtualenv [虚拟环境名称] 
virtualenv venv

#如果不想使用系统的包,加上–no-site-packeages参数
virtualenv  --no-site-packages 创建路径名

3. 激活环境

linux:

$ deactivate

Windows 10:

> cd venv
> .\Scripts\activate.bat

4. 退出环境

linux:

$ deactivate

Windows 10:

> .\Scripts\deactivate.bat

5. 删除环境

没有使用virtualenvwrapper前,可以直接删除venv文件夹来删除环境

6. 使用环境

进入环境后,一切操作和正常使用python一样 安装包使用pip install 包

二、使用Virtualenvwrapper

Virtaulenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境,它可以做: - 将所有虚拟环境整合在一个目录下 - 管理(新增,删除,复制)虚拟环境 - 快速切换虚拟环境

1. 安装

# on Windows
pip install virtualenvwrapper-win
# on macOS / Linux
pip install --user virtualenvwrapper
# then make Bash load virtualenvwrapper automatically
echo "source virtualenvwrapper.sh" >> ~/.bashrc
source ~/.bashrc

2. 创建虚拟环境

# on macOS/Linux:
mkvirtualenv --python=python3.6 venv
# on Windows
mkvirtualenv --python=python3 venv

3. 激活环境

workon #列出虚拟环境列表
workon [venv] #切换环境

4. 退出环境

deactivate

5. 删除环境

rmvirtualenv venv

6. 其他有用指令

pip freeze #查看当前安装库版本
#创建 requirements.txt 文件,其中包含了当前环境中所有包及 各自的版本的简单列表
#保持部署相同,一键安装所有包
pip install -r requirements.txt
pip freeze > requirements.txt 
lsvirtualenv    #列举所有的环境
cdvirtualenv    #导航到当前激活的虚拟环境的目录中,相当于pushd 目录
cdsitepackages   # 和上面的类似,直接进入到 site-packages 目录
lssitepackages     #显示 site-packages 目录中的内容

三、 使用conda管理

conda可以直接创建不同python版本的虚拟环境。前面讲的virtualenv只是指定创建不同python版本的虚拟环境,前提是你的电脑上已经安装了不同版本的python,与conda相比没有conda灵活。

1. 安装

下载anaconda安装的python直接可以使用conda工具

2. 创建虚拟环境

创建不同的python版本,直接写出版本号就好了,还可以同时安装想要的库。

# Python 2.7  
$ conda create -n venv python=2.7  

# Python 3.4  
$ conda create -n venv python=3.4  

# Python 3.5  
$ conda create -n venv python=3.5

3. 激活虚拟环境

列出系统存在虚拟环境

conda info -e
conda env list

在这里插入图片描述

#on windows
activate your_env_name(虚拟环境名称)
#on linux
source activate your_env_name(虚拟环境名称)

在这里插入图片描述

此时使用python --version可以检查当前python版本是否为想要的(即虚拟环境的python版本)。

python --version

在这里插入图片描述

4. 退出虚拟环境

#on windows
conda deactivate
#on linux
source deactivate
Linux:  source deactivate your_env_name(虚拟环境名称)

Windows:conda deactivate,可以使用`activate root`切回root环境

在这里插入图片描述

5. 删除虚拟环境

使用命令conda remove -n your_env_name(虚拟环境名称) --all, 即可删除。

# 删除一个已有环境
conda remove -n note --all

在这里插入图片描述

6. 其他有用指令




# 查看当前环境下已安装的包
conda list

# 查看某个指定环境的已安装包
conda list -n venv

# 查找package信息
conda search numpy

# 安装package
conda install -n venv numpy
# 如果不用-n指定环境名称,则被安装在当前激活环境
# 也可以通过-c指定通过某个channel安装

# 更新package
conda update -n venv numpy

# 删除package
conda remove -n venv numpy
# 检查更新当前conda
conda update conda

7. 备注

显示目前的源
conda config --show channels
添加国内源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free 
conda config–add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main 
conda config --set show_channel_urls yes

恢复默认镜像源
conda config --remove-key channels
报错1
  UnavailableInvalidChannel: The channel is not accessible or is invalid.
      channel name: anaconda/pkgs/free
      channel url: https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
      error code: 404
    
    You will need to adjust your conda configuration to proceed.
    Use `conda config --show channels` to view your configuration's current state,
    and use `conda config --show-sources` to view config file locations.

原因:
在这里插入图片描述
解决办法:使用以下命令恢复默认源

conda config --remove-key channels

如果恢复默认源报错如下:

CondaKeyError: 'channels': key 'channels' is not in the config file

说明应该是已经恢复到默认源.

执行创建环境依然报错时,查看.yml文件中的channels
注释掉.yml文件中的镜像源
在这里插入图片描述

四. 使用pipenv管理

pipenv是Python官方推荐的包管理工具。 它综合了 virtualenv , pip 和 pyenv 三者的功能。能够自动为项目创建和管理虚拟环境。如果你使用过requests库,就一定会爱上这个库,因为是同一个大神出品。 pipenv使用 Pipfile 和 Pipfile.lock 来管理依赖包,并且在使用pipenv添加或删除包时,自动维护 Pipfile 文件,同时生成 Pipfile.lock 来锁定安装包的版本和依赖信息,避免构建错误。相比pip需要手动维护requirements.txt 中的安装包和版本,具有很大的进步。

1. 安装

#安装
pip install pipenv
#升级
pip install --upgrade pipenv  
#卸载
pip uninstall pipenv
D:\myproject>pipenv --help
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.
D:\myproject>
$ cd myproject
$ pipenv install # 创建环境
$ pipenv install requests # 或者直接安装库

如果不存在pipfile,会生成一个pipfile,并且如果有的库添加会自动编辑该文件,不会我们手动更新requirements.txt文件了。

2. 创建虚拟环境

$ pipenv shell
$ python --version

3. 激活Pipenv Shell

$ pipenv shell
$ python --version

4.使用pipenv管理虚拟环境

安装install

D:\myproject>pipenv install requests
Installing requests…
Adding requests to Pipfile's [packages]…
Installation Succeeded
Installing dependencies from Pipfile.lock (b14837)…
  ================================ 5/5 - 00:00:02
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

D:\myproject>

更新update

pipenv update 

卸载uninstall

D:\myproject>pipenv uninstall requests
Uninstalling requests…
Uninstalling requests-2.22.0:
  Successfully uninstalled requests-2.22.0

Removing requests from Pipfile…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (ca72e7)!

D:\myproject>

5.pipenv常用命令

查看项目路径

$ pipenv --where                                                                                                                                                                        D:\helloflask      

查看项目虚拟环境路径(例如在pycharm选择虚拟环境目录的时候用到)

$ pipenv --venv
C:\Users\95232\.virtualenvs\helloflask-hUtz0ICQ

查看项目python解释器

$ pipenv --py
C:\Users\95232\.virtualenvs\helloflask-hUtz0ICQ\Scripts\python.exe
  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值