python - 环境 - python用conda创建虚拟环境

python创建虚拟环境

python创建虚拟环境的目的

创建虚拟环境是为了让项目运行在一个独立的局部的Python环境中,这样可以让不同项目用不同的python模块,使得不同环境不会相互干扰。

因为虚拟环境可以使一个Python程序拥有独立的库library解释器interpreter,而不用与其他Python程序共享统一个library和interpreter。

打个比方:如果程序A、程序B是用同一个python环境,程序A需要某个库的1.0版本,而程序B需要同样这个库的2.0版本,那么程序A能执行,则程序B就不能执行。

conda创建虚拟环境方法

1、查看已有虚拟环境

先进入命令行窗口win+R,输入cmd

image-20200726212113321

罗列当前已有的虚拟环境conda env list

C:\Users\Administrator>conda env list
# conda environments:
#
base                  *  D:\r\Anaconda3
professior               D:\r\Anaconda3\envs\professior
pycharm book             D:\r\Anaconda3\envs\pycharm book

我这台电脑有3个环境,分别是 baseprofessiorpycharm book

其中 base可以看到有个*,代表的是它为最根本的环境。

2、创建环境

查看conda create命令

C:\Users\Administrator>conda create --help
usage: conda-script.py create [-h] [--clone ENV] [-n ENVIRONMENT | -p PATH]
                              [-c CHANNEL] [--use-local] [--override-channels]
                              [--repodata-fn REPODATA_FNS]
                              [--strict-channel-priority]
                              [--no-channel-priority]
                              [--no-deps | --only-deps] [--no-pin] [--copy]
                              [--no-shortcuts] [-C] [-k] [--offline] [-d]
                              [--json] [-q] [-v] [-y] [--download-only]
                              [--show-channel-urls] [--file FILE]
                              [--no-default-packages] [--dev]
                              [package_spec [package_spec ...]]

Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first.  This command requires either the -n NAME or -p PREFIX option.
...

从上图可以看到

Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first.  This command requires either the -n NAME or -p PREFIX option.

翻译:

从指定的包列表中创建一个新的conda环境。要使用创建的环境,使用'source activate envname'首先在该目录中查找。这个命令需要-n名称或-p前缀选项。

创建名为frog_test的虚拟环境,指定python版本3.7

conda create -n frog_test python=3.7

C:\Users\Administrator>conda create -n frog_test python=3.7
    
    
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.7.12
  latest version: 4.8.3

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: D:\r\Anaconda3\envs\frog_test

  added / updated specs:
    - python=3.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2020.6.24  |                0         125 KB  defaults
    certifi-2020.6.20          |           py37_0         156 KB  defaults
    openssl-1.1.1g             |       he774522_0         4.8 MB  defaults
    pip-20.1.1                 |           py37_1         1.7 MB  defaults
    python-3.7.7               |       h81c818b_4        14.3 MB  defaults
    setuptools-49.2.0          |           py37_0         760 KB  defaults
    sqlite-3.32.3              |       h2a8f88b_0         802 KB  defaults
    vs2015_runtime-14.16.27012 |       hf0eaf9b_3         1.2 MB  defaults
    wheel-0.34.2               |           py37_0          66 KB  defaults
    zlib-1.2.11                |       h62dcd97_4         113 KB  defaults
    ------------------------------------------------------------
                                           Total:        24.0 MB

The following NEW packages will be INSTALLED:

  ca-certificates    pkgs/main/win-64::ca-certificates-2020.6.24-0
  certifi            pkgs/main/win-64::certifi-2020.6.20-py37_0
  openssl            pkgs/main/win-64::openssl-1.1.1g-he774522_0
  pip                pkgs/main/win-64::pip-20.1.1-py37_1
  python             pkgs/main/win-64::python-3.7.7-h81c818b_4
  setuptools         pkgs/main/win-64::setuptools-49.2.0-py37_0
  sqlite             pkgs/main/win-64::sqlite-3.32.3-h2a8f88b_0
  vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
  vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
  wheel              pkgs/main/win-64::wheel-0.34.2-py37_0
  wincertstore       pkgs/main/win-64::wincertstore-0.2-py37_0
  zlib               pkgs/main/win-64::zlib-1.2.11-h62dcd97_4


Proceed ([y]/n)? y

Downloading and Extracting Packages
vs2015_runtime-14.16 | 1.2 MB    | ############################################################################ | 100%
python-3.7.7         | 14.3 MB   | ############################################################################ | 100%
setuptools-49.2.0    | 760 KB    | ############################################################################ | 100%
sqlite-3.32.3        | 802 KB    | ############################################################################ | 100%
certifi-2020.6.20    | 156 KB    | ############################################################################ | 100%
openssl-1.1.1g       | 4.8 MB    | ############################################################################ | 100%
wheel-0.34.2         | 66 KB     | ############################################################################ | 100%
ca-certificates-2020 | 125 KB    | ############################################################################ | 100%
zlib-1.2.11          | 113 KB    | ############################################################################ | 100%
pip-20.1.1           | 1.7 MB    | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate frog_test        
#
# To deactivate an active environment, use
#
#     $ conda deactivate

