前言:
此时看官们对“python控制结构一educoder”大概比较注意,朋友们都想要剖析一些“python控制结构一educoder”的相关文章。那么小编也在网上汇集了一些有关“python控制结构一educoder””的相关文章,希望看官们能喜欢,姐妹们快快来学习一下吧!本篇涉及的知识点conda是什么,3个一官网:最棒资源两个版本:建议安装miniconda版安装miniconda基本命令环境:创建/删除查看既存环境:conda env list创建虚拟环境:conda create -n [环境名称] [安装库包列表]查看环境列表:conda env list引申知识启动新创建环境:oldgeek-study新创建环境中安装所需包:oldgeek-study退出新环境:输入exit删除环境:conda remove -n [环境名称] --all创建个低版本的环境:Python3.8克隆一个base环境,以备不实之需环境包导入和导出镜像终端执行下面命令添加国内的镜像:咱们清华大学开源的镜像站骚操作删除conda速查宝典查看帮助和版本环境相关日常命令通道相关
华山论剑:conda & pip参考资料对话
大家好,我是老码农,这是小码匠数据科学之旅的第5篇文章。
本文重点分享Python的包管理工具和环境管理工具:conda。
未来的日子中,期待和大家一起成长,一起分享高质量原创文章
闭嘴,安静的看文章
小码匠:今天学什么?
老码农:你每天放学回来开口第一句,过于雷同了,有没有点创意啊?你是小孩,不要像我们大人被岁月慢慢磨掉了棱角,变得循规蹈矩,毫无创意。
小码匠:就一句话也被你奚落,那我闪了,不学了。
老码农:甩锅,你敢。
小码匠:你又不舍得揍我,吓唬我不管用的。
老码农:好啦,今天我写了篇文章,你自己静下心来,好好读读,有不懂的我们在探讨。学习这个东西,还是要靠自己的,手把手教你,不是好事。再说,你自学能力这么强,也不需要我手把手教啊。
小码匠:老码农,你就会忽悠我,斗不过你,那我看文章了啊。
老码农:嗯,那你慢慢读,精读的哟。
conda是什么,3个一一个能支持Python、R、Java、JavaScript、C等语言包、依赖和环境管理工具一个能在Windows、MacOS、Linux上运行开源的软件包管理系统和环境管理系统一个能在本地轻松创建、保存、切换环境官网:最棒资源多看官方文档,网络上知识碎片化太严重,官方文档最新、最严谨、最全,是我们学习知识的最佳场所官网:两个版本:建议安装miniconda版anaconda:包含常用的包,个人版就有440MB,里面包罗万象,但很多东西你未必用到,所以才有mini版Mac版本:440MB(2021年11月3日的Mac版本)miniconda:miniconda则是精简版,需要啥装啥,所以推荐使用minicondaMac版本:52.3MB(2021年11月3日的Mac版本),大小差了9倍,虽然硬盘不值钱,但咱也没必要浪费啊。根据自己电脑的操作系统选择,下载相应的安装包。
安装miniconda
“
小码匠,教你一个秘籍,大部分安装程序你一路回车就能搞定
”
小码将批注
“
老码农,这哪是秘籍,废话当宝贝? 你自己收着吧
”
Step1: 按【继续】Step2: 继续按【继续】Step3: 继续按【继续】Step4: 继续按【同意】Step5: 继续按【安装】,几秒后就安装完毕基本命令确认版本:conda -v
(base) coder@192 pycharm % conda -V conda 4.10.3(base) coder@查看命令参数: conda, 可以看到子命令有clean、help、list、install、search等子命令
(base) coder@192 ~ % condausage: conda [-h] [-V] command ...conda is a tool for managing and deploying applications, environments and packages.Options:positional arguments: command clean Remove unused packages and caches. compare Compare packages between conda environments. config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (/Users/cynthia/.condarc) by default. create Create a new conda environment from a list of specified packages. help Displays a list of available conda commands and their help strings. info Display information about current conda install. init Initialize conda for shell interaction. [Experimental] install Installs a list of packages into a specified conda environment. list List linked packages in a conda environment. package Low-level conda package utility. (EXPERIMENTAL) remove Remove a list of packages from a specified conda environment. uninstall Alias for conda remove. run Run an executable in a conda environment. [Experimental] search Search for packages and display associated information. The input is a MatchSpec, a query language for conda packages. See examples below. update Updates conda packages to the latest compatible version. upgrade Alias for conda update.optional arguments: -h, --help Show this help message and exit. -V, --version Show the conda version number and exit.conda commands available from other packages: env查看子命令帮助信息: conda [子命令] -h
(base) coder@192 ~ % conda create -husage: conda create [-h] [--clone ENV] [-n ENVIRONMENT | -p PATH] [-c CHANNEL] [--use-local] [--override-channels] [--repodata-fn REPODATA_FNS] [--strict-channel-priority] [--no-...Options:positional arguments: package_spec Packages to install or update in the conda environment....Target Environment Specification: -n ENVIRONMENT, --name ENVIRONMENT Name of environment. -p PATH, --prefix PATH Full path to environment location (i.e. prefix)....环境:创建/删除
conda最有特色的点,可以便利创建不同的开发环境,对开发环境进行管理
查看既存环境:conda env list
(base) coder@192 ~ % conda env list# conda environments:# /Applications/JupyterLab.app/Contents/Resources/jlab_serverbase * /Users/coder/opt/miniconda3创建虚拟环境:conda create -n [环境名称] [安装库包列表]下面示例:环境名称:oldgeek-study,环境默认安装python3.10版
(base) coder@192 ~ % conda create -n oldgeek-study python=3.10Collecting package metadata (current_repodata.json): doneSolving environment: done## Package Plan ## environment location: /Users/coder/opt/miniconda3/envs/oldgeek-study added / updated specs: - python=3.10The following packages will be downloaded: package | build ---------------------------|----------------- certifi-2020.6.20 | pyhd3eb1b0_3 155 KB ncurses-6.3 | hca72f7f_0 856 KB pip-21.2.4 | py310hecd8cb5_0 1.8 MB python-3.10.0 | h88f2d9e_1 10.1 MB setuptools-58.0.4 | py310hecd8cb5_0 782 KB tzdata-2021e | hda174b7_0 112 KB ------------------------------------------------------------ Total: 13.8 MBThe following NEW packages will be INSTALLED: bzip2 pkgs/main/osx-64::bzip2-1.0.8-h1de35cc_0 ca-certificates pkgs/main/osx-64::ca-certificates-2021.10.26-hecd8cb5_2 certifi pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3 libcxx pkgs/main/osx-64::libcxx-12.0.0-h2f01273_0 libffi pkgs/main/osx-64::libffi-3.3-hb1e8313_2 ncurses pkgs/main/osx-64::ncurses-6.3-hca72f7f_0 openssl pkgs/main/osx-64::openssl-1.1.1l-h9ed2024_0 pip pkgs/main/osx-64::pip-21.2.4-py310hecd8cb5_0 python pkgs/main/osx-64::python-3.10.0-h88f2d9e_1 readline pkgs/main/osx-64::readline-8.1-h9ed2024_0 setuptools pkgs/main/osx-64::setuptools-58.0.4-py310hecd8cb5_0 sqlite pkgs/main/osx-64::sqlite-3.36.0-hce871da_0 tk pkgs/main/osx-64::tk-8.6.11-h7bc2e8c_0 tzdata pkgs/main/noarch::tzdata-2021e-hda174b7_0 wheel pkgs/main/noarch::wheel-0.37.0-pyhd3eb1b0_1 xz pkgs/main/osx-64::xz-5.2.5-h1de35cc_0 zlib pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3Proceed ([y]/n)?输入:y,创建虚拟环境
Proceed ([y]/n)? yDownloading and Extracting Packagesncurses-6.3 | 856 KB | ############################################################################## | 100% tzdata-2021e | 112 KB | ############################################################################## | 100% setuptools-58.0.4 | 782 KB | ############################################################################## | 100% pip-21.2.4 | 1.8 MB | ############################################################################## | 100% certifi-2020.6.20 | 155 KB | ############################################################################## | 100% python-3.10.0 | 10.1 MB | ############################################################################## | 100% Preparing transaction: doneVerifying transaction: doneExecuting transaction: done## To activate this environment, use## $ conda activate oldgeek-study## To deactivate an active environment, use## $ conda deactivate(base) cynthia@192 ~ %查看环境列表:conda env list
(base) coder@192 ~ % conda env list# conda environments:# /Applications/JupyterLab.app/Contents/Resources/jlab_serverbase * /Users/coder/opt/miniconda3oldgeek-study /Users/coder/opt/miniconda3/envs/oldgeek-study引申知识Step1: 创建虚拟环境时安装包:requests、numpy 环境名称:coder-study
conda create -n coder-study requests numpyStep2: 在Step1创建的环境上还想继续安装包:scripy
conda install -n coder-study scipy启动新创建环境:oldgeek-study
(base) coder@192 ~ % conda activate oldgeek-study(oldgeek-study) coder@192 ~ %新创建环境中安装所需包:oldgeek-study安装网络HTTP包:rquests,悲剧安装失败
(oldgeek-study) coder@192 ~ % conda install requestsCollecting package metadata (current_repodata.json): doneSolving environment: failed with initial frozen solve. Retrying with flexible solve.Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.Collecting package metadata (repodata.json): doneSolving environment: failed with initial frozen solve. Retrying with flexible solve.Solving environment: | Found conflicts! Looking for incompatible packages.This can take several minutes. Press CTRL-C to abort.failed UnsatisfiableError: The following specifications were found to be incompatible with each other:Output in format: Requested package -> Available versions在conda创建的环境中也可以使用pip安装,执行命令:pip install requestspip 是一个Python包管理工具,主要是用于安装 PyPI 上的软件包。
(oldgeek-study) coder@192 ~ % pip install requestsCollecting requests Using cached requests-2.26.0-py2.py3-none-any.whl (62 kB)Collecting certifi>=2017.4.17 Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB) |████████████████████████████████| 149 kB 164 kB/s Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.7-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 67 kB/s Collecting idna<4,>=2.5 Downloading idna-3.3-py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 16 kB/s Collecting charset-normalizer~=2.0.0 Downloading charset_normalizer-2.0.7-py3-none-any.whl (38 kB)Installing collected packages: urllib3, idna, charset-normalizer, certifi, requestsSuccessfully installed certifi-2021.10.8 charset-normalizer-2.0.7 idna-3.3 requests-2.26.0 urllib3-1.26.7
使用pip安装成功,那我们用conda list查看安装情况
(oldgeek-study) coder@192 ~ % conda list# packages in environment at /Users/coder/opt/miniconda3/envs/oldgeek-study:## Name Version Build Channelbzip2 1.0.8 h1de35cc_0 ca-certificates 2021.10.26 hecd8cb5_2 certifi 2020.6.20 pyhd3eb1b0_3 libcxx 12.0.0 h2f01273_0 libffi 3.3 hb1e8313_2 ncurses 6.3 hca72f7f_0 openssl 1.1.1l h9ed2024_0 pip 21.2.4 py310hecd8cb5_0 python 3.10.0 h88f2d9e_1 readline 8.1 h9ed2024_0 setuptools 58.0.4 py310hecd8cb5_0 sqlite 3.36.0 hce871da_0 tk 8.6.11 h7bc2e8c_0 tzdata 2021e hda174b7_0 wheel 0.37.0 pyhd3eb1b0_1 xz 5.2.5 h1de35cc_0 zlib 1.2.11 h1de35cc_3 (oldgeek-study) cynthia@192 ~ % pip listPackage Version------------------ ---------certifi 2021.10.8charset-normalizer 2.0.7idna 3.3pip 21.2.4requests 2.26.0setuptools 58.0.4urllib3 1.26.7(oldgeek-study) cynthia@192 ~ %悲剧,没有查到,用conda安装的包,conda都能进行管理,因为我们使用pip安装的,所以查看不到,没关系, 我们用pip list可以继续查看,能看到吧。自己管理自己的东西,别人管理的与我何关
(oldgeek-study) coder@192 pycharm % pip listPackage Version----------------------------- ---------anyio 3.3.4appnope 0.1.2...requests 2.26.0安装包: 科学计算包:NumPy
(oldgeek-study) coder@192 ~ % conda install numpyCollecting package metadata (current_repodata.json): doneSolving environment: failed with initial frozen solve. Retrying with flexible solve.Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.Collecting package metadata (repodata.json): doneSolving environment: failed with initial frozen solve. Retrying with flexible solve.Solving environment: | Found conflicts! Looking for incompatible packages.This can take several minutes. Press CTRL-C to abort.failed UnsatisfiableError: The following specifications were foundto be incompatible with the existing python installation in your environment:Specifications: - numpy -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.8,<3.9.0a0|>=3.9,<3.10.0a0|>=3.5,<3.6.0a0']Your python: python=3.10If python is on the left-most side of the chain, that's the version you've asked for.When python appears to the right, that indicates that the thing on the left is somehownot available for the python version you are constrained to. Note that conda will notchange your python version to a different minor version unless you explicitly specifythat.
悲剧:没有安装成功,我们太新潮了,python3.10刚出来,numpy尚未来的及对python3.10的支持,所以直接安装挂了。
建议:
学习了解新功能,可以安装新版本生产环境:切换新版本一定要注意,要经过仔细测试,无问题后才可以在生产切换,切勿未经测试,直接切换,酿成重大生产事故。退出新环境:输入exit
(oldgeek-study) coder@192 ~ % exitSaving session......copying shared history......saving history...truncating history files......completed.[进程已完成]删除环境:conda remove -n [环境名称] --all
(oldgeek-study) coder@192 ~ % conda remove -n oldgeek-study --allCondaEnvironmentError: cannot remove current environment. deactivate and run conda remove again创建个低版本的环境:Python3.8科学计算包:numpy数学包:scipy符号包:sympy可视化包:matplotlib seaborn
conda create -n oldgeek-study python=3.8 numpy scipy sympy matplotlib seaborn克隆一个base环境,以备不实之需
“
后面oldgeek-study环境在安装新包时出了问题,可以直接用这个base环境,继续搞,省去了初始化安装必要包的时间
”
(base) coder@192 ~ % conda create -n coder-base --clone oldgeek-studyWARNING: A directory already exists at the target location '/Users/coder/opt/miniconda3/envs/coder-base'but it is not a conda environment.Continue creating environment (y/[n])? ySource: /Users/coder/opt/miniconda3/envs/oldgeek-studyDestination: /Users/coder/opt/miniconda3/envs/coder-basePackages: 131Files: 0Preparing transaction: doneVerifying transaction: doneExecuting transaction: done## To activate this environment, use## $ conda activate coder-base## To deactivate an active environment, use## $ conda deactivate环境包导入和导出场景
老码农在自己的电脑环境:coder-base安装了许多软件包,一直运行很稳定
小码匠自己瞎捣鼓,把自己环境弄挂了,着急想尽快重新构建个环境,那咋办呢。
导出base环境yml文件
(base) coder@192 ~ % conda env export --file coder-base.yml --name coder-base查看yml文件内容,哦,文件内容原来是安装包的列表
(base) coder@192 ~ % cat coder-base.yml name: coder-basechannels: - conda-forge - defaultsdependencies: - anyio=3.3.4=py38h50d1736_0 - appnope=0.1.2=py38h50d1736_2 - argon2-cffi=21.1.0=py38h96a0964_1 - async_generator=1.10=py_0 - attrs=21.2.0=pyhd8ed1ab_0 - babel=2.9.1=pyh44b312d_0 - backcall=0.2.0=pyh9f0ad1d_0 - backports=1.0=py_2 - backports.functools_lru_cache=1.6.4=pyhd8ed1ab_0利用刚导出的文件列表,直接创建
conda env create -f coder-base.yml镜像
场景:我们有时安装软件包时,经常会很慢,有时还直接挂了,那有没有提速的办法呢。
是什么原因呢?
安装完conda,默认我们会去官方镜像站点拉取包,官方的服务器都在国外,网络一旦不稳定,自然我们拉取软件包会很慢。
conda config --add channels biocondaconda config --add channels conda-forge终端执行下面命令初次执行,会在当前用户的目录下面生成一个.condarc,文件位置在当前用户的目录下
conda configMac端,执行ls ~/.co*,是不是看到了有个.condarc文件,这个是conda的配置文件
(base) coder@192 ~ % ls ~/.co*/Users/coder/.codota-id /Users/coder/.condarc查看文件内容
(base) coder@192 ~ % cat ~/.condarcshow_channel_urls: true查看channel,空空哒,我们没有添加任何配置,自然是空空哒
(base) cynthia@192 ~ % conda config --get channels忘了很重要的一点:conda info,执行该命令,能查看conda现在的配置信息,信息很丰富
(base) coder@192 ~ % conda info active environment : base active env location : /Users/coder/opt/miniconda3 shell level : 1 user config file : /Users/coder/.condarc populated config files : /Users/coder/.condarc conda version : 4.10.3 conda-build version : not installed python version : 3.9.5.final.0 virtual packages : __osx=10.16=0 __unix=0=0 __archspec=1=x86_64 base environment : /Users/coder/opt/miniconda3 (writable) conda av data dir : /Users/coder/opt/miniconda3/etc/conda conda av metadata url : None channel URLs : package cache : /Users/coder/opt/miniconda3/pkgs /Users/coder/.conda/pkgs envs directories : /Users/coder/opt/miniconda3/envs /Users/coder/.conda/envs platform : osx-64 user-agent : conda/4.10.3 requests/2.25.1 CPython/3.9.5 Darwin/20.6.0 OSX/10.16 UID:GID : 501:20 netrc file : None offline mode : False添加国内的镜像:咱们清华大学开源的镜像站
conda config --add channels config --add channels config --add channels config --add channels显示安装的频道
(base) coder@192 ~ % conda config --get channels--add channels 'defaults'# lowest priority--add channels ';--add channels ';--add channels ';--add channels ';# highest priority骚操作我想切换到小码匠的学习环境,通常我们这样做
conda activate coder-study换个姿势,我能不能直接输入coder,就可以直接进入呢?当然可以了
(base) coder@192 ~ % vim ~/.bash_profile
# >>> conda initialize >>># !! Contents within this block are managed by 'conda init' !!__conda_setup="$('/Users/coder/opt/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"if [ $? -eq 0 ]; then eval "$__conda_setup"else if [ -f "/Users/coder/opt/miniconda3/etc/profile.d/conda.sh" ]; then . "/Users/coder/opt/miniconda3/etc/profile.d/conda.sh" else export PATH="/Users/coder/opt/miniconda3/bin:$PATH" fifiunset __conda_setup# <<< conda initialize <<<export PATH=/System/Volumes/Data/fgb/04.tool/apache-maven-3.8.1/bin:$PATHexport PATH="$PATH":/usr/local/mysql/binexport HOMEBREW_BOTTLE_DOMAIN= PATH=${PATH}:/Library/Frameworks/Python.framework/Versions/3.10/bin/python3alias python="/Library/Frameworks/Python.framework/Versions/3.10/bin/python3"alias coder="conda activate coder-study"
(base) coder@192 ~ % source ~/.bash_profile
(base) coder@192 ~ % coder(coder-study) cynthia@192 ~ %删除condaMac查看安装路径:conda env list,我们可以看到默认是安装在/Users/cynthia/opt/miniconda3
(base) coder@192 opt % conda env list# conda environments:# /Applications/JupyterLab.app/Contents/Resources/jlab_serverbase * /Users/coder/opt/miniconda3移除conda,使用rm命令
rm -rf /Users/coder/opt/miniconda3Windows:去控制面板,点击“添加或删除程序”,选择miniconda并点击删除程序。就不截图了。速查宝典官方查看帮助和版本
命令
说明
conda
查看命令列表
conda [子命令] -h 栗子:conda list -h
查看自己命令帮助信息
conda -V
查看版本号
conda info
查看配置信息
conda
环境相关
命令
说明
conda create -n [env_name]
创建环境
conda create -n [env_name] python=3.8
创建环境并安装Python指定版本
conda create -n [env_name] python=3.8 dumpy
创建环境并安装Python指定版本并安装其他包
conda remove -n [env_name] --all
移除环境
conda activate [env_name]
进入环境
环境内直接输入:exit
退出环境
conda create --name new_env_name --clone base_env_name
克隆环境
conda env export --file [文件名字.yml] --name [env_name]
栗子:conda env export --file coder-base.yml --name coder-base
导出环境的包列表
conda env create -f [文件名字.yml]
conda env create -f coder-base.yml
基于导出的yml文件创建新环境
日常命令
命令
说明
conda install [package_name]
安装包
caonda list
查看已安装列表
conda search [package_name]
搜索待安装包信息
conda update [package_name]
更新包
conda remove [package_name]
删除包
conda clean -t
直接清除缓存包
conda clean -y -a
直接清除索引缓存、未使用缓存包
通道相关
命令
说明
conda config --add channels
conda config --add channels
conda config --add channels
conda config --add channels
更换源:栗子是清华大学的通道
conda config --show channels
查看已安装的通道
conda config --remove-key channels
恢复默认通道
conda update [package_name]
更改通道
conda config --remove channels
删除某个通道
华山论剑:conda & pip
Command reference
命令
Conda
Pip
Virtualenv
安装包
conda install $PACKAGE_NAME
pip install $PACKAGE_NAME
X
更新包
conda update --name $ENVIRONMENT_NAME $PACKAGE_NAME
pip install --upgrade $PACKAGE_NAME
X
更新管理器
conda update conda
Linux/macOS: pip install -U pip Win: python -m pip install -U pip
X
卸载包
conda remove --name $ENVIRONMENT_NAME $PACKAGE_NAME
pip uninstall $PACKAGE_NAME
X
创建环境
conda create --name $ENVIRONMENT_NAME python
X
cd $ENV_BASE_DIR; virtualenv $ENVIRONMENT_NAME
进入环境
conda activate $ENVIRONMENT_NAME*
X
source $ENV_BASE_DIR/$ENVIRONMENT_NAME/bin/activate
退出环境
conda deactivate
X
deactivate
搜寻可用包
conda search $SEARCH_TERM
pip search $SEARCH_TERM
X
从指定通道安装包
conda install --channel $URL $PACKAGE_NAME
pip install --index-url $URL $PACKAGE_NAME
X
显示已安装包
conda list --name $ENVIRONMENT_NAME
pip list
X
获取已安装包
conda list --export
pip freeze
X
查看环境列表
conda info --envs
X
Install virtualenv wrapper, then lsvirtualenv
安装包
conda install pip
pip install conda
X
安装Python指定版本
conda install python=x.x
X
X
卸载Python
conda update python*
X
参考资料官网用户指南安装命令速查精简版下载和安装对话
小码匠:累死宝宝了,老码农,你为啥整理这么多啊,看着有点累啊。
老码农:这份文档绝大部分问题你都可以解决,可以快速查找。
小码匠:那也不用一次弄这么细吧。
老码农:小码匠,很严肃的话题,学习要注重效率,梳理资料,搞N次,每次都丢三落四的,会严重影响未来的效率。一次搞全,后面再补充新知识补充就好了。要养成好的学习习惯,才能事倍功半的。
梳理资料
要有目录结构,便于查看的同学快速定位到自己想看的知识尽量系统,梳理一遍也是梳理,二遍也是梳理,为啥不一次搞好呢资料要确认,不要随意拷贝,错误的知识耽误自己,更耽误别人
小码匠:你的要求好高啊,我尽量争取吧,不能成为你的拖油瓶。
老码农:你不会的,因为你要成为码匠的。
标签: #python控制结构一educoder