为什么需要Python环境以及如何使用Conda对其进行管理

I have over two decades of professional experience as a developer, I know a wide variety of frameworks and programming languages, and one of my favorites is Python. I’ve been teaching it for quite some time now, and according to my experience, establishing Python environments is a challenging topic.

作为开发人员,我有二十多年的专业经验,我知道各种各样的框架和编程语言,而我最喜欢的之一是Python。 我已经教授了一段时间,根据我的经验, 建立Python环境是一个充满挑战的话题

Thus, my main motivation for writing this article was to help current and potential Python users to have a better understanding of how to manage such environments.

从而, 我撰写本文的主要动机是帮助当前和潜在的Python用户更好地了解如何管理此类环境。

If you’ve opened this article, chances are that you already know what Python is, why it is a great tool, and you even have a Python installed on your computer.

如果您打开了本文,可能是您已经知道Python是什么,为什么它是一个很好的工具,甚至您的计算机上已经安装了Python。

So why exactly do you need Python environments? You might ask: shouldn’t I just install the latest Python version?

那么,到底为什么需要Python环境呢? 您可能会问:我不应该只安装最新的Python版本吗?

为什么需要多个Python环境 (Why you need multiple Python environments)

When you start learning Python, it is a good starting point to install the newest Python version with the latest versions of the packages you need or want to play around with. Then, most likely, you immerse yourself in this world, and download Python applications from GitHub, Kaggle or other sources. These applications may need other versions of Python/packages than the ones you have been currently using.

当您开始学习Python时, 最好将最新的Python版本与您需要或想要使用的软件包的最新版本一起安装。 然后,很可能您将自己沉浸在这个世界中,并从GitHubKaggle或其他来源下载Python应用程序。 这些应用程序可能需要与您当前使用的版本不同的Python /软件包。

In this case, you need to set up different so-called environments.

在这种情况下,您需要设置不同的所谓环境

Aside from this situation, there are more use cases when having additional environments might come in handy:

除了这种情况之外,还有更多的用例可能会派上用场:

  • You have an application (developed by yourself or by someone else) that once worked beautifully. But now you’ve tried to run it, and it is not working. Perhaps one of the packages is no longer compatible with the other parts of your program (due to the so-called breaking changes). A possible solution is to set up a new environment for you application, that contains the Python version and the packages that are completely compatible with your application.

    您有一个曾经由您自己或他人开发的应用程序 ,可以很好地运行。 但是现在您已经尝试运行它了,但是它不起作用。 也许其中一个软件包不再与程序的其他部分兼容(由于所谓的重大更改 )。 一种可能的解决方案是为您的应用程序设置一个新环境,其中包含Python版本和与您的应用程序完全兼容的软件包。

  • You are collaborating with someone else, and you want to make sure that your application is working on your team member’s computer, and vice versa, so you can also set up an environment for your co-worker’s application(s).

    您正在与其他人进行协作 ,并且想要确保您的应用程序在团队成员的计算机上正常工作,反之亦然,因此您还可以为同事的应用程序建立环境。

  • You are delivering an application to your client, and again, you want to make sure that it is working smoothly on your client’s computer.

    您正在向客户端交付应用程序 ,并且再次要确保它在客户端计算机上可以正常运行。

An environment consists of a certain Python version and some packages. Consequently, if you want to develop or use applications with different Python or package version requirements, you need to set up different environments.

环境由某个Python版本和一些软件包组成。 因此,如果要开发或使用具有不同Python或软件包版本要求的应用程序 ,则需要设置不同的环境。

Now that we’ve discussed why environments are useful, let’s dive in and talk about some of the most important aspects of managing them.

既然我们已经讨论了为什么环境有用,那么让我们深入探讨一下管理环境的一些最重要方面。

包装和环境经理 (Package and environment managers)

The two most popular tools for setting up environments are:

两种最流行的环境设置工具是:

  • PIP (a Python package manager; funnily enough, it stands for “Pip Installs Packages”) with virtualenv (a tool for creating isolated environments)

    画中画 (Python软件包管理器;很有趣,它代表“ Pip Installs软件包”)与virtualenv (用于创建隔离环境的工具)

  • Conda (a package and environment manager)

    Conda (包装和环境经理)

In this article, I cover how to use Conda. I prefer it because:

在本文中,我介绍了如何使用Conda 。 我喜欢它是因为:

  1. Clear Structure: It is easy to understand its directory structure

    结构清晰 :易于理解其目录结构

  2. Transparent File Management: It doesn’t install files outside its directory

    透明文件管理 :它不会在其目录外安装文件

  3. Flexibility: It contains a lot of packages (PIP packages are also installable into Conda environments)

    灵活性 :它包含很多软件包(PIP软件包也可以安装到Conda环境中)

  4. Multipurpose: It is not only for managing Python environments and packages — you can also use it for R (a programming language for statistical computing)

    多用途 :它不仅用于管理Python环境和软件包,还可以用于R(一种用于统计计算的编程语言)

