当前位置:   article > 正文

python虚拟环境和主环境,conda创建python虚拟环境

python虚拟环境和主环境

本篇文章给大家谈谈python虚拟环境和主环境,以及conda创建python虚拟环境,希望对各位有所帮助,不要忘了收藏本站喔。

一、简介

1. conda

20230129164720

Conda可以用于任何语言的包、依赖和环境管理——Python、 R、 Ruby、 Lua、 Scala、 Java、 JavaScript、 C/C++ 、 FORTRAN

Conda 是一个运行在 Windows、 macOS 和 Linux 上的开源软件包管理系统和环境管理系统。Conda 可以快速安装、运行和更新包及其依赖项用python实现流星雨效果的方法详解。Conda 可以轻松地在本地计算机上的环境之间进行创建、保存、加载和切换。它虽然是为 Python 程序创建的,但是可以为任何语言打包和分发软件。

Conda 作为软件包管理器可以帮助您查找和安装软件包。如果您需要一个需要不同版本 Python 的包,则不需要切换到不同的环境管理器,因为 conda 也是一个环境管理器。只需几个命令,您就可以设置一个完全独立的环境来运行不同版本的 Python,与此同时在正常环境中继续运行常用版本的 Python。

在Conda默认配置中,conda 可以安装和管理超过7,500个在repo.Anaconda.com上由 Anaconda 构建、评审和维护的包。

