适用于Matlab用户的python第4部分环境和软件包

Remember all problems you’ve had throughout the years regarding developers having different Matlab versions:

记住这些年来您遇到的有关具有不同Matlab版本的开发人员的所有问题:

You’ve made this great script that you push to Git, but suddenly your colleague calls you up and asks why you pushed code that doesn’t work? You tell her that it works on your computer, and after a 30-minute Teams meeting with screen-sharing, you realise it’s because you used a classifier from the Classification Learner toolbox which is not available in her 2018b Matlab version, while you are rocking the newer 2020a version.

您已经制作了一个很棒的脚本,将其推送到Git,但是突然之间,您的同事打电话给您,问您为什么推送了无效的代码? 您告诉她它可以在您的计算机上运行,​​并且在30分钟的团队会议与屏幕共享会面之后,您意识到这是因为您在摇摆的同时使用了Classification Learner工具箱中的分类器,该分类器在她的2018b Matlab版本中不可用。较新的2020a版本。

The bad news is that problems like this also occur in Python. The good news is that it’s much easier to handle than in Matlab, since installing a different version of Python is much easier and takes up less space, than having different versions of Matlab on the same PC.

坏消息是在Python中也会出现类似的问题。 好消息是,比在Matlab中处理起来要容易得多,因为与在同一台PC上安装不同版本的Matlab相比,安装不同版本的Python更容易并且占用更少的空间。

Having installed Conda as described in Step 2, the easiest way to overcome this is to use Conda environments.

步骤2中所述安装Conda后,解决此问题的最简单方法是使用Conda环境。

什么是模块和软件包? (What are modules and packages?)

A key feature of Python is the use of modules and packages. The reason why we prefer using these is that we don’t have to have a huge installation of all possible features (hint, hint, Matlab) lying on our PC: Instead, we can add features when needed (by importing modules and packages), and only when needed. It also allows for easier organisation of our code, and for re-using features in other contexts.

Python的主要功能是使用模块和包。 我们之所以喜欢使用这些功能,是因为我们不必在PC上大量安装所有可能的功能( hint,hint,Matlab ):相反,我们可以在需要时添加功能(通过导入模块和包) ,并且在需要时使用。 它还可以简化我们的代码的组织,并允许在其他上下文中重用功能。

Modules:In programming, “ a module is a piece of software that has a specific functionality“ [1]. The Python language itself is quite bare-bone, meaning one would have to import modules to get additional functionalities.One such module could be the random module, which implements a pseudo-random number generator.

模块:在编程中,“ 模块是一种具有特定功能的软件 ” [ 1 ]。 Python语言本身非常准,这意味着必须导入模块才能获得其他功能,其中一个模块可能是random模块,该模块实现了伪随机数生成器。

Packages: Packages are namespaces (or libraries) which contain multiple packages and modules themselves. One such example is the numpy package, which provides support for arrays and matrices and mathematical functions for manipulating them (similar to what is done in Matlab).

软件包:软件包是名称空间(或库) ,它们本身包含多个软件包和模块numpy包就是这样的例子,它提供了对数组和矩阵的支持,以及对它们进行操作的数学函数(类似于Matlab中的实现)。

conda环境 (Conda Environments)

Image for post
Despite the name, Conda environments don’t have anything to do with global warming. Photo by John O’Nolan on Unsplash
尽管名称如此,但Conda环境与全球变暖没有任何关系。 John O'NolanUnsplash拍摄的照片

Environments is a feature in Conda which doesn’t really exists in Matlab. But it can prove very useful when working on different project which require different packages (or different versions of packages). So instead of having one version of Python installed with all the necessary packages (as in Matlab), you can make an environment for each project you work on, which would only contain the packages needed for that project. This also helps making the code more portable.

环境是Conda中的一项功能,而Matlab中实际上并不存在。 但是当在需要不同软件包(或不同版本的软件包)的不同项目中工作时,它可能会非常有用。 因此,您不必为所有需要的软件包安装一个Python版本(如在Matlab中那样),而是可以为您从事的每个项目创建一个环境,其中仅包含该项目所需的软件包。 这也有助于使代码更具可移植性。

There are a few alternatives to these Conda environments, e.g. Pipenv and VirtualEnv. However, Conda is the best option as it provides a much simpler interface [2]

