conda 命令
Python is the most popular language for data scientists. Conda is the most common tool to create a virtual environment and manage packages for data scientists using Python.
Python是数据科学家最流行的语言。 onda Conda是使用Python创建虚拟环境和管理数据科学家的最常用工具。
Unfortunately, figuring out the best way to get conda on your machine and when to install packages from various channels isn’t straightforward. And it’s not easy to find the most useful commands for using conda and pip all in one place. ☹️
不幸的是,弄清楚在计算机上获取conda的最佳方法以及何时从各个渠道安装软件包并不是一件容易的事。 而且很难在一处找到使用conda和pip的最有用的命令。 ☹️
In this article I’m going to provide the essential conda commands and suggestions to help you avoid headaches with installation and use.
在本文中,我将提供必要的conda命令和建议,以帮助您避免安装和使用时的麻烦。
Let’s get to it!
让我们开始吧!
需要 (The Need)
Whether working locally or on a server in the cloud, you want a virtual environment to isolate your Python version and packages so that you can:
无论是本地工作还是在云中的服务器上工作,您都希望虚拟环境隔离您的Python版本和软件包,以便您可以:
- use different package versions for different projects 对不同的项目使用不同的软件包版本
- experiment with bleeding edge package versions 试用最新版本的边缘软件包
- get the same Python and package versions as your teammates so your shared code works similarly 与队友使用相同的Python和软件包版本,因此共享代码的工作方式相似
我必须使用Conda吗? (Do I have to use Conda?)
Nope. It’s very common in data analysis and scientific computing, but not so common outside.
不。 它在数据分析和科学计算中非常普遍,但在外部却不那么普遍。
There are lots of tools you can use to manage virtual Python environments. conda, Venv, pyenv, pipenv, poetry, Docker, and virtualenv are the most prominent. Trust me, you don’t even want to go down this rabbit hole.
您可以使用许多工具来管理虚拟Python环境。 conda , Venv , pyenv, pipenv , poetry , Docker和virtualenv最突出。 相信我,你甚至不想掉进这个兔子洞。
But if you must, here’s a Hacker News thread for a taste of the fun. And Yufeng G, Developer Advocate for Google Cloud, has a nice video on conda, virtualenv, and pyenv here.
但是,如果必须的话, 这里有一个黑客新闻主题,带您领略乐趣。 而裕丰摹 ,对于谷歌云计算开发人员倡导者,对畅达,virtualenv中,并pyenv一个不错的视频在这里 。
Today we’re exploring conda, so let’s dive in!
今天我们正在探索conda,所以让我们开始吧!
水蟒 (Anaconda)
Anaconda is both the name of the company behind the software and the name of the fully featured software distribution. The Anaconda company offers consulting and three Anaconda editions: individual, team, and enterprise. Individual is common and powerful. It’s not like there are freeware gotchas. It’s what I use.
Anaconda既是软件背后公司的名称,也是功能齐全的软件发行版的名称。 Anaconda公司提供咨询和三个Anaconda版本:个人,团队和企业。 个人是共同而强大的。 并不是有免费软件陷阱。 I这就是我用的。
发行:Anaconda vs Miniconda (Distributions: Anaconda vs Miniconda)
You can choose to download and install one of two distributions: Anaconda or Miniconda.
您可以选择下载并安装以下两个发行版之一: Anaconda或Miniconda 。
相似之处 (Similarities)
- Both Anaconda and Miniconda get you conda, the package manager and virtual environment manager in one. Anaconda和Miniconda都可以使您将conda(包管理器和虚拟环境管理器)合而为一。
- Both can be installed on Windows, Mac, or Linux systems. 两者都可以安装在Windows,Mac或Linux系统上。
- Both come with Python and pip installed. 两者都安装了Python和pip。
差异性 (Differences)
- The Anaconda distribution installs many Python packages and tools that are common for data science. Miniconda does not. It’s the difference between a big, batteries-included download and a minimal download without many packages installed for you. Anaconda发行版安装了许多数据科学通用的Python软件包和工具。 Miniconda没有。 这是包含电池的大型下载文件与没有为您安装许多程序包的最小下载文件之间的区别。
Miniconda install ranges between 50 and 90 MiB in mid-2020, depending on your operating system .
取决于您的操作系统,在2020年中期Miniconda的安装范围为50到90 MiB。
The Anaconda distribution requires a “minimum 5 GB disc space to download and install” — according to the docs in mid-2020.
根据2020年中期的文档,Anaconda发行版需要“至少5 GB的磁盘空间才能下载和安装”。
- The Anaconda distribution installer also includes options to install a GUI and other popular software such as the VSCode text editor. Anaconda发行安装程序还包括用于安装GUI和其他流行软件(例如VSCode文本编辑器)的选项。
If you are getting started with scientific computing using Python and have the space on your computer, I suggest you download the Anaconda distribution. It’s a lot of stuff, but it’s good stuff.
如果您开始使用Python进行科学计算并在计算机上拥有足够的空间,建议您下载Anaconda发行版。 这是很多东西,但这是好东西。
If you’ve worked with conda previously, I suggest you download Miniconda. You can add just the packages you need as you go.
如果您以前使用过conda,建议您下载Miniconda。 您可以随时添加所需的软件包。
See install instructions for Anaconda here and for Miniconda here.
请参阅安装说明Python这里和Miniconda 这里 。
conda命令 (Conda commands)
If you installed the Anaconda distribution you can use the GUI installer, but I suggest using the command line interface to help you work faster. You are a programmer after all (or becoming one)!
如果安装了Anaconda发行版,则可以使用GUI安装程序,但是我建议使用命令行界面来帮助您更快地工作。 毕竟您是一名程序员(或成为一名程序员)!
Let’s look at common conda commands to create and manage conda environments. Everything is the same whether you installed Anaconda or Miniconda. It’s all conda at this point.
让我们看一下创建和管理conda环境的常见conda命令。 无论您安装的是Anaconda还是Miniconda,一切都相同。 在这一点上,一切都是conda。
创建conda环境 (Create conda environments)
conda create -n myenv pandas jupyterlab
Create a new conda environment named myenv with the latest version of Python available on the main conda channel. Install pandas and jupyterlab packages into the environment. -n
is short for --name
.
conda create -n myenv pandas jupyterlab
创建一个名为myenv的新conda create -n myenv pandas jupyterlab
环境,并在conda create -n myenv pandas jupyterlab
主通道上使用最新版本的Python。 将pandas和jupyterlab软件包安装到环境中。 -n
是--name
缩写。
Conda asks for confirmation with package changes. Press y
when asked whether you want to proceed.
conda要求更改包裹确认。 当询问您是否要继续时,请按y
。
conda create -n myclone --clone myenv
Duplicate the environment named myenv into the new environment named myclone. All the packages come along for the ride!
conda create -n myclone --clone myenv
将名为myenv的环境复制到名为myclone的新环境中。 所有的包裹都来了!
conda create -n myenv python=3.8
Create a new conda environment named myenv with the Python version specified. Pip and about 15 other packages are installed.
conda create -n myenv python=3.8
使用指定的Python版本创建一个名为myenv的新conda环境。 Pip和大约15个其他软件包已安装。
管理conda环境 (Manage conda environments)
conda activate myenv
Activate the myenv virtual environment.
conda activate myenv
激活myenv虚拟环境。
You can tell you are in an active virtual environment because your command line prompt will start with the name of your virtual environment in parentheses. Like this:
您可以告诉您您处于活动的虚拟环境中,因为您的命令行提示符将以括号内的虚拟环境名称开头。 像这样:
(base) Jeffs-MBP:~ jeffhale$
The default conda environment is named base.
默认的conda环境名为base 。
Conda can be set up to activate an environment whenever you open your terminal. When installing the software the installer will ask you if you want to initialize it “by running conda init". I suggest you say yes so that your shell scripts get modified. Then you’ll start your terminal sessions in a conda environment.
只要您打开终端,就可以将Conda设置为激活环境。 在安装软件时,安装程序会询问您是否要“通过运行conda init来初始化它。我建议您说是,以便修改shell脚本。然后,您将在conda环境中启动终端会话。
Wherever you navigate in your on CLI, you will be within your activated conda virtual environment.
无论您在CLI上的何处浏览,都将处于激活的conda虚拟环境中。
conda deactivate
— Deactivate the current environment. Now you won’t be in a virtual environment. The conda environment you were in still exists, you just aren’t in it.
conda deactivate
—禁用当前环境。 现在,您将不在虚拟环境中。 您所处的conda环境仍然存在,只是您不在其中。
conda env list
— List conda environments.
conda env list
list-列出conda环境。
conda env remove --name myenv
— Remove the myenv conda environment. Delete it forever.
conda env remove --name myenv
—删除myenv conda环境。 永久删除。
在活动环境中管理软件包 (Manage packages in active environment)
Use these commands from within an active conda environment. These are the conda commands I use most frequently.
在活动的conda环境中使用这些命令。 这些是我最常使用的conda命令。
conda list
— List installed packages in the active environment. The output includes the package version and the conda channel the package was installed from.
conda list
—列出活动环境中已安装的软件包。 输出包括软件包版本和安装软件包的conda通道。
conda套餐 (Conda packages)
conda install pandas
— Download and install the pandas package from the main conda channel.
conda install pandas
—从主要conda频道下载并安装pandas软件包。
conda install -c conda-forge pandas
— Download and install the pandas package from the main conda-forge channel.
conda install -c conda-forge pandas
—从conda-forge主渠道下载并安装pandas软件包。
The conda-forge channel is “A community-led collection of recipes, build infrastructure and distributions for the conda package manager.” Use the conda-forge channel when a package doesn't exist on the main channel or the version on the main channel isn't as new as you would like. Packages often get updated on conda-forge before the main conda channel. ☝️
conda-forge渠道是“社区主导的食谱集,为conda软件包管理器构建基础结构和分发。” 当主频道中不存在软件包或主频道中的版本不是您想要的新版本时,请使用conda-forge频道。 软件包通常在主要conda通道之前在conda-forge上更新。 ☝️
You can also specify some other channel, but conda-forge is where you will find many packages.
您还可以指定其他一些通道,但是conda-forge是您可以找到许多软件包的地方。
conda update pandas
— Download and install the latest pandas package from the main conda channel.
conda update pandas
-从conda主频道下载并安装最新的pandas软件包。
conda uninstall pandas
— Uninstall the pandas package in your conda environment.
conda uninstall pandas
—在conda环境中卸载pandas软件包。
点子 (Pip)
Pip is the most common package installer. It ships with Python.
Pip是最常见的软件包安装程序。 它与Python一起提供。
When in an active conda environment, pip will install packages into that active conda environment. ⚠️
在活动的conda环境中时,pip会将软件包安装到该活动的conda环境中。 ⚠️
pip install -U pandas
— Install or update the pandas package from PyPI, the Python package index. -U
specifies to update all dependent packages.
pip install -U pandas
—从PyPI(Python软件包索引)安装或更新pandas软件包。 -U
指定更新所有从属软件包。
pip uninstall pandas
— Uninstall the pandas package that was installed by pip.
pip uninstall pandas
—卸载pip uninstall pandas
的pandas软件包。
PyPI is the most common place to find Python packages. PyPI often has packages that are not on conda or conda-forge. PyPI often gets the latest version of packages first.
PyPI是查找Python软件包的最常见的地方。 PyPI通常具有不在conda或conda-forge上的软件包。 PyPI通常会首先获得软件包的最新版本。
Occasionally conda has packages that aren’t on PyPI, but that is less common.
有时候,conda的程序包不在PyPI上,但这并不常见。
Conda and PyPI packages generally play together nicely.
Conda和PyPI软件包通常可以很好地配合使用。
Make sure that if you are installing a version of a package from PyPI you don't already have the same package installed from conda. If you do have it installed, then uninstall the conda version first. Otherwise conda won’t use the new PyPI version. By default, the official conda channel is the highest priority channel and conda will use package versions from higher priority channels first.☝️
确保如果要从PyPI安装软件包的版本,则尚未从conda安装相同的软件包。 如果已安装,请先卸载conda版本。 否则,conda将不会使用新的PyPI版本。 默认情况下,官方conda通道是优先级最高的通道,conda将首先使用优先级更高的通道的软件包版本.☝️
The official conda recommendation is to try to install packages from the main conda channel first, then conda-forge as necessary, and then pip. This helps avoid conflicts. In general, installing from any of those sources should work fine.
官方的conda建议是先尝试从主要conda通道安装软件包,然后根据需要安装conda-forge,然后再安装pip。 这有助于避免冲突。 通常,从任何这些来源进行安装都可以正常工作。
如果事情横盘整理 (If things ever go sideways)
Sometimes things get messed up. Generally uninstalling packages or creating a new conda environment is all you need to fix the situation.
有时候事情变得一团糟。 通常,解决问题只需要卸载软件包或创建新的conda环境。
If that doesn’t work, conda update conda
updates the conda software itself.
如果这不起作用,则conda update conda
更新conda软件本身。
If that doesn’t work, you can uninstall and reinstall conda.
如果那不起作用,则可以卸载并重新安装conda。
There are lots of conda users out there and lots of documentation, so as always, the search engine is your friend.
那里有很多conda用户和许多文档,因此,搜索引擎一如既往是您的朋友。
Source: pixabay.com 资料来源:foto.com包 (Wrap)
I hope you’ve enjoyed this overview of conda. If you did, please share it on your favorite social media so other folks can find it, too.
希望您喜欢conda的概述。 如果您这样做了,请在您喜欢的社交媒体上分享它,以便其他人也可以找到它。
I write about Python, SQL, Docker, and other tech topics. If any of that’s of interest to you, sign up for my mailing list of awesome data science resources and checkout out some of my 60+ articles you grow your skills here.
我撰写有关Python , SQL , Docker和其他技术主题的文章。 如果您有任何兴趣,请注册我的超棒数据科学资源邮件列表,并查看我的60多篇文章中的内容,您可以在这里提高自己的技能。
Happy conda-ing!
祝您生日快乐!
翻译自: https://towardsdatascience.com/13-conda-commands-for-data-scientists-e443d275eb89
conda 命令