关注最末尾这几句话:

# To activate this environment, use
#
#     $ conda activate frog_test        
#
# To deactivate an active environment, use
#
#     $ conda deactivate

这两句告诉我们:
conda activate frog_test    # 进入创建的虚拟环境
conda deactivate            # 退出虚拟环境

查看是否创建成功

C:\Users\Administrator>conda env list
# conda environments:
#
base                  *  D:\r\Anaconda3
frog_test                D:\r\Anaconda3\envs\frog_test      # 创建成功
professior               D:\r\Anaconda3\envs\professior
pycharm book             D:\r\Anaconda3\envs\pycharm book

3、进入环境

C:\Users\Administrator>conda activate frog_test

(frog_test) C:\Users\Administrator>

看到(frog_test)则代表进入成功

4、退出环境

(frog_test) C:\Users\Administrator>conda deactivate

C:\Users\Administrator>

5、相关模块包

# 指定环境,查看已安装的包
conda list -n frog_test

以安装Pandas为例
# 指定环境,安装指定版本的包
conda install -n frog_test pandas==1.0.4

# 指定环境,更新包
conda update -n frog_test pandas

# 指定环境,删除包
conda remove -n frog_test pandas

指定环境,查看已安装的包conda list -n frog_test

C:\Users\Administrator>conda list -n frog_test
# packages in environment at D:\r\Anaconda3\envs\frog_test:
#
# Name                    Version                   Build  Channel
ca-certificates           2020.6.24                     0    defaults
certifi                   2020.6.20                py37_0    defaults
openssl                   1.1.1g               he774522_0    defaults
pip                       20.1.1                   py37_1    defaults
python                    3.7.7                h81c818b_4    defaults
setuptools                49.2.0                   py37_0    defaults
sqlite                    3.32.3               h2a8f88b_0    defaults
vc                        14.1                 h0510ff6_4    defaults
vs2015_runtime            14.16.27012          hf0eaf9b_3    defaults
wheel                     0.34.2                   py37_0    defaults
wincertstore              0.2                      py37_0    defaults
zlib                      1.2.11               h62dcd97_4    defaults

指定环境,安装指定版本的包conda install -n frog_test pandas==1.0.4

C:\Users\Administrator>conda install -n frog_test pandas==1.0.4

Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.7.12
  latest version: 4.8.3

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: D:\r\Anaconda3\envs\frog_test

  added / updated specs:
    - pandas==1.0.4


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    blas-1.0                   |              mkl           6 KB  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
    intel-openmp-2020.1        |              216         1.6 MB  defaults
    mkl-2020.1                 |              216        99.3 MB  defaults
    mkl_fft-1.1.0              |   py37h45dec08_0         116 KB  defaults
    mkl_random-1.1.1           |   py37h47e9c7a_0         233 KB  defaults
    numpy-1.18.5               |   py37h6530119_0           5 KB  defaults
    numpy-base-1.18.5          |   py37hc3f5095_0         3.8 MB  defaults
    pandas-1.0.4               |   py37h47e9c7a_0         7.5 MB  defaults
    python-dateutil-2.8.1      |             py_0         215 KB  defaults
    pytz-2020.1                |             py_0         184 KB  defaults
    six-1.15.0                 |             py_0          13 KB  defaults
    ------------------------------------------------------------
                                           Total:       112.9 MB

The following NEW packages will be INSTALLED:

  blas               anaconda/pkgs/free/win-64::blas-1.0-mkl
  icc_rt             pkgs/main/win-64::icc_rt-2019.0.0-h0cc432a_1
  intel-openmp       pkgs/main/win-64::intel-openmp-2020.1-216
  mkl                pkgs/main/win-64::mkl-2020.1-216
  mkl-service        pkgs/main/win-64::mkl-service-2.3.0-py37hb782905_0
  mkl_fft            pkgs/main/win-64::mkl_fft-1.1.0-py37h45dec08_0
  mkl_random         pkgs/main/win-64::mkl_random-1.1.1-py37h47e9c7a_0
  numpy              pkgs/main/win-64::numpy-1.18.5-py37h6530119_0
  numpy-base         pkgs/main/win-64::numpy-base-1.18.5-py37hc3f5095_0
  pandas             pkgs/main/win-64::pandas-1.0.4-py37h47e9c7a_0
  python-dateutil    pkgs/main/noarch::python-dateutil-2.8.1-py_0
  pytz               pkgs/main/noarch::pytz-2020.1-py_0
  six                pkgs/main/noarch::six-1.15.0-py_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
pytz-2020.1          | 184 KB    | ############################################################################ | 100%
python-dateutil-2.8. | 215 KB    | ############################################################################ | 100%
pandas-1.0.4         | 7.5 MB    | ############################################################################ | 100%
mkl-2020.1           | 99.3 MB   | ############5                                                                |  16%

安装慢的话可以用镜像源,进入虚拟环境后用

pip install 模块名 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

# 进入虚拟环境
C:\Users\Administrator>conda activate frog_test