这些Conda环境有一些替代方案,例如Pipenv和VirtualEnv。 但是,Conda是最好的选择,因为它提供了更加简单的界面[ 2 ]

The following list is based on the great guide made by [3].

以下列表是基于[ 3 ]的出色指南。

1.列出可用环境 (1. List available environments)

You can list the all available Conda environments on your machine as follows:

您可以如下列出计算机上所有可用的Conda环境:

conda info --env

2.创建一个新环境 (2. Create a new environment)

You can create a new blank environment called <env_name> with python version x.x very easily in Conda. (To se a list of available python versions first, type conda search "python" or conda search "^python$" and press enter.)

您可以在Conda中轻松地使用python版本xx创建一个名为<env_name>的新空白环境。 (要首先获取可用python版本的列表,请键入conda search "python"conda search "^python$" ,然后按Enter。)

conda create -n <env_name> python=x.x

You can also create a new environment by cloning an existing environment (e.g. base). This is useful if you want to build upon an existing environment instead of starting from scratch.

您还可以通过克隆现有环境(例如base )来创建新环境。 如果您要在现有环境上构建而不是从头开始,这将很有用。

conda create --name <env_name> --clone base

Another option is to make an environment with the packages included in the default Anaconda installation:

另一种选择是使用默认的Anaconda安装中包含的软件包创建环境:

conda create --name <env_name> anaconda python=x.x

Finally, you can also create an environment with a predefined set of packages, e.g. numpy and scipy:

最后,您还可以使用一组预定义的包(例如numpyscipy)创建一个环境:

conda create -n <env_name> numpy scipy

3.激活环境 (3. Activate an environment)

You can activate an environment by typing the following, where <env_name> is the name of your environment, (e.g. base as done above).

您可以通过键入以下内容来激活环境,其中<env_name>是您环境的名称(例如,上面的基数 )。

conda activate <env_name>

When activating an environment, you automatically move to that environment with that environments Python version and packages available.

激活环境时,您将自动使用该环境的Python版本和软件包迁移到该环境。

In the environment, you can start Python by typing python in the terminal, just as in the base environment, but you will now be using the environments local python instead of the global base python.

在环境中,您可以通过在终端中键入python来启动Python,就像在基本环境中一样,但是现在您将使用环境本地python而不是全局基本 python。

NOTE: If you get an error regarding pip or Python in your environment and the error message is something like “cannot find Python”, then it might be because you have non-ASCII characters in you user folder, which will make it impossible to find any of the packages installed in your environment. To fix this, the only option is to rename you user folder, by following the steps described here.

注意:如果在您的环境中遇到有关pip或Python的错误,并且错误消息类似于“找不到Python”,则可能是因为您的用户文件夹中包含非ASCII字符,这使得找不到您环境中安装的任何软件包。 要解决此问题,唯一的选择是按照 此处 描述的步骤来重命名用户文件夹

4.在环境中安装其他软件包 (4. Install additional packages in an environment)

To see which packages are available in a certain environment with following command (this works with any environment created with Conda):

使用以下命令查看在特定环境中可用的软件包(这适用于使用Conda创建的任何环境):

conda list -n <env_name>

To install a new package in an environment, you type one of the following, depending on whether you want the latest or a certain version (version x.x ) of the package :

要在环境中安装新软件包,请键入以下内容之一,具体取决于您需要软件包的最新版本还是特定版本( xx版):

conda install -n <env_name> <package_name>conda install -n <env_name> <package_name>=x.x

5.导出环境 (5. Exporting an environment)

One of the great things about environments is that they can be exported for others (or yourself) to easily get the same setup as you have, with exactly the same Python version and exactly the same packages. You export an environment to an yml-file like this:

关于环境的一大优点是,可以使用完全相同的Python版本和完全相同的软件包将它们导出给其他人(或您自己),以轻松地获得与您相同的设置。 您可以将环境导出到yml -file,如下所示:

conda env export --name 

This can then be imported on the other PC by simply creating a new env using the yml file.

然后,只需使用yml文件创建一个新的环境,就可以将其导入另一台PC上。

conda env create -f envname.yml

6.停用环境 (6. Deactivate an environment)