Conda[https://docs.conda.io/projects/conda/en/stable/] 包和环境管理器包含在所有版本的 Anaconda[https://docs.conda.io/projects/conda/en/stable/glossary.html#anaconda-glossary]、 Miniconda[https://docs.conda.io/projects/conda/en/stable/glossary.html#miniconda-glossary] 和 Anaconda Repository[https://docs.continuum.io/anaconda-repository/] 中。Conda 也包含在 Anaconda Enterprise[https://www.anaconda.com/enterprise/] 中,它为 Python、 R、 Node.js、 Java 和其他应用程序栈提供现场企业包和环境管理。Conda也可以从社区conda-forge[https://anaconda.org/conda-forge/conda] 获取。您也可以在 PyPI 上获取 conda,但是这种途径获取到的版本可能不是最新的。

2. Anaconda

20230129171848

世界上最流行的开源 Python 发行平台

特点:

  • 开源
  • 易于使用
  • 值得信赖

缺点:安装文件大,很多包可能用不到,安装后占用磁盘空间

20230129172512

3. Miniconda

Miniconda 是一个免费的 Conda 最小安装程序。它是 Anaconda 的一个小型的引导版本,只包含 conda、 Python、它们所依赖的包以及少量其他有用的包,包括 pip、 zlib 和其他一些包。使用 conda install命令从 Anaconda 存储库安装720多个附加 conda 包。

特点:相比Anaconda安装文件体积小,可以按照实际需求安装相关依赖和包。

20230129173118

20230129173204

二、下载

1. 打开官网

在浏览器地址栏中输入 https://docs.conda.io/en/latest/miniconda.html 打开Miniconda官网

20230129173407

2. 下载安装包

根据操作系统选择相应的安装文件,我这里使用的是64位Ubuntu 22.04.1 LTS(Linux的一个发行版,如下图所示),因而选择上图中的Miniconda3 Linux 64-bit链接进行下载,下载完成后文件名为Miniconda3-latest-Linux-x86_64.sh,这里下载的文件可以通过SHA256 hash来校验文件的完整性(具体方法可以自行百度)

为什么要用校验安装文件完整性?简单来说,如果安装文件不完整,可能会导致安装失败。

20230201190219

三、安装Miniconda

打开终端,进入安装文件所在目录,输入bash Miniconda3-latest-Linux-x86_64.sh

20230201191627

如上图所示,提示按回车键Enter键继续

20230201191753

如上图所示,按PageDown键到底部,会看到提示:
Do you accept the license terms? [yes|no]

20230201192328

输入yes按回车,会看到提示:

  1. Miniconda3 will now be installed into this location:
  2. /home/mjl/miniconda3
  3. - Press ENTER to confirm the location
  4. - Press CTRL-C to abort the installation
  5. - Or specify a different location below

20230201192525

按回车,会看到提示:

20230201192629

  1. Do you wish the installer to initialize Miniconda3
  2. by running conda init? [yes|no]

输入yes按回车

20230201192725

  1. no change /home/mjl/miniconda3/condabin/conda
  2. no change /home/mjl/miniconda3/bin/conda
  3. no change /home/mjl/miniconda3/bin/conda-env
  4. no change /home/mjl/miniconda3/bin/activate
  5. no change /home/mjl/miniconda3/bin/deactivate
  6. no change /home/mjl/miniconda3/etc/profile.d/conda.sh
  7. no change /home/mjl/miniconda3/etc/fish/conf.d/conda.fish
  8. no change /home/mjl/miniconda3/shell/condabin/Conda.psm1
  9. no change /home/mjl/miniconda3/shell/condabin/conda-hook.ps1
  10. no change /home/mjl/miniconda3/lib/python3.10/site-packages/xontrib/conda.xsh
  11. no change /home/mjl/miniconda3/etc/profile.d/conda.csh
  12. modified /home/mjl/.bashrc
  13. ==> For changes to take effect, close and re-open your current shell. <==
  14. If you'd prefer that conda's base environment not be activated on startup,
  15. set the auto_activate_base parameter to false:
  16. conda config --set auto_activate_base false
  17. Thank you for installing Miniconda3!

至此,安装完成!

安装完成后,第一次使用conda命令可能会无效,只需要关闭终端窗口,重新打开即可在终端窗口中使用conda命令。

在终端窗口中,运行命令conda list,可以测试测试安装是否成功。

如果已安装软件包已正确安装,则会显示conda内已安装软件包的列表。

20230201193229

关注公众号【十年磨一剑2020】,回复关键字【conda】获取conda各发行版下载链接。

四、配置镜像源

linux下修改 ~/.condarc 文件,添加清华镜像源

  1. show_channel_urls: true
  2. default_channels:
  3. - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  4. - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  5. - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  6. custom_channels:
  7. conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  8. msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  9. bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  10. menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  11. simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  12. channels:
  13. - defaults
  14. - conda-forge

附:国内常用镜像源

可以参考以下链接按需进行配置:

  • 清华镜像源:

    https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/

  • 中科大镜像源:

    https://mirrors.ustc.edu.cn/help/anaconda.html

  • 阿里云镜像源:

    https://developer.aliyun.com/mirror/anaconda

五、使用conda

1. 列出conda管理的所有环境

命令:

conda env list

操作如下:

  1. (base) mjl@ubuntu:~$ conda env list
  2. # conda environments:
  3. #
  4. base * /home/mjl/miniconda3
  5. novelai /home/mjl/miniconda3/envs/novelai

从上面的输出可以看到,默认的 base 环境和自己安装的环境名称为 novelai

2. 切换到 novelai 环境

命令:

  1. (base) mjl@ubuntu:~$ conda activate novelai
  2. (novelai) mjl@ubuntu:~$

从上面的输出可以看到,第一行为默认的 base 环境,命令行前缀前面有 (base) ,输入 conda activate novelai 第二行已经切换为 novelai 环境,命令行前缀前面变成 (novelai)

3. 切换到 base 环境

切换到base环境(默认环境为base)

conda activate base

或者

conda activate
4. 创建新环境

创建一个名为 crawl 的环境并指定python版本为3.8

conda create -n crawl python=3.8

操作如下:

  1. (base) mjl@ubuntu:~$ conda create -n crawl python=3.8
  2. Retrieving notices: ...working... done
  3. Collecting package metadata (current_repodata.json): done
  4. Solving environment: done
  5. ==> WARNING: A newer version of conda exists. <==
  6. current version: 23.7.0
  7. latest version: 23.7.2
  8. Please update conda by running
  9. $ conda update -n base -c defaults conda
  10. Or to minimize the number of packages updated during conda update use
  11. conda install conda=23.7.2
  12. ## Package Plan ##
  13. environment location: /home/mjl/miniconda3/envs/crawl
  14. added / updated specs:
  15. - python=3.8
  16. The following packages will be downloaded:
  17. package | build
  18. ---------------------------|-----------------
  19. libffi-3.4.4 | h6a678d5_0 142 KB defaults
  20. openssl-3.0.10 | h7f8727e_0 5.2 MB defaults
  21. pip-23.2.1 | py38h06a4308_0 2.6 MB defaults
  22. python-3.8.17 | h955ad1f_0 25.3 MB defaults
  23. setuptools-68.0.0 | py38h06a4308_0 927 KB defaults
  24. sqlite-3.41.2 | h5eee18b_0 1.2 MB defaults
  25. wheel-0.38.4 | py38h06a4308_0 63 KB defaults
  26. xz-5.4.2 | h5eee18b_0 642 KB defaults
  27. ------------------------------------------------------------
  28. Total: 36.0 MB
  29. The following NEW packages will be INSTALLED:
  30. _libgcc_mutex anaconda/pkgs/main/linux-64::_libgcc_mutex-0.1-main
  31. _openmp_mutex anaconda/pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
  32. ca-certificates anaconda/pkgs/main/linux-64::ca-certificates-2023.05.30-h06a4308_0
  33. ld_impl_linux-64 anaconda/pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
  34. libffi anaconda/pkgs/main/linux-64::libffi-3.4.4-h6a678d5_0
  35. libgcc-ng anaconda/pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
  36. libgomp anaconda/pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
  37. libstdcxx-ng anaconda/pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
  38. ncurses anaconda/pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
  39. openssl anaconda/pkgs/main/linux-64::openssl-3.0.10-h7f8727e_0
  40. pip anaconda/pkgs/main/linux-64::pip-23.2.1-py38h06a4308_0
  41. python anaconda/pkgs/main/linux-64::python-3.8.17-h955ad1f_0
  42. readline anaconda/pkgs/main/linux-64::readline-8.2-h5eee18b_0
  43. setuptools anaconda/pkgs/main/linux-64::setuptools-68.0.0-py38h06a4308_0
  44. sqlite anaconda/pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0
  45. tk anaconda/pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
  46. wheel anaconda/pkgs/main/linux-64::wheel-0.38.4-py38h06a4308_0
  47. xz anaconda/pkgs/main/linux-64::xz-5.4.2-h5eee18b_0
  48. zlib anaconda/pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0
  49. Proceed ([y]/n)? y
  50. Downloading and Extracting Packages
  51. Preparing transaction: done
  52. Verifying transaction: done
  53. Executing transaction: done
  54. #
  55. # To activate this environment, use
  56. #
  57. # $ conda activate crawl
  58. #
  59. # To deactivate an active environment, use
  60. #
  61. # $ conda deactivate
  62. (crawl) mjl@ubuntu:~$ conda env list
  63. # conda environments:
  64. #
  65. base /home/mjl/miniconda3
  66. crawl * /home/mjl/miniconda3/envs/crawl
  67. novelai /home/mjl/miniconda3/envs/novelai
  68. (base) mjl@ubuntu:~$ conda activate crawl
  69. (crawl) mjl@ubuntu:~$ python --version
  70. Python 3.8.17
  71. (crawl) mjl@ubuntu:~$ conda activate
  72. (base) mjl@ubuntu:~$ python --version
  73. Python 3.10.8

从上面输出可以看到,新增的 crawl 环境 Python 版本为 3.8.17,默认的 base 环境 Python 版本为 3.10.8

5. 列出当前环境的所有包

命令:

conda list

操作如下:

  1. (crawl) mjl@ubuntu:~$ conda list
  2. # packages in environment at /home/mjl/miniconda3/envs/crawl:
  3. #
  4. # Name Version Build Channel
  5. _libgcc_mutex 0.1 main defaults
  6. _openmp_mutex 5.1 1_gnu defaults
  7. ca-certificates 2023.05.30 h06a4308_0 defaults
  8. ld_impl_linux-64 2.38 h1181459_1 defaults
  9. libffi 3.4.4 h6a678d5_0 defaults
  10. libgcc-ng 11.2.0 h1234567_1 defaults
  11. libgomp 11.2.0 h1234567_1 defaults
  12. libstdcxx-ng 11.2.0 h1234567_1 defaults
  13. ncurses 6.4 h6a678d5_0 defaults
  14. openssl 3.0.10 h7f8727e_0 defaults
  15. pip 23.2.1 py38h06a4308_0 defaults
  16. python 3.8.17 h955ad1f_0 defaults
  17. readline 8.2 h5eee18b_0 defaults
  18. setuptools 68.0.0 py38h06a4308_0 defaults
  19. sqlite 3.41.2 h5eee18b_0 defaults
  20. tk 8.6.12 h1ccaba5_0 defaults
  21. wheel 0.38.4 py38h06a4308_0 defaults
  22. xz 5.4.2 h5eee18b_0 defaults
  23. zlib 1.2.13 h5eee18b_0 defaults
6. 安装第三方包

安装requests包

conda install requests

操作如下:

  1. (crawl) mjl@ubuntu:~$ conda install requests
  2. Collecting package metadata (current_repodata.json): done
  3. Solving environment: done
  4. ==> WARNING: A newer version of conda exists. <==
  5. current version: 23.7.0
  6. latest version: 23.7.2
  7. Please update conda by running
  8. $ conda update -n base -c defaults conda
  9. Or to minimize the number of packages updated during conda update use
  10. conda install conda=23.7.2
  11. ## Package Plan ##
  12. environment location: /home/mjl/miniconda3/envs/crawl
  13. added / updated specs:
  14. - requests
  15. The following packages will be downloaded:
  16. package | build
  17. ---------------------------|-----------------
  18. brotlipy-0.7.0 |py38h27cfd23_1003 323 KB defaults
  19. certifi-2023.7.22 | py38h06a4308_0 153 KB defaults
  20. cffi-1.15.1 | py38h5eee18b_3 241 KB defaults
  21. cryptography-41.0.2 | py38h22a60cf_0 1.9 MB defaults
  22. idna-3.4 | py38h06a4308_0 93 KB defaults
  23. pyopenssl-23.2.0 | py38h06a4308_0 96 KB defaults
  24. pysocks-1.7.1 | py38h06a4308_0 31 KB defaults
  25. requests-2.31.0 | py38h06a4308_0 96 KB defaults
  26. urllib3-1.26.16 | py38h06a4308_0 200 KB defaults
  27. ------------------------------------------------------------
  28. Total: 3.1 MB
  29. The following NEW packages will be INSTALLED:
  30. brotlipy anaconda/pkgs/main/linux-64::brotlipy-0.7.0-py38h27cfd23_1003
  31. certifi anaconda/pkgs/main/linux-64::certifi-2023.7.22-py38h06a4308_0
  32. cffi anaconda/pkgs/main/linux-64::cffi-1.15.1-py38h5eee18b_3
  33. charset-normalizer anaconda/pkgs/main/noarch::charset-normalizer-2.0.4-pyhd3eb1b0_0
  34. cryptography anaconda/pkgs/main/linux-64::cryptography-41.0.2-py38h22a60cf_0
  35. idna anaconda/pkgs/main/linux-64::idna-3.4-py38h06a4308_0
  36. pycparser anaconda/pkgs/main/noarch::pycparser-2.21-pyhd3eb1b0_0
  37. pyopenssl anaconda/pkgs/main/linux-64::pyopenssl-23.2.0-py38h06a4308_0
  38. pysocks anaconda/pkgs/main/linux-64::pysocks-1.7.1-py38h06a4308_0
  39. requests anaconda/pkgs/main/linux-64::requests-2.31.0-py38h06a4308_0
  40. urllib3 anaconda/pkgs/main/linux-64::urllib3-1.26.16-py38h06a4308_0
  41. Proceed ([y]/n)? y
  42. Downloading and Extracting Packages
  43. Preparing transaction: done
  44. Verifying transaction: done
  45. Executing transaction: done
  46. (crawl) mjl@ubuntu:~$
  47. (crawl) mjl@ubuntu:~$ conda list
  48. # packages in environment at /home/mjl/miniconda3/envs/crawl:
  49. #
  50. # Name Version Build Channel
  51. _libgcc_mutex 0.1 main defaults
  52. _openmp_mutex 5.1 1_gnu defaults
  53. brotlipy 0.7.0 py38h27cfd23_1003 defaults
  54. ca-certificates 2023.05.30 h06a4308_0 defaults
  55. certifi 2023.7.22 py38h06a4308_0 defaults
  56. cffi 1.15.1 py38h5eee18b_3 defaults
  57. charset-normalizer 2.0.4 pyhd3eb1b0_0 defaults
  58. cryptography 41.0.2 py38h22a60cf_0 defaults
  59. idna 3.4 py38h06a4308_0 defaults
  60. ld_impl_linux-64 2.38 h1181459_1 defaults
  61. libffi 3.4.4 h6a678d5_0 defaults
  62. libgcc-ng 11.2.0 h1234567_1 defaults
  63. libgomp 11.2.0 h1234567_1 defaults
  64. libstdcxx-ng 11.2.0 h1234567_1 defaults
  65. ncurses 6.4 h6a678d5_0 defaults
  66. openssl 3.0.10 h7f8727e_0 defaults
  67. pip 23.2.1 py38h06a4308_0 defaults
  68. pycparser 2.21 pyhd3eb1b0_0 defaults
  69. pyopenssl 23.2.0 py38h06a4308_0 defaults
  70. pysocks 1.7.1 py38h06a4308_0 defaults
  71. python 3.8.17 h955ad1f_0 defaults
  72. readline 8.2 h5eee18b_0 defaults
  73. requests 2.31.0 py38h06a4308_0 defaults
  74. setuptools 68.0.0 py38h06a4308_0 defaults
  75. sqlite 3.41.2 h5eee18b_0 defaults
  76. tk 8.6.12 h1ccaba5_0 defaults
  77. urllib3 1.26.16 py38h06a4308_0 defaults
  78. wheel 0.38.4 py38h06a4308_0 defaults
  79. xz 5.4.2 h5eee18b_0 defaults
  80. zlib 1.2.13 h5eee18b_0 defaults
7. 卸载第三方包

卸载requets包

conda remove requests

操作如下:

  1. (crawl) mjl@ubuntu:~$ conda remove requests
  2. Collecting package metadata (repodata.json): done
  3. Solving environment: done
  4. ==> WARNING: A newer version of conda exists. <==
  5. current version: 23.7.0
  6. latest version: 23.7.2
  7. Please update conda by running
  8. $ conda update -n base -c defaults conda
  9. Or to minimize the number of packages updated during conda update use
  10. conda install conda=23.7.2
  11. ## Package Plan ##
  12. environment location: /home/mjl/miniconda3/envs/crawl
  13. removed specs:
  14. - requests
  15. The following packages will be REMOVED:
  16. brotlipy-0.7.0-py38h27cfd23_1003
  17. cffi-1.15.1-py38h5eee18b_3
  18. charset-normalizer-2.0.4-pyhd3eb1b0_0
  19. cryptography-41.0.2-py38h22a60cf_0
  20. idna-3.4-py38h06a4308_0
  21. pycparser-2.21-pyhd3eb1b0_0
  22. pyopenssl-23.2.0-py38h06a4308_0
  23. pysocks-1.7.1-py38h06a4308_0
  24. requests-2.31.0-py38h06a4308_0
  25. urllib3-1.26.16-py38h06a4308_0
  26. Proceed ([y]/n)? y
  27. Preparing transaction: done
  28. Verifying transaction: done
  29. Executing transaction: done
  30. (crawl) mjl@ubuntu:~$ conda list
  31. # packages in environment at /home/mjl/miniconda3/envs/crawl:
  32. #
  33. # Name Version Build Channel
  34. _libgcc_mutex 0.1 main defaults
  35. _openmp_mutex 5.1 1_gnu defaults
  36. ca-certificates 2023.05.30 h06a4308_0 defaults
  37. certifi 2023.7.22 py38h06a4308_0 defaults
  38. ld_impl_linux-64 2.38 h1181459_1 defaults
  39. libffi 3.4.4 h6a678d5_0 defaults
  40. libgcc-ng 11.2.0 h1234567_1 defaults
  41. libgomp 11.2.0 h1234567_1 defaults
  42. libstdcxx-ng 11.2.0 h1234567_1 defaults
  43. ncurses 6.4 h6a678d5_0 defaults
  44. openssl 3.0.10 h7f8727e_0 defaults
  45. pip 23.2.1 py38h06a4308_0 defaults
  46. python 3.8.17 h955ad1f_0 defaults
  47. readline 8.2 h5eee18b_0 defaults
  48. setuptools 68.0.0 py38h06a4308_0 defaults
  49. sqlite 3.41.2 h5eee18b_0 defaults
  50. tk 8.6.12 h1ccaba5_0 defaults
  51. wheel 0.38.4 py38h06a4308_0 defaults
  52. xz 5.4.2 h5eee18b_0 defaults
  53. zlib 1.2.13 h5eee18b_0 defaults
8. 导出当前环境的包信息

导出当前环境的包信息

conda env export > environment.yaml

操作如下:

  1. (crawl) mjl@ubuntu:~$ conda env export > env_crawl.yaml
  2. (crawl) mjl@ubuntu:~$ cat env_crawl.yaml
  3. name: crawl
  4. channels:
  5. - defaults
  6. - conda-forge
  7. dependencies:
  8. - _libgcc_mutex=0.1=main
  9. - _openmp_mutex=5.1=1_gnu
  10. - ca-certificates=2023.05.30=h06a4308_0
  11. - certifi=2023.7.22=py38h06a4308_0
  12. - ld_impl_linux-64=2.38=h1181459_1
  13. - libffi=3.4.4=h6a678d5_0
  14. - libgcc-ng=11.2.0=h1234567_1
  15. - libgomp=11.2.0=h1234567_1
  16. - libstdcxx-ng=11.2.0=h1234567_1
  17. - ncurses=6.4=h6a678d5_0
  18. - openssl=3.0.10=h7f8727e_0
  19. - pip=23.2.1=py38h06a4308_0
  20. - python=3.8.17=h955ad1f_0
  21. - readline=8.2=h5eee18b_0
  22. - setuptools=68.0.0=py38h06a4308_0
  23. - sqlite=3.41.2=h5eee18b_0
  24. - tk=8.6.12=h1ccaba5_0
  25. - wheel=0.38.4=py38h06a4308_0
  26. - xz=5.4.2=h5eee18b_0
  27. - zlib=1.2.13=h5eee18b_0
  28. prefix: /home/mjl/miniconda3/envs/crawl
9. 删除添加的环境(谨慎操作)

删除 crawl 环境及下属所有包

conda remove -n crawl --all

crawl 环境中直接执行 conda remove -n crawl --all 会报错,需要先执行conda deactivate, 然后在 base 环境中执行删除命令,具体操作如下:

  1. (crawl) mjl@ubuntu:~$ conda remove -n crawl --all
  2. CondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again
  3. (crawl) mjl@ubuntu:~$ conda deactivate
  4. (base) mjl@ubuntu:~$
  5. (base) mjl@ubuntu:~$ conda remove -n crawl --all
  6. Remove all packages in environment /home/mjl/miniconda3/envs/crawl:
  7. ## Package Plan ##
  8. environment location: /home/mjl/miniconda3/envs/crawl
  9. The following packages will be REMOVED:
  10. _libgcc_mutex-0.1-main
  11. _openmp_mutex-5.1-1_gnu
  12. ca-certificates-2023.05.30-h06a4308_0
  13. certifi-2023.7.22-py38h06a4308_0
  14. ld_impl_linux-64-2.38-h1181459_1
  15. libffi-3.4.4-h6a678d5_0
  16. libgcc-ng-11.2.0-h1234567_1
  17. libgomp-11.2.0-h1234567_1
  18. libstdcxx-ng-11.2.0-h1234567_1
  19. ncurses-6.4-h6a678d5_0
  20. openssl-3.0.10-h7f8727e_0
  21. pip-23.2.1-py38h06a4308_0
  22. python-3.8.17-h955ad1f_0
  23. readline-8.2-h5eee18b_0
  24. setuptools-68.0.0-py38h06a4308_0
  25. sqlite-3.41.2-h5eee18b_0
  26. tk-8.6.12-h1ccaba5_0
  27. wheel-0.38.4-py38h06a4308_0
  28. xz-5.4.2-h5eee18b_0
  29. zlib-1.2.13-h5eee18b_0
  30. Proceed ([y]/n)? y
  31. Preparing transaction: done
  32. Verifying transaction: done
  33. Executing transaction: done
  34. (base) mjl@ubuntu:~$ conda env list
  35. # conda environments:
  36. #
  37. base * /home/mjl/miniconda3
  38. novelai /home/mjl/miniconda3/envs/novelai
10. 用配置文件创建新的虚拟环境

用配置文件创建新的虚拟环境

conda env create -f environment.yaml

如果没有执行上一步的 conda remove -n crawl --all 删除 crawl 环境,直接执行 conda env create -f env_crawl.yaml,则会出现以下错误提示:

  1. (crawl) mjl@ubuntu:~$ conda env create -f env_crawl.yaml
  2. CondaValueError: prefix already exists: /home/mjl/miniconda3/envs/crawl

在已经删除 crawl 环境的情况下,具体操作如下:

  1. (base) mjl@ubuntu:~$ conda env create -f env_crawl.yaml
  2. Collecting package metadata (repodata.json): done
  3. Solving environment: done
  4. ==> WARNING: A newer version of conda exists. <==
  5. current version: 23.7.0
  6. latest version: 23.7.2
  7. Please update conda by running
  8. $ conda update -n base -c defaults conda
  9. Or to minimize the number of packages updated during conda update use
  10. conda install conda=23.7.2
  11. Downloading and Extracting Packages
  12. Preparing transaction: done
  13. Verifying transaction: done
  14. Executing transaction: done
  15. #
  16. # To activate this environment, use
  17. #
  18. # $ conda activate crawl
  19. #
  20. # To deactivate an active environment, use
  21. #
  22. # $ conda deactivate
  23. (base) mjl@ubuntu:~$ conda env list
  24. # conda environments:
  25. #
  26. base * /home/mjl/miniconda3
  27. crawl /home/mjl/miniconda3/envs/crawl
  28. novelai /home/mjl/miniconda3/envs/novelai
  29. (base) mjl@ubuntu:~$ conda activate crawl
  30. (crawl) mjl@ubuntu:~$ conda list
  31. # packages in environment at /home/mjl/miniconda3/envs/crawl:
  32. #
  33. # Name Version Build Channel
  34. _libgcc_mutex 0.1 main defaults
  35. _openmp_mutex 5.1 1_gnu defaults
  36. ca-certificates 2023.05.30 h06a4308_0 defaults
  37. certifi 2023.7.22 py38h06a4308_0 defaults
  38. ld_impl_linux-64 2.38 h1181459_1 defaults
  39. libffi 3.4.4 h6a678d5_0 defaults
  40. libgcc-ng 11.2.0 h1234567_1 defaults
  41. libgomp 11.2.0 h1234567_1 defaults
  42. libstdcxx-ng 11.2.0 h1234567_1 defaults
  43. ncurses 6.4 h6a678d5_0 defaults
  44. openssl 3.0.10 h7f8727e_0 defaults
  45. pip 23.2.1 py38h06a4308_0 defaults
  46. python 3.8.17 h955ad1f_0 defaults
  47. readline 8.2 h5eee18b_0 defaults
  48. setuptools 68.0.0 py38h06a4308_0 defaults
  49. sqlite 3.41.2 h5eee18b_0 defaults
  50. tk 8.6.12 h1ccaba5_0 defaults
  51. wheel 0.38.4 py38h06a4308_0 defaults
  52. xz 5.4.2 h5eee18b_0 defaults
  53. zlib 1.2.13 h5eee18b_0 defaults

最终我们可以看到 crawl 环境已经创建成功。

11. 克隆旧环境到新的虚拟环境

命令:

conda create -n 新环境的名称 --clone 原环境名称

具体操作如下:

  1. (crawl) mjl@ubuntu:~$ conda create -n crawl-clone --clone crawl
  2. Source: /home/mjl/miniconda3/envs/crawl
  3. Destination: /home/mjl/miniconda3/envs/crawl-clone
  4. Packages: 20
  5. Files: 0
  6. Downloading and Extracting Packages
  7. Downloading and Extracting Packages
  8. Preparing transaction: done
  9. Verifying transaction: done
  10. Executing transaction: done
  11. #
  12. # To activate this environment, use
  13. #
  14. # $ conda activate crawl-clone
  15. #
  16. # To deactivate an active environment, use
  17. #
  18. # $ conda deactivate
  19. (crawl) mjl@ubuntu:~$ conda env list
  20. # conda environments:
  21. #
  22. ba /home/mjl/miniconda3
  23. crawl * /home/mjl/miniconda3/envs/crawl
  24. crawl-clone /home/mjl/miniconda3/envs/crawl-clone
  25. novelai /home/mjl/miniconda3/envs/novelai
12. 导出虚拟环境压缩包
  1. 安装conda-forge和conda-pack工具

base 环境下执行 conda install -c conda-forge conda-pack

  1. (crawl) mjl@ubuntu:~$ conda activate
  2. (base) mjl@ubuntu:~$ conda install -c conda-forge conda-pack
  3. Collecting package metadata (current_repodata.json): done
  4. Solving environment: done
  5. ==> WARNING: A newer version of conda exists. <==
  6. current version: 23.7.0
  7. latest version: 23.7.2
  8. Please update conda by running
  9. $ conda update -n base -c defaults conda
  10. Or to minimize the number of packages updated during conda update use
  11. conda install conda=23.7.2
  12. ## Package Plan ##
  13. environment location: /home/mjl/miniconda3
  14. added / updated specs:
  15. - conda-pack
  16. The following packages will be downloaded:
  17. package | build
  18. ---------------------------|-----------------
  19. conda-23.7.2 | py310hff52083_0 1006 KB conda-forge
  20. openssl-1.1.1v | h7f8727e_0 3.7 MB defaults
  21. python_abi-3.10 | 2_cp310 4 KB conda-forge
  22. ------------------------------------------------------------
  23. Total: 4.7 MB
  24. The following NEW packages will be INSTALLED:
  25. conda-pack conda-forge/noarch::conda-pack-0.7.0-pyh6c4a22f_0
  26. python_abi conda-forge/linux-64::python_abi-3.10-2_cp310
  27. The following packages will be UPDATED:
  28. ca-certificates anaconda/pkgs/main::ca-certificates-2~ --> conda-forge::ca-certificates-2023.7.22-hbcca054_0
  29. conda anaconda/pkgs/main::conda-23.7.0-py31~ --> conda-forge::conda-23.7.2-py310hff52083_0
  30. openssl 1.1.1u-h7f8727e_0 --> 1.1.1v-h7f8727e_0
  31. The following packages will be SUPERSEDED by a higher-priority channel:
  32. certifi anaconda/pkgs/main/linux-64::certifi-~ --> conda-forge/noarch::certifi-2023.7.22-pyhd8ed1ab_0
  33. Proceed ([y]/n)? y
  34. Downloading and Extracting Packages
  35. Preparing transaction: done
  36. Verifying transaction: done
  37. Executing transaction: done
  1. 环境打包

命令:

conda pack -n 新环境名称 -o 新环境名称.tar.gz

具体操作如下:

  1. (base) mjl@ubuntu:~$ conda pack -n crawl-clone -o crawl-clone.tar.gz
  2. Collecting packages...
  3. Packing environment at '/home/mjl/miniconda3/envs/crawl-clone' to 'crawl-clone.tar.gz'
  4. [########################################] | 100% Completed | 6.3s
13. 加载导出的虚拟环境压缩包
  1. 在需要加载的新环境envs目录下解压文件
  1. (base) mjl@ubuntu:~$ mkdir /home/mjl/miniconda3/envs/crawl-clone2
  2. (base) mjl@ubuntu:~$ tar -zxvf crawl-clone.tar.gz -C /home/mjl/miniconda3/envs/crawl-clone2
  1. 激活环境
  1. (base) mjl@ubuntu:~$ conda env list
  2. # conda environments:
  3. #
  4. base * /home/mjl/miniconda3
  5. crawl /home/mjl/miniconda3/envs/crawl
  6. crawl-clone /home/mjl/miniconda3/envs/crawl-clone
  7. crawl-clone2 /home/mjl/miniconda3/envs/crawl-clone2
  8. novelai /home/mjl/miniconda3/envs/novelai
  9. (base) mjl@ubuntu:~$ conda activate crawl-clone2
  10. (crawl-clone2) mjl@ubuntu:~$
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览398348 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/200010
推荐阅读
相关标签
  

闽ICP备14008679号