At the time of writing this article, I use the 4.3.x versions of Conda, but the new 4.4.x versions are also available.

在撰写本文时,我使用的是Conda的4.3.x版本,但也提供了新的4.4.x版本。

In the case of Conda 4.4, there have been recent changes affecting Linux/Mac OS X users. They are described in this changelog entry.

就Conda 4.4而言,最近发生的变化影响了Linux / Mac OS X用户。 它们在此变更日志条目中进行了描述。

如何选择合适的Conda下载选项 (How to choose an appropriate Conda download option)

Installing your Conda system is a bit more complicated than downloading a nice picture from Unsplash or buying a new ebook. Why is that?

与从Unsplash下载漂亮的图片或购买新的电子书相比, 安装Conda系统要复杂一些。 这是为什么?

1.安装程序 (1. Installer)

Currently, there are 3 different installers:

目前,有3种不同的安装程序

Let’s take a closer look at the free tools, Anaconda and Miniconda. Now, what are the main differences between these two?

让我们仔细看看免费工具AnacondaMiniconda 。 现在,这两者之间的主要区别是什么?

What are the things they share in common? They both set up on your computer

他们有什么共同点? 它们都在您的计算机上设置

  • the Conda (the package & environment management system) and

    Conda (包装和环境管理系统)和

  • the so-called “root environment” (more on that a bit later).

    所谓的“根环境” (稍后会详细介绍)。

As for the main differences, Miniconda requires about 400MB disk space, and it contains only a few basic packages.

至于主要区别, Miniconda需要大约400MB磁盘空间,并且仅包含一些基本软件包。

The Anaconda installer requires about 3GB disk space, and it installs over 150 scientific packages (for example, packages for statistics and machine learning). It also sets up the Anaconda Navigator, a GUI tool that helps you manage Conda environments and packages.

Anaconda安装程序大约需要3GB磁盘空间,并且安装了150多个科学软件包(例如,用于统计和机器学习的软件包)。 它还设置了Anaconda Navigator,这是一个GUI工具,可以帮助您管理Conda环境和软件包。

I prefer Miniconda, since I’ve never used most of the packages that are included in Anaconda by default. Another reason is that applying Miniconda allows for a smoother duplication of the environment (for example, if I want to use it on a different computer as well), since I only install the packages required by my app(s) on both computers.

我更喜欢Miniconda,因为默认情况下我从未使用过Anaconda中包含的大多数软件包。 另一个原因是,应用Miniconda可以使环境的复制更为顺畅(例如,如果我也想在其他计算机上使用它),因为我只在两台计算机上安装了我的应用所需的软件包。

From now on, I’m going to describe how Miniconda works (in the case of using Anaconda, the process is almost the same).

从现在开始,我将描述Miniconda的工作原理(在使用Anaconda的情况下,过程几乎相同)。

2–3。 平台(操作系统和位数) (2–3. Platform (operating system and bit-count))

In addition to these 3 different installers, there are also subtypes based on bit-count: 32- and 64-bit installers. And of course these also have subtypes for the different operating systems: Windows, Linux, and Mac OS X (except that the Mac OS X version is 64-bit only).

除了这3种不同的安装程序外,还有基于位计数的子类型: 32位和64位安装程序。 当然,它们也具有针对不同操作系统的子类型: Windows,Linux和Mac OS X (除了Mac OS X版本仅是64位)。

In this article, I focus on the Windows version (the Linux and the Mac OS X versions are only slightly different. For instance the path of the installation folders and some command line commands differ).

在本文中,我重点介绍Windows版本(Linux和Mac OS X版本仅稍有不同。例如,安装文件夹的路径和某些命令行命令不同)。

So 32-bit or 64-bit?

那么是32位还是64位?

If you have a 64-bit operating system (OS) with 4GB RAM or more, you should install the 64-bit version. Additionally, you might need a 64-bit installer if the packages you are planning to apply require the 64-bit versions of Python. For instance, if you want to use TensorFlow — more precisely, the official so-called binaries — you need a 64-bit OS and Python version.

如果您具有带4GB或更多RAM的64位操作系统(OS),则应安装64位版本。 此外,如果计划应用的软件包需要Python的64位版本,则可能需要64位安装程序。 例如,如果您想使用TensorFlow(更准确地说是官方的所谓二进制文件),则需要64位OS和Python版本。

If you have a 32-bit OS or you are planning to utilize packages that only have 32-bit versions, the 32-bit version is the good option for you.

如果您具有32位操作系统,或计划使用仅具有32位版本的软件包,则32位版本是您的理想选择。

4. Python版本(用于根环境) (4. Python version (for the root environment))

If these 3 dimensions aren’t enough (installers, 32/64-bit, and operating systems), there is a 4th one based on the different Python versions (

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值