To end a session in the current environment, enter the following. Note that it is not necessary to specify the environment name — whichever is currently active will be deactivated, and the shell variables will be returned to normal.

要在当前环境中结束会话,请输入以下内容。 注意,没有必要指定环境名称-当前处于活动状态的环境都将被停用,并且shell变量将恢复为正常状态。

conda deactivate

7.删除不再需要的虚拟环境 (7. Delete a no longer needed virtual environment)

To delete a Conda environment, enter the following, where <env_name> is the name of the environment you wish to delete.

要删除Conda环境,请输入以下内容,其中<env_name>是您要删除的环境的名称。

conda remove --name <env_name> -all
conda env remove -n <env_name>

手动安装软件包 (Installing packages manually)

Image for post
Photo by Zoya Loonohod on Unsplash
Zoya LoonohodUnsplash上的 照片

You can install additional packages in Python in two ways: Using conda and using pip, which is a package manager commonly used among Python users.

您可以通过以下两种方式在Python中安装其他软件包:使用conda和使用pip ,这是Python用户常用的软件包管理器。

Pip vs Conda is a huge discussion topic in itself, as both are ways of providing packages to your python installation. But Jake VanderPlas summarizes the difference quite well in his blog [4]:

Pip vs Conda本身就是一个巨大的讨论话题,因为两者都是向python安装提供软件包的方式。 但是杰克·范德普拉斯(Jake VanderPlas)在他的博客中很好地总结了差异[ 4 ]:

Conda and pip are different projects with different intended audiences: pip installs python packages within any environment; conda installs any package within conda

Conda和pip是具有不同目标受众的不同项目:pip在任何环境中安装python软件包; conda在conda中安装任何软件包

The difference is also summarized in the table below:

下表中还总结了差异:

So, what does this mean for you? Well, it’s actually quite simple:

所以这对于你来说意味着什么? 好吧,实际上很简单:

You should always use Conda whenever possible.

您应该尽可能使用Conda

The reason for this is that the packages installed using Conda can be re-used across environments, whereas pip modules are installed globally (if in the base environment) or in the local environment. Also, if pip is used to install software into a conda environment, conda will be unaware of these changes and may make modifications that would break the environment. Therefore, the more packages that can be kept in conda, the simpler and better.

原因是使用Conda安装的软件包可以在整个环境中重复使用,而pip模块则在全局(如果在基本环境中)或本地环境中安装。 另外,如果使用pip将软件安装到conda环境中,则conda将不会意识到这些更改,并且可能进行会破坏环境的修改。 因此,可以在conda中保存的软件包越多,越简单和更好。

Notice that I said whenever possible in the above sentence. This is because some packages might not be available in Conda, as the number of packages available with pip is much bigger: Conda contains ~1500 packages while pip has over 150000 [5].

请注意,我在上述句子中尽可能说了。 这是因为Conda中可能不提供某些软件包,因为pip可用的软件包数量要大得多: Conda包含约1500个软件包,而pip超过150000 [ 5 ]

So, you should install a package by trying conda install first, and fall back to pip install if the package isn’t part of Anaconda.

因此,您应该首先尝试conda installpip install软件包,如果该软件包不是Anaconda的一部分,则应退回到pip install

There are some other, more technical reasons for using Conda as described by e.g. [6]. See [5] for more info on using pip in a Conda environment

如[ 6 ]所述,还有其他一些使用Conda的技术原因。 有关在Conda环境中使用pip的更多信息,请参见[ 5 ]

使用conda (Using Conda)

Installing packages in Conda is done by simply typing

只需键入以下内容即可在Conda中安装软件包

conda install <package_name>

在环境中使用pip (Using pip in an environment)

To use pip, you must first install pip in your environment using Conda.

要使用pip,必须首先使用Conda在您的环境中安装pip。

conda install -n <env_name> pip
conda activate <env_name>

Then, you can install packages using pip:

然后,您可以使用pip安装软件包:

pip install <package_name>

In the next part (coming soon!) we will look at how to make numerical simulations using Numpy.

在下一部分(即将发布!)中,我们将研究如何使用Numpy进行数值模拟。

翻译自: https://medium.com/@rasmusgs/python-for-matlab-users-part-4-environments-and-packages-b4a027bd1339

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值