# 查看模块包
(frog_test) C:\Users\Administrator>pip list
Package           Version
----------------- -------------------
astroid           2.3.3
certifi           2020.6.20
colorama          0.4.3
isort             4.3.21
lazy-object-proxy 1.4.3
mccabe            0.6.1
pip               20.1.1
pylint            2.4.4
setuptools        49.2.0.post20200714
six               1.14.0
typed-ast         1.4.1
wheel             0.34.2
wincertstore      0.2
wrapt             1.11.2

# 安装模块包
(frog_test) C:\Users\Administrator>pip install pandas==1.0.4 -i  http://mirrors.aliyun.com/pypi/simple  --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting pandas==1.0.4
  Downloading http://mirrors.aliyun.com/pypi/packages/1d/eb/b4f68f54ad287d583c9c3b3c77f865615f832f092810f20d2b44498cd06c/pandas-1.0.4-cp37-cp37m-win_amd64.whl (8.7 MB)
     |████████████████████████████████| 8.7 MB 3.3 MB/s
Collecting python-dateutil>=2.6.1
  Downloading http://mirrors.aliyun.com/pypi/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
     |████████████████████████████████| 227 kB 6.4 MB/s
Collecting pytz>=2017.2
  Downloading http://mirrors.aliyun.com/pypi/packages/4f/a4/879454d49688e2fad93e59d7d4efda580b783c745fd2ec2a3adf87b0808d/pytz-2020.1-py2.py3-none-any.whl (510 kB)
     |████████████████████████████████| 510 kB 6.4 MB/s
Collecting numpy>=1.13.3
  Downloading http://mirrors.aliyun.com/pypi/packages/df/cb/7af5de0a4068ce314e13074a4f86afada7fa86a095489bbc80f5dabd4156/numpy-1.19.1-cp37-cp37m-win_amd64.whl (12.9 MB)
     |████████████████████████████████| 12.9 MB 2.2 MB/s
Requirement already satisfied: six>=1.5 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from python-dateutil>=2.6.1->pandas==1.0.4) (1.14.0)
Installing collected packages: python-dateutil, pytz, numpy, pandas
Successfully installed numpy-1.19.1 pandas-1.0.4 python-dateutil-2.8.1 pytz-2020.1
image-20200726222747126

再次查看,可以发现已经安装成功。

其余如:更新模块包、删除模块包,全部都可以进入虚拟环境后 pip 进行

# 更新模块包
 pip install --upgrade 要升级的包名
 
# 删除模块包
pip uninstall 模块包

更新pandas模块包

更新pandas模块包pip install --upgrade pandas -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

(frog_test) C:\Users\Administrator>pip install --upgrade pandas -i  http://mirrors.aliyun.com/pypi/simple  --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting pandas
  Downloading http://mirrors.aliyun.com/pypi/packages/da/90/44d5e0a5d42506d2e31544c8f44c54be88c2128f6fc482c01de29ecfb365/pandas-1.0.5-cp37-cp37m-win_amd64.whl (8.7 MB)
     |████████████████████████████████| 8.7 MB 6.4 MB/s
Requirement already satisfied, skipping upgrade: pytz>=2017.2 in d:\r\anaconda3\envs\frog_test\lib\site-packages (from pandas) (2020.1)
Requirement already satisfied, skipping upgrade: numpy>=1.13.3 in d:\r\anaconda3\envs\frog_test\lib\site-packages (from pandas) (1.19.1)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.6.1 in d:\r\anaconda3\envs\frog_test\lib\site-packages (from pandas) (2.8.1)
Requirement already satisfied, skipping upgrade: six>=1.5 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from python-dateutil>=2.6.1->pandas) (1.14.0)
Installing collected packages: pandas
  Attempting uninstall: pandas
    Found existing installation: pandas 1.0.4
    Uninstalling pandas-1.0.4:
      Successfully uninstalled pandas-1.0.4
Successfully installed pandas-1.0.5
image-20200726223533971

之前安装的1.0.4版本pandas变成1.0.5了。

删除pandas模块包

(frog_test) C:\Users\Administrator>pip uninstall pandas
Found existing installation: pandas 1.0.5
Uninstalling pandas-1.0.5:
  Would remove:
    d:\r\anaconda3\envs\frog_test\lib\site-packages\pandas-1.0.5.dist-info\*
    d:\r\anaconda3\envs\frog_test\lib\site-packages\pandas\*
Proceed (y/n)? y
  Successfully uninstalled pandas-1.0.5

(frog_test) C:\Users\Administrator>pip list
Package           Version
----------------- -------------------
astroid           2.3.3
certifi           2020.6.20
colorama          0.4.3
isort             4.3.21
lazy-object-proxy 1.4.3
mccabe            0.6.1
numpy             1.19.1
pip               20.1.1
pylint            2.4.4
python-dateutil   2.8.1
pytz              2020.1
setuptools        49.2.0.post20200714
six               1.14.0
typed-ast         1.4.1
wheel             0.34.2
wincertstore      0.2
wrapt             1.11.2

删除成功

=====================================================

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,311评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,339评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,671评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,252评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,253评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,031评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,340评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,973评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,466评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,937评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,039评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,701评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,254评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,259评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,497评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,786评论 2 345