本集简介
双语字幕
仅展示文本字幕,不包含中文音频;想边听边看,请使用 Bayt 播客 App。
欢迎收听Real Python播客。
Welcome to the Real Python podcast.
这是第297期。
This is episode 297.
你有没有遇到过导入包里的模块时,被命名搞糊涂的情况?
Have you ever been confused by the naming of modules you're importing from a package?
有没有一种标准的方法来组织和命名你的Python虚拟环境?
Is there a standard way to organize and name your Python virtual environments?
本周节目,布雷特·卡农回来了,聊聊他最近参与的Python增强提案。
This week on the show, Brett Cannon returns to discuss the Python enhancement proposals he's been working on recently.
我们先从PEP 794说起,这个提案扩展了Python包的元数据,用来明确项目提供的导入名称。
We start with PEP seven ninety four, which extends the metadata fields for Python packages to specify the import names a project provides.
这些元数据能帮开发者在知道导入名称时,准确找到该安装哪个包。
The metadata will help developers identify the correct project to install when they know the import name or the import names a project provides once installed.
我们还会回头聊聊WebAssembly,讨论PEP 816,它规定了CPython发布版中对WASI的支持。
We also dive back into WebAssembly to discuss PEP eight sixteen, which specifies the WASI support in CPython releases.
我们还会深入探讨关于 PEP 832 的争议,它提出了关于虚拟环境命名与发现的标准。
And we wade into the controversy around PEP eight thirty two, which proposes standards around the naming and the discovery of virtual environments.
布雷特分享了他为何如此积极地撰写和推动 PEP 的初衷。
Brett shares his motivation for being a prolific author and supporter of PEPs.
我们聊到了他如何倡导标准和协议,让当前和未来的开发者都能更轻松地使用 Python。
We discuss his promotion of standards and protocols to simplify the Python ecosystem for current and future developers.
好的。
Alright.
我们开始吧。
Let's get started.
《Real Python》播客是一档每周探讨 Python 在实际应用中使用的节目。
The Real Python Podcast is a weekly conversation about using Python in the real world.
我是主持人克里斯托弗·贝利。
My name is Christopher Bailey, your host.
每周我们都会邀请社区里的专家做访谈,讨论 realpython.com 上的文章、课程和相关话题。
Each week, we feature interviews with experts in the community and discussions about the topics, articles, and courses found at realpython.com.
节目结束后,欢迎访问 realpython.com,和一群专家一起学习真实的 Python 技能。
After the podcast, join us and learn real world Python skills with a community of experts at realpython.com.
嗨,布雷特。
Hey, Brett.
欢迎回来。
Welcome back.
谢谢你们再次邀请我,真高兴回来。
Thanks for having me back, our story.
非常感谢。
Appreciate it.
是的。
Yeah.
我很期待和你聊聊三个 PEP,我之前在节目里提过它们几次。
I'm excited to talk to you about, well, three different PEPs that I've kind of mentioned on the show across time here.
其中几个已经通过了。
A few of them have been accepted.
有几项还在观望中。
A few are on the fence here.
最后还有一项全新的内容,你之前提到会在PyCon上公布。
And then, something totally new at the very end here that you mentioned was gonna be announced at PyCon.
现在PyCon已经开了,那项内容也正式公布了。
And so now that PyCon's happened, that's that's been announced.
所以我特别想深入聊聊这些内容。
So I'm excited to dig into this stuff.
是的。
Yes.
我好像根本停不下来,手里的笔一直没放下。
I seem to not be able to put my pencil down and stop working.
所以。
So
最后是不是有人让你别再干了,该休息了?
Was there a thing at the end where you were told to put it down?
就像交Scantron答题卡时那样?
Like, the end of a Scantron test or something?
是的。
Yeah.
没错。
Exactly.
对吧?
Right?
就是大家统一把铅笔放下。
It's just like, okay, everyone put your pencil down.
考试结束了。
Test over.
但其实不是。
It's like, no.
还得把最后几道题做完才行。
Gotta gotta get the last last questions in.
是的
Yeah.
不是
No.
我有个问题,就是总没法对自己的想法说不,也不好好写点总结。
I seem to have a problem of not saying no to my own ideas and not writing some pep about it.
我们可以提一下你刚上线时跟我说过的数据,说你是第四多产的 pep 作者。
Well, I guess we could mention the stats that you mentioned to me when we got on the call here that you're the the fourth most prolific pep writer.
对吧?
Is that right?
是的
Yeah.
维克多·辛纳、巴里·沃沙、吉多和艾莉莎·考夫林写的比我还多。
So I'm tied with Victor Sinner, Barry Warsaw, Guido, and Alyssa Coughlin have written more.
明白了
Okay.
但现在我和别人并列第四了。
But I am now tied for fourth.
而且,我名义上是四点八%的PEP的作者或合著者,不过顺便说一句,这并不意味着我从一开始就是合著者。
And apparently, I have I am a author or co author, which by the way, doesn't mean I was the co author necessarily from the beginning.
有些PEP,我是后来才成为合著者的,因为原作者退出了,或者我慢慢接手维护,就这么成了合著者。
Some PEPs I've become co author of over the years just because people stepped away or I've just ended up helping maintain some active Okay.
只要参与维护得够久,就自然成了合著者。
PEP long enough to become co author.
不过呢,是这样的。
But I yeah.
我作为作者或合著者,参与了所有PEP的百分之四点八。
I'm co author or author on 4.8% of all PEPs that have ever been written.
真厉害。
Wow.
对。
Yeah.
有些人可能会说,我有点执着了。
Some might say I have a problem.
是的。
Yeah.
好的。
Alright.
我觉得这一组内容不仅有意思,而且对学Python的人特别有帮助。
Well, I think this group of them are all not only interesting, but also, I think would be good for people learning Python.
在我看来,你近年来写的那些PEP,主要目标是推动大家走向标准化,试图理清Python这些年积累下来的打包乱象——毕竟,Python社区里的人、工具这么多,大家一直不太愿意统一标准。
The the goal I feel of most of your PEPs in recent history, to me, are to move things towards standards, to try to un unscramble this sort of packaging world that has developed over the years of Python and and things like that, where this, like, very diverse community of Python people and and tools and so forth were reluctant to settle on standards.
当然,这个话题我们也可以再深入聊聊。
And again, we could, you know, talk about that a little bit.
不过,你是不是也这么想?
But do you do you feel that way?
这算是你的一些目标吗?
That that's kind of some of the goals that you have?
哦,这确实是明确的目标。
Oh, it's actually very explicitly the goal.
我曾在 VS Code 的 Python 扩展团队担任开发经理,干了七年。
I was the dev manager for the Python extension in Versus Code for seven years.
对。
Yeah.
所以我非常了解作为 Python 开发者日常会遇到什么情况。
So I had very frontline experience of what it's like to be a developer for using Python.
对吧?
Right?
对。
From Yeah.
从完全零基础、第一天入门的人,到像我这样做了几十年的人。
Total beginners, first day to people like me who've been doing it for decades, literally.
以及那些让他们卡壳的具体步骤。
And just what the steps were that tripped them up.
我们非常幸运,拥有大量热情的用户和愿意为 Python 开发工具的人,但也因此出现了很多功能相似、但实现方式各异的工具,以满足不同人的需求。
And we are very lucky to have a lot of passionate users and people who want to develop tools for Python, but it does mean that there typically are a lot of tools that kind of do similar things, but in different ways to fit people's needs.
是的。
Yeah.
这到底是好是坏,取决于你如何看待这个世界。
Which is good or bad depending on your view of the world.
但 Python 的出现早于很多其他语言的生态系统,比如 Rust、NPM/Node,还有 Go,这些语言的工具链都是内置的。
But Python also predates a lot of these language ecosystems like Rust and NPM slash Node and these other Go where all the tooling was baked in.
对吧?
Right?
明白了。
Okay.
所以,Python 的文化是这样的:核心团队和打包相关的开发者并不会强制规定你必须用什么工具。
So because of that, we've ended up in a culture for Python of we the Python core team and packaging, whoever else doesn't really dictate what tools you use.
对。
Yes.
所以这就意味着,做事的方法多得数不清,而且从来都不容易掌握,我并不是说要去控制,而是说很难确切地知道该怎么做。
And so part of that meant that there was a bazillion different ways to do things and it was never easy to kind of con I don't wanna say control necessarily, but way to know exactly how to do things.
对。
Yeah.
所以我刚开始做打包相关工作时,目标就是把一切都变成可选项,而不是必选项。
And so I went in when I started to work on packaging stuff with a goal of making everything a choice, not a requirement.
对吧?
Right?
就像我刚开始的时候,首要目标就是让 setuptools 成为一个可选项,而不是必选项。
Like when I first got started, the first goal was to make setup tools a choice, not a requirement.
好的。
Okay.
因为在那之前,大家基本上都默认所有人都在用 setuptools。
Because up to that point, everyone kind of just implicitly assumed everyone used setup tools.
对吧?
Right?
而不是,也许 setup tools 的做法并不完全符合我的需求,或者没法实现我想要的功能。
Versus, well, maybe setup tools doesn't quite work the way I want or doesn't quite do the thing I need it to do.
对吧?
Right?
所以这才有了 pyproject.toml 以及其中的 project 表格。
And so that's how pyproject dot toml and the project table in it came about.
对吧?
Right?
还有 build system 表格。
And the build system table.
对吧?
Right?
我们怎么才能让你自由选择想要的构建后端呢?
It's like, how do we kind of make it so that you can choose the build back end you want?
又怎么避免同一个元数据要写五种不同格式,因为五个工具各自要求的写法都不一样?
And then how do we make it so you don't have to write the same bit of metadata five different ways because five different tools all want it written down differently?
对。
Right.
对吧?
Right?
所以一直以来,大家到底在哪里达成了普遍共识,认为这是大多数开发者真正需要或会做的事情呢?
So it's just been a constant, like, just where where is their universal agreement that this is something most developers need or do?
我们就把这些达成共识的内容明确写下来,作为共同的规范。
And let's write it down so that it's the artifacts that we all agree to.
对吧?
Right?
重点不在于具体用什么流程。
It's not necessarily the process.
你可以用任何你喜欢的工具。
You can use whatever tool you want.
我们只需要让这些工具都认同一点:每个项目都得有个名字。
Let's just all get the tools agree though that there is a common denominator here of like, every project needs a name.
那我们该怎么把名字写下来呢?
How do we write the name down?
当然。
Sure.
对吧?
Right?
这其实正是我们之前很多工作的核心。
And that's what a lot of this work has been.
一开始是围绕打包展开的,但我现在想把它扩展到更广泛的开发者工作流程中。
So it's happened to have been around packaging to start, but I'm kind of expanding it to more developer workflow
对。
Yeah.
目标。
Goal.
我觉得这正是很多这类工作的本质。
I think that's a lot of this stuff.
是的。
Yeah.
我们稍后会谈到第一个跟打包关系不大、但与此相关的PEP。
And we'll talk about the the first PEP that's more not packaging, but kinda hits on that later.
对。
Yeah.
我两年前当了爸爸,我不希望孩子放学回来问我:爸爸,为什么学Python这么难?
But I became a dad two years ago, and I don't want my kid coming home from school saying, hey, dad, why is it so hard to get going in Python?
因为如果孩子在学校要学编程语言,很可能是Python,我想让他们有最好的体验。
Because chances are if they're gonna learn a programming language in school, it's gonna be Python, and I want them to have the nicest experience that they can have.
所以我有十年时间去改善这一切,不能让孩子回来失望,觉得爸爸没把事情做好。
So I now have a decade to make that happen and not be not have my kid come home disappointed in how dad has handled things.
所以说,没错。
So so yeah.
不是的。
No.
你说得完全对。
You're totally right.
你有没有听过其他人也这么想,就是考虑他们孩子将来用这门语言的事?
Have you ever heard that from anyone else that that they're thinking about the future of of their children using the language and stuff like that?
在Python圈子里,你认识的其他家长也有这种感受吗?
Is that a shared feeling among among other parents that you've met that are in the Python ecosystem?
这是个好问题。
It's a good question.
我记不清有没有人明确说过,但确实如此。
I don't know if I've ever heard it specifically articulated, but Yeah.
有不少人意识到,从教育的角度来看,这一点很重要。
There are enough of us who realize that it is important for education, educational reasons to Yeah.
得让这门语言用起来顺手。
Make it work nicely.
我们的共同目标就是确保Python对每个人都很友好。
That is a general goal to make sure that Python works for everyone.
是的
Yeah.
对吧
Right?
我的意思是,这其实就是我们整个社区共同秉持的理念——开放、友好、包容。
I mean, it's just kind of the ethos we all have, right, in the community of being open and welcoming and inclusive.
这意味着要从新手到专家都一视同仁。
And that means being inclusive from beginners to experts.
作为专家,我们很容易照顾到专家的需求,但必须主动去关注新手的处境。
And it's very easy for all of us being experts to make sure the experts are taken care of, but you have to very explicitly make sure you also worry about the beginners.
这一直都是我们的传统。
And that's always been a thing.
比如,指导委员会和核心开发者们一直都重视这一点。
Like, the steering council has always cared about and other core devs as well.
因为我们都参加过像 Python 大会、EuroPython 这样的活动,见过那些新手,他们说:天啊,这真的彻底改变了我的人生。
Because we've all been to conferences like Python and Europython or whatever and met those beginners who have gone like, oh my goodness, this has made such a big deal in my life.
非常感谢你们让这件事这么好操作。
Thank you much for making this easy to work with.
是的。
Yeah.
有老师主动来找我们,说这真是太好了。
Had teachers come to us and say, this is great.
对了,还有一个痛点。
Oh, by the way, there's this other pain point.
对吧?
Right?
诸如此类的事情。
Etcetera, etcetera.
对吧?
Right?
那你们接建议吗?
So Do you take requests?
是的。
Yeah.
没错,就是这样。
Well, exactly.
对吧?
Right?
所以我们一直都在想,不管是在最前面还是在脑子里角落里,总在琢磨怎么确保它用起来顺手。
So it's always in our mind, whether it's top of mind or just in the back of our head, it's always how do we make sure it works well for
对。
Yeah.
是的。
Yeah.
初学者。
Beginners.
我正好现在家里就有一个刚入门的新手。
And I just happened to now have a beginner baked into the house.
是的。
Yeah.
对。
Yeah.
没错。
Yeah.
总有一天。
Someday.
也许吧。
Maybe.
不用强求。
Not forced.
我经常想到播客和我们的听众,总是会考虑这个问题。
I think about often about with the podcast and our audience, just always thinking about that.
所以我很喜欢请你来一起聊聊这些事。
And that's why I like having you on to kinda discuss some of this stuff.
那咱们直接切入正题吧。
And I guess we could just dig right in.
好啊。
Sure.
我会尽量别扯太远,毕竟咱们得把每一点都讲到。
I'll try not to go too far because we wanna cover each one of these.
也许这个是最简单的一个,我不确定。
And maybe this I don't know if this is the simplest one.
我也说不准。
I don't know.
所以PEP 794,是的。
So PEP seven ninety four Mhmm.
标题是 import name metadata,是的。
Titled import name metadata Mhmm.
已经通过了。
Has been accepted.
对吧?
Right?
没错。
Correct.
所以这仍然属于打包方面的内容。
So this is again still on the packaging side.
我总是以为,所谓‘被接受’就是说这个会出现在下一个 Python 版本里,但其实这种 PEP 并不是这个意思。
So I always think of, like, accepted as being like, oh, this will be in the next release of Python, and it's not really that type of PEP.
没错。
Correct.
明白了。
Okay.
所以这更偏向于一个打包相关的 PEP?
So it's more of a packaging PEP?
这完全是关于打包的 PEP。
It's entirely a packaging PEP.
好的。
Okay.
所以这个概念是这样的,我来念一下摘要吧,我觉得这样能让我们更快地过一遍。
So the concept is and I'll read the abstract because I think that might be a quick way for us to get through it.
对。
Mhmm.
这个PEP建议扩展Python打包的核心元数据规范,新增两个可重复的字段:import-name和import-namespace,用于记录项目安装后提供的导入名称。
The PEP proposes extending the core metadata specification for Python, packaging to include two new repeatable fields named import dash name and import dash namespace to record the import names that a project provides once installed.
项目表中将新增两个键:import-names和import-namespaces,用来为这两个新的核心元数据字段提供值。
New keys named import dash names and import dash namespaces will be added to the project table in the py project dot toml for providing the values for the new core metadata fields.
这就引出了核心元数据版本2.5的推出。
This leads to the introduction of core metadata version 2.5.
所以,再次提到这个pyproject.toml文件,我觉得我们之前已经多次讨论过了,比如锁文件之类的东西。
So, again, kind of leaning on this PIP project dot TOML file, which I think is we've covered multiple times the you know, I was talking about lock files and and other things like that.
我也同意。
And I've Mhmm.
我越来越经常看到这种情况了,我觉得这真的非常好。
I'm seeing it more and more all the time, which I I think is really, really great.
对。
Yeah.
我没把这事搞砸。
I didn't screw that one up.
谢天谢地。
Thank goodness.
我再分享几篇深入探讨这个问题的文章。
Well, I'll share a couple other articles that dig into it a little bit.
有一篇是Goran等人写的。
One was Goran et al.
他写了一篇文章,讨论如何修正Python项目名称和导入模块的问题。
He has a article towards fixing Python project names and import modules Mhmm.
我觉得挺不错的。
Which I think is pretty good.
其实,就这一篇提到这个。
Actually, it's just this one on it.
他给了几个例子,我本人也遇到过,真是让人想不通。
And he gave us a couple of examples that I personally have experienced and kinda, like, scratch my head on.
尤其是Pillow这个例子,大概是最常见的。
And it's, the Pillow example, I think, is probably the most common one.
对。
Yep.
你可能会想,好吧。
You might go and say, alright.
我来用 pip 安装 Pillow。
I'm gonna pip install Pillow.
然后你又想,行了。
And then you're like, alright.
现在我可以把它用在我的 Python 脚本里了。
Now I'm gonna use it in my Python script or what have you.
然后你就写 import Pillow。
And you're like, import Pillow.
结果却告诉你:不行。
And it's like, no.
其实得写 import PIL。
It's actually import PIL.
你心里就想,这怎么叫 Public Image Limited?
You're like, why is it public image limited?
这到底是什么情况?
What is this?
哦,不好意思。
Oh, sorry.
是啊。
Yeah.
我总觉得这种事挺奇怪的。
I always think that's kind of a weird thing that happens.
另一个例子是Beautiful Soup,很多人用这个。
The other example is Beautiful Soup that a lot of people use Mhmm.
你得安装Beautiful Soup、Beautiful Soup 4,或者别的版本。
Where you install Beautiful Soup or Beautiful Soup four or what have you.
然后你就想,好吧。
And then it's like, okay.
那我要怎么导入它呢?
Well, how do I import that?
结果你得写 import bs4。
And it's import b s four.
对。
Yep.
Scikit-learn也是个好例子,你导入的是sklearn,而不是全名。
Scikit learn's another good one because you import sk learn instead.
是的。
Yeah.
是的
Yeah.
对,没错
And so Yeah.
我不太清楚为什么大家会这么选,背后有什么历史原因。
I don't know the history of, like, why people have selected that.
这些都会让使用者感到困惑,比如到底该怎么用这个东西呢?
So those are the kind of, like, a little bit of namespace confusion for the user to, like, well, how do I use this thing?
没错
Right.
然后我想到了工具方面,整体都在向人工智能靠拢。
And then I think about, like, tooling and everything kind of moving toward AI.
我有个想法,如果有个工具想帮你解决这类问题,它看到你写了 import PIL,就想帮你安装这个包,结果发现 PyPI 上根本找不到,或者其他类似的情况,它就卡住了。
And I had this thought where if something is trying to figure out how to help you with this type of thing, and it sees the idea of import PIL, and then it's trying to add that package, and then it can't figure out, like, well, I can't find that, you know, on PyPI or what have you, or other things like that.
对
And so Mhmm.
还有其他一些例子,比如很多人喜欢用的 utils 模块,这种东西很容易造成命名空间的冲突和污染。
Other examples that are kind of covered in some of this is the dreaded utils module that a lot of people like to use that could potentially cause this sort of namespace collisions and pollution across different things.
所以我不确定你是否想聊聊背后的原因。
And so I don't know if you wanna just talk about, like, kind of the some of the reasoning behind it.
当然,前面的概述已经提到过一些了。
I mean, obviously, the abstract covered some of that.
不过你这番介绍可真够到位的。
But You've done a great sales pitch.
那行吧。
So Okay.
不错。
Good.
我倒要看看我得花多少功夫。
We'll see how much I have to do.
不过我想说一件事。
I I do wanna say one thing, though.
就在几秒钟前,我还说庆幸自己在这几个PEP上没搞砸。
Just a few seconds ago, I did say I'm glad I didn't screw up on these PEPs.
我想说清楚一点。
Wanna be clear.
这些其他的PEP我都有合作者。
I had coauthors on all these other PEPs.
所以要是有什么问题,那都是我的责任。
So I wanted to say anything that's went wrong is my fault.
要是有什么做得好的地方,那都是大家的功劳,这点我要说明白。
Anything that went well is all of us, just to be clear.
我不想让任何人误解。
Don't wanna
我可不想让人觉得所有PyTorch项目、Tableau、生产项目和项目表格的功劳都是我一个人的。
make mislead anyone or make it with I'm taking personal credit for all of PyTorch projects, Tableau and prod and the project table.
对。
Yeah.
对。
Yeah.
不是那个意思。
Without the way.
对。
Yeah.
比如说,我可以跟你讲讲 Pillow 的来龙去脉。
So for instance, I I can actually tell you the history behind Pillow.
对吧?
Right?
PIL,也就是 Python 的图像处理库,最初是弗雷德里克·伦德开发的,后来他不再维护了,Pillow 就是由此分叉出来的。
So PIL, which is the Python image library, was started by Frederick Lund, and Pillow became basically a fork as Frederick just stopped working on it.
明白了。
Okay.
那 scikit-learn 呢,为什么叫 sklearn?
Why at scikit learn with with sklearn?
我猜是因为数据科学圈的人特别喜欢用简短的模块名。
My guess is just that I've noticed the data science side of the community really likes shortened module names.
就像 pandas 缩写成 pd 这样的?
Like p d for pandas and stuff like that?
是的。
Yeah.
对。
Yeah.
没错。
Exactly.
所以他们干脆就按这种方式命名了。
So I think they just baked that in that way.
而且 'scikit-learn' 里有个连字符,但模块名不能用连字符。
Plus, there's a dash inside kit dash learn, and you can't have dashes.
所以他们没直接用 'Yeah'。
So instead of just going Yeah.
我觉得他们只是简写成了下划线。
Underscore, I think they just shortened it.
Beautiful Soup 4 写起来太长了。
Beautiful Soup four is very long to type.
所以我猜他们就是想,没必要让每个人都这么麻烦吧。
So my guess is they just wanna, yeah, why make everyone go through that?
对。
Yeah.
原因有很多。
There are various reasons.
不过说实话,这确实是个坑。
But, yeah, I mean, it it it's a gotcha.
对吧?
Right?
看起来挺意外的,但完全说得通。
It it's it's surprising, but it's totally legitimate.
正如你所说,有时候人们写的模块名字会互相冲突,因为正好用了相同的模块名。
And as you said, sometimes people also write modules that whose names are gonna get trampled on each other because they happen to use the same module name.
即使文件不同也没用。
So even if it's a different file.
对吧?
Right?
比如,如果导入时两个模块指向同一个东西,到底哪个能胜出?
Like, if the import's gonna cause the import to resolve the same thing, who's gonna win that race?
所以,这其实是Python早期设计带来的副作用,别忘了,那时候连打包这个概念都还不存在。
So, yeah, it's it's a side effect of just how Python did things originally because don't forget, we started Python well before packaging was even a thing.
最早的时候,项目的名称跟代码根本没关系,大家就是把代码打包成zip文件,解压到项目里,人人都自己带依赖。
And then originally, the name of your project had nothing to do with, actually, the code because it was just zip files that unzipped into your project code and literally, everyone just vendored everything.
那时候根本没有什么轮子(wheel)的概念。
There's there was no concept of wheels, for instance.
所以从历史上看,根本没人去管这事儿。
So historically, there's there's just was no force.
等我们开始制定打包标准的时候,问题就来了:难道要强迫所有人重命名所有项目或所有导入语句,把现有代码全搞崩吗?
And then by the time we decided to start having standards around packaging, it's just like, well, do we force everyone to rename all their projects or rename all their imports and break all that code?
当然不能。
Well, no.
所以我们只能学会接受这种现状。
So we just have had to learn to live with it.
于是我写了PEP 794,主要是为了记录一个事实:现在有些包,名字和导入名并不一致,比如scikit-learn,你不能用这个名字来导入。
So I wrote PEP seven ninety four as a way to kind of just write down the fact that there are packages out there that, well, they might be named scikit learn, you don't import them that way.
你得用sklearn来导入。
You import them with sklearn.
对。
Yeah.
还有Pillow,你导入的时候用的是PIL。
Or with Pillow, you import PIL.
我这么做是因为这种情况是双向的。
And the reason I did it was it goes both directions.
对吧?
Right?
一个是,我装了这个项目之后,到底该导入什么?
One is, hey, if I install this project, when am I going to get to?
我该导入哪个名字?
What what will I import?
对吧?
Right?
明白了。
Okay.
有些情况可能会让人有点意外。
And some things might be a little surprising.
对吧?
Right?
比如你安装 Pytest,包名是 pytest,但导入的时候得用 _pytest,还有 py。
So for instance, if you install Pytest, it's Pytest, it's underscore Pytest, and it's py.
你可能根本想不到这一点。
And you might not realize that.
对吧?
Right?
提前了解这些其实挺重要的,不然你根本搞不清这东西到底是从哪儿冒出来的。
So knowing that kind of upfront is kind of good to know because like, where the hell did this thing come from?
对吧?
Right?
要是 PyPI 或者别的工具能直接提醒你一下就好了,比如安装 Pytest 的时候,告诉你其实会装三个东西。
So like if PyPI or some other tooling could just say like, hey, when you install Pytest, it's not just Pytest, it's three things.
对吧?
Right?
还有安装 Pillow 的时候,实际上用的是 PIL。
Or when you install Pillow, it's PIL.
没错。
Right.
这是一方面。
That's one thing.
另一方面,从工具的角度来看,我就拿 VS Code 举个例子吧,毕竟我参与过这个项目。
The other thing is is from a tooling perspective, right, let's, I'm just gonna use Versus Code as an example because obviously I've worked on that.
对。
Right.
但这种情况其实适用于几乎所有需要分析依赖的编辑器或工具。
But this applies to pretty much any editor or any tool that has to kind of introspect on things.
比如你输入 import PIL,编辑器却弹出波浪线提醒你‘没安装这个包’。
Let's say you type import PIL and you get that squiggle saying like, hey, this isn't installed.
这到底是怎么回事?
What's going on?
对。
Right.
然后你就想安装它。
And you want to install it.
你在 PyPI 上根本找不到 PIL,至少找不到你想要的那个。
You ain't gonna find PIL on PyPI, or at not the one you want.
你要找的是 Pillow。
You want Pillow.
所以没错。
So Right.
那些想帮用户把导入名称映射回实际包名的工具,到底该怎么知道该安装哪个包,或者该把什么写进锁文件、pyproject.toml 里作为依赖?
How the heck are tools that are trying to help map import names back to the project to know what to install or put in your lock file or put in your pyproject dot toml as a dependency?
对啊。
Yeah.
这些工具怎么
How are
才能搞清楚这一点呢?
they supposed to figure that out?
而目前,根本做不到。
And today, you can't.
你只能逐个遍历 PyPI 上的所有包,检查轮子文件里的内容,然后反向映射回去。
You you either have to walk every package on PyPI, look into the files that are in the wheels and stuff, and reverse map them.
顺便说一句,就算你能可靠地做到这一点,我也得提醒你,毕竟我写过 Python 导入系统的大半部分,很多地方都有巧妙的坑,让模块名和文件名对不上。
By the way, assuming that you can do that reliably, because once again, having written most of Python's import system, I can tell you there are a lot of fun little tricks you can play to make names not match directly to file names.
对的。
Mhmm.
所以你根本不能指望说,‘我看看实际文件,里面确实有这些文件’就万事大吉。
So you can't even rely on like, oh, well, if I look in the real file, I can see these files are here.
我之前也这么想,但你导入的时候,它们压根不会以那种方式出现。
I was like, yeah, but they might not show up that way when you import.
明白了。
Okay.
但至少如果你有这些元数据,直接查元数据就行了。
But at least if you have this metadata, you can just look at the metadata.
对吧?
Right?
展开剩余字幕(还有 480 条)
你不需要逐个查看文件。
You don't have to look at the individual files.
现在可以直接从 PyPI 下载元数据,然后直接看这个就行。
And you can download metadata now directly from PyPI and just look at that.
对吧?
Right?
所以性能上不会像你为了看一个元数据文件,却要下载整整一吉字节的 PyTorch。
So the performance is not like you're downloading like one gig of PyTorch just to look at its metadata file.
你只需要下载那几KB的元数据,看看就行了。
You can just grab the little, like, kilobyte of metadata and look at that.
这就是我这么做的原因。
And so that's why I did it.
对吧?
Right?
我只是想,怎么才能把这些数据开放出来,让工具真正能用上?
Was just kind of how do we expose this data out so that tools can actually use it?
不管是 PY 的 API 在界面上直接提示你:你安装这个,就会得到这些内容。
Whether it's PY API showing it to you somehow in its UI just like, hey, if you install this, this is what you'll get.
还是工具在问:我到底该安装什么才能让这个东西跑起来?
Or tools being like, hey, how the heck do I find something to install to make this thing exist?
是的。
Yeah.
明白了。
Okay.
所以这事儿是来回拉扯的,说到底。
And So it's kinda coming and going, if you will.
没错。
Exactly.
两边都在互相推动。
It's it's both directions.
而且就像你说的,确实挺难的。
And then it's tough as you said.
对吧?
Right?
比如有人写了个通用工具,自己都没意识到已经这么做了,结果别人因为没考虑到这一点,直接用了同一个名字。
Like, if someone's got got kind of like a generic utility and didn't realize they'd done that and that someone said they might trample that name because they just happen not to think about the fact that Yeah.
另一个项目恰好也用了这个名字。
This other project happens to have that thing.
还有一种更糟糕的情况,有人故意抢注这个名称。
And then there's the malicious version of that too where somebody's trying to squat on that space potentially.
所以确实是这样。
So Yeah.
不过说实话,这种情况下,很多人根本不会说实话。
Although, let's be honest, chances are people are gonna lie in that case.
对吧?
Right?
因为根本没什么能阻止他们。
Because there's nothing Yeah.
强迫项目使用它,甚至遵循它的规范。
Forcing projects to use this or to even follow what they say.
所以我不会把安全问题说得太严重,但至少能帮你避免出错。
So it's not I wouldn't go too far into the whole security thing, but at least helps you Right.
别搞砸了。
Not mess up.
对吧?
Right?
这确实是项目在做正确的事。
It's definitely projects doing the right thing.
你不会不小心在 util 里覆盖掉它,或者你只是说,我要弄个叫 PIE 的通用模块。
You won't accidentally overwrite that under util or you just say, I'm gonna have a generic module named PIE.
是的。
Yeah.
而且我还要装个 PIE test。
And I'm also gonna install PIE test.
呃,抱歉,这个会有冲突。
Like, sorry, that's gonna conflict.
总得有人赢,有人输,这样可不行。
Someone's gonna win, someone's gonna lose, and that's not gonna work out well.
是的。
Yeah.
对。
Yeah.
你平时导入库的时候,比如导入 pandas 写成 pd 这种,经常这么干吗?
How often do you when you import things, we we talked a little bit about the, you know, import pandas as, you know, PD or whatever.
嗯,有这习惯。
Mhmm.
你自己会这么做吗?
Is that a common thing that that you do yourself?
不会。
No.
好的。
Okay.
不会。
No.
我只在数据科学领域看到有人这么用,就是缩写成 PD 这种方式。
I I only see that on the data science science side of the world of doing the as thing and shortening.
是的。
Yeah.
我从来不这么干。
I never do.
我个人的习惯是先导入整个模块,再从模块里导入具体的对象。
So I will say my personal practice for imports is I import to the module and then import to the objects in the module.
所以我不会这么做。
So I don't go Okay.
比如不会写成 from django.models import data model 这样的代码。
Like, I don't know, from django dot models import data model.
我通常是用 from Django import models,不过顺便说一下,这个导入名可能不太对,我得澄清一下。
I do I do from by the way, that might be the wrong import name just to be clear.
别给我发火气冲冲的消息啊。
Don't don't shoot me angry messages.
没错。
Exactly.
我会用 from Django import models 这种方式。
But I do from Django import models.
对吧?
Right?
我就到此为止,不再往下写了。
And I'd stop there.
除非真的有必要,比如名字冲突了,我才会再用 from ... import ... 的方式。
And I'd only use ads if I had to like clarify because it's gonna conflict with some name or something.
对。
Right.
但我这么做是为了让我写代码时更清楚地知道,嘿,这个东西不属于当前模块。
But I do that so that it's a little more obvious to me when I'm writing my code that, hey, this is not in this module.
这不是我写的代码。
It's not something I wrote.
它是从别的地方导入的。
It's coming from somewhere else.
明白了。
Okay.
对吧?
Right?
因为我并不指望每个人都给自己的名字加前缀,好让我可以直接导入。
Because I don't necessarily expect everyone to write their names with a prefix so that I can import that name directly.
对。
Right.
对吧?
Right?
比如Django就这么做,但世界上不是每个项目都会用前缀来明确区分。
Because like Django does that, but not every other project in the world does that prefixing to make it very clear.
所以如果我直接导入这个类或函数,那就很合理。
So if I import that class, that function directly, that makes sense.
对吧?
Right?
我最典型的例子就是random。
My canonical example is random.
如果你在代码里导入了一个random函数,而你已经往下写了好几百行,那这个random到底指的是哪个?
If you import a random function in some code and you're a couple 100 lines down away from your import statement, what random is it?
是secrets.random吗?
Is it secrets dot random?
是random.random吗?
Is it random dot random?
还是NumPy.random?
Is it NumPy dot random?
你的 random 到底是哪个啊?
Like, what is it your own random?
你正在用的是谁的 random?
Whose random are you working on?
再看 secrets.random,就很清楚了。
Versus, look at secrets dot random.
我知道自己用的是哪个 random 函数。
I know exactly what random function I'm dealing with.
是的。
Yeah.
没错。
So Yeah.
这说得通。
That makes sense.
我个人不这么做,但社区里确实有一大帮人一直这么用。
I I personally don't do it, but obviously, an entire side of the community does the as constantly.
是的。
Yeah.
对。
Yeah.
大多数项目其实并没有这种命名上的差异,所以PEP特别说明了,如果你的名字在标准化后和项目名一致,就不用纠结大小写的问题。
And most projects, I don't think really have this differential of names, which is why the PEP specifically says like, hey, if your name matches your project name when it's normalized, like, don't worry about capitalization stuff.
没错。
Right.
你没必要这么做。
You don't have to do it.
对吧?
Right?
反正你从来都不用非得这么做。
You don't have to ever do it anyway.
这是可选的。
It's optional.
但这样做对项目来说是个好习惯,就是如果你打算用这种命名方式,最好明确写下来,免得别人措手不及,也能让大家顺利使用。
But it's just a nice thing to give projects just like, hey, if you do this thing with the name, please please write it down so that no one's caught off guard and anyone can work with it appropriately.
这说得通。
That makes sense.
顺便说一句,别觉得奇怪,还没人见过这种做法在实际中出现。
Which by the way, don't don't want you to feel weird that they haven't seen this out in the wild yet.
正如你提到的,因为我们把核心元数据版本升级到了2.5。
As you mentioned, because we're this bumped the core metadata version to 2.5.
所以当我们提到核心元数据时,如果你查看源码包或者sdisk里的那个tar.gz文件,里面那个pkg-info文件就是。
So when I when we talk about core metadata here, if you look in a source distribution or sdisk, that tar dot gz file, that's the pkg dash info file.
明白了。
Okay.
而如果你看wheel文件,那个metadata文件就是。
And if you look in a wheel file, that's the metadata file.
你打开看看,里面会有一个版本号。
If look in there, you'll see that there's a version number on it.
要支持这个功能,得满足好几个条件。
And the deal is is for that to be supported, multiple things have to happen.
首先,PyPI 得接受它。
First, you have to have PyPI accept it.
对。
Right.
对吧?
Right?
不然的话,他们会直接拒绝,说不认识这个东西。
And because otherwise, they'll reject it saying, we don't know what that is.
实际上,他们用的是我参与贡献的一个项目,叫 packaging。
And they actually use a project I'm a contributor to called packaging.
名字倒是挺直白的,不过也容易让人搞混。
Very obviously named, but also makes talking about it a little confusing.
是的。
Yeah.
但 packaging 这个包或项目得先支持才行,因为 PyPI 正是靠它来验证元数据是否合法,有没有损坏或格式错误之类的。
But the packaging package or project needs to support it first because that's what PyPI actually uses to verify that the metadata is legit and not corrupted or mis malformed or whatever.
我们处理这类事情就是按这个流程来的。
This is our protocol for this stuff.
对。
Yeah.
所以整个流程得这么走:首先得让一个 PEP 被采纳,修改版本号。
So the way this all has to happen is first you get a PEP accepted that changes the version number.
等 PEP 通过了,就得给 packaging 提交一个 PR,更新代码,让它能正确识别这个新版本。
Once you get that, then you have to do a PR to packaging to get that updated so that it can be read appropriately.
然后这个更新还得同步到 warehouse 里,也就是 PyPI 的项目名称,让系统能认出这个新版本号。
That then has to be moved over and used in warehouse, which is the project name for PyPI, to then recognize that version number.
如果需要调整界面来配合的话,也得一起改。
And also if there has to be any UI changes to make that work.
等这些都搞定了,像 Twine 这样的工具才需要更新,才能支持上传新版本的包。
And then once that happens, then projects like Twine need to support it to be able to upload it.
然后所有的构建后端也得支持,不管是 PDM 后端、Hashlane、uv 构建、setuptools、flit,还是别的什么。
And then all of the build back ends need to support it, whether that's we're talking about like PDM back end, Hashlane, uv builds, setup tools, flit, whatever.
对。
Right.
你得选一个构建后端。
Choose your build backend.
它们还得能生成并识别这个字段。
They also then have to be able to generate it and recognize that field.
显然,这中间步骤不少。
So obviously a lot of steps.
对。
Right.
截至这次录音时,Twine 已经支持了。
And as of this recording, Twine now supports it.
我觉得他们还得发个新版本才能包含这个功能。
I think they have to do a release still with it in it, maybe.
现在这些构建后端都在等 Twine 的下一个版本发布,之后它们大多已经准备好拉取请求,随时可以合并使用。
And the build back ends then are, I think, all waiting for the next release of Twine, and then they all have a bunch of them have PRs ready to go once that can go out and be used.
不过 PyPI 本身已经支持这个功能了。
But PyPI does have support for it.
只是目前还没有任何一个后端主动去推动它。
It's just none of the back ends are pushing it.
其实这一切都是最近才发生的。
And this all happened actually fairly recently.
我前几天刚查过,就是为了准备这次对话。
I actually just looked it up the other day kind of in prep for this Yeah.
录音。
Recording.
我当时就想,现在大家对这个功能到底是什么态度?
And I was like, where do they all stand on this?
我其实没怎么跟进这些工具的后续,因为所有维护者本来就在同一个对话里。
I haven't I haven't been I don't I don't babysit the pest once they're in because all the tool maintainers are all part of the conversation anyway.
是的。
Yeah.
就是啊,没错。
And it's like, oh, yeah.
大家都已经准备好了拉取请求,一直在等 pip-tools 支持,等到了之后,又在等 Twine 支持。
Everyone's got PRs, and they were all waiting for pip pip r a to get it, and then they got it, and then they were waiting for Twain to get it.
现在 Twine 的主分支已经支持了。
And now Twain's got it on main.
所以我觉得他们现在都在等 Twine 发新版本,一发布就能把拉取请求更新上线。
And so I think they're all waiting for Twain to come out, then they'll convert their PRs and be ready to go.
我可能对 Twine 发布的时间判断错了。
I could even be wrong about the Twain release.
说不定已经发布了。
Maybe it's out.
不过我觉得应该还没正式发布。
I don't I don't think it's out quite yet.
但至少它已经上线了,随时可以使用。
But it's it's at least there on main and ready to go.
所以一切终于都安排好了,希望今年内,至少在需要的时候,大家都能用上。
So everything's finally lining up that hopefully shortly, sometime at least this year, everyone will be able to use it where it's appropriate.
不过话说回来,是的。
But once again Yeah.
不错。
Cool.
我觉得绝大多数人其实用不上。
I think the vast majority of people won't need it.
但一旦你真需要它,那肯定会很方便。
But for those when you do, it's gonna hopefully be handy.
是的。
Yeah.
有一类PEP,我之前一直对这个有点好奇。
So there's a section of PEPs that and I wondered about this.
对
Mhmm.
希望咱们能赶紧把这部分简单带过一下。
Hopefully, we can just cover this kind of quickly.
是啊
But Mhmm.
我大概是四年前开始注意到这个问题的,怎么教这个。
I started noticing it maybe, I don't four years ago, how to teach this.
是的
Yes.
这是新加入的吗?
Is that something that is new?
是最近加进去的吗?
Like, did it get added?
它一直都在吗?
Was it always there?
没有。
No.
它是后来加到PEP流程里的。
It did get added to the PEP process.
明白了。
Okay.
就像你说的,大概四年前左右吧。
As you said, maybe four four years ago or something.
明白了。
Okay.
所以我意识到这是个新加入的内容。
So I did recognize it as a new thing.
是的。
Yeah.
好的。
Okay.
是的
Yeah.
对
Yeah.
不是
No.
有时候会有人提出想法,比如怎么让PEP更容易
We occasionally someone will come up with ideas just like, how do we make PEPs easier to
两者
both
怎么说呢,怎么才能让关于PEP的讨论更顺畅?
have a basically, how do we make it easy to have a conversation about a PEP?
是的
Yeah.
对吧
Right?
因为Python增强提案的目的是记录一场讨论。
Because Python Enhancement Proposals are meant to record a conversation.
它的作用是记录围绕某个提议展开的对话,以便让相关人员做出决定:到底要不要采纳这个改动。
It's meant to record a conversation around a proposal to have something happen to then take it to someone to make a decision as to whether to say, yes, we wanna make that change or not.
没错。
Right.
对吧?
Right?
所以大家总在讨论,怎么让这些文档更容易理解,让阅读的人既能搞清楚内容、表达自己的观点——支持或反对,也能提出问题、寻求澄清,从而帮助完善文档,对吧?
So there's always talk about how do we make these documents easier to digest such that both people who are reading them to understand, to help express their opinions to support or not, or to ask questions to get clarification, then help make the document better Right.
不仅要让普通读者能看懂,决策者也得能看懂。
Can do that, but also the decision makers.
所以当时参与讨论的那些人,他们当然在内,但现在我们说的是那些没参与原始讨论的人。
So the people that were in the conversation, they all those people, but now we're talking maybe the people that are outside that conversation.
是的。
Yeah.
没错。
Exactly.
而且决策者也需要掌握所有信息,不用再去翻整个对话的细节。
And then also the people have to make the final decision having all the information in front of them so they don't have to go back and read all the details of the whole conversation.
对吧?
Right?
比如在指导委员会里
So like on the steering council
没错。
Right.
我那时候在委员会,就得跟上讨论的进度。
When I was on it, you try to keep up with the conversation.
但说到底,你真正关心的是PEP里的内容,因为开会时你讨论的正是这些。
Well, ultimately, what you really care about is what's in the PEP because that's what you're gonna talk about in your in in the meeting with in the for the steering council.
就是说,PEP里写的是什么,因为最终决定要做什么,就看这份文档。
It's like, okay, here's what the PEP says because that's ultimately going to be the document that leads to what's gonna happen.
对吧?
Right?
它最初是参考依据。
It's the reference initially.
顺便说一下,PEP一旦被采纳,就成了历史文件。
By the way, once PEPs are accepted, they're historical documents.
所以,如果你看到某个PEP和现在的实际情况对不上,别担心。
So don't worry if you ever read a PEP that doesn't seem to match what the real what the world is like today.
没错。
Right.
这很正常。
It's okay.
它就是一份历史文件。
It's it's historical document.
是的。
Yeah.
别担心这个。
Don't worry about it.
但关键是,该怎么教这个呢。
But the point is is, like, how to teach this.
对吧。
Right?
正如我们之前提到的,我们确实重视教育。
It as we talked about earlier, we do care about education.
其中一部分就是,我们要确保无论采纳什么,都认真考虑过:教别人这个会有多难?
And part of that is, like, how do we make sure that whatever we accept, we have thought through, like, how hard is this going to be to teach someone?
因为这关系到我们对这项决定可能带来影响的思考。
Because that kind of plays into thinking through the ramifications of this.
对吧。
Right?
比如,如果我们提出一个很难教给别人的东西
Like, if we ask something that's gonna be really hard to teach somebody
是的。
Yeah.
也许我们不该这么做。
Maybe we shouldn't do it.
对。
Right.
对吧?
Right?
如果教起来很困难,那真的值得吗?
Or if it's gonna be hard to teach, is it really worth it?
是的。
Yeah.
对每一位教育者来说,这种复杂性真的值得吗?
Is this truly worth that complication for every educator?
会被接受吗?
Will it be embraced?
是的。
Yeah.
对。
Yeah.
没错。
Exactly.
对吧?
Right?
比如,那位教授或老师要教学生这些东西,他们会恨我们还是感谢我们这么做呢?
Like, is that poor professor or teacher having to teach their students this going to hate us or love us for doing this?
对吧?
Right?
或者至少他们能理解,我知道这很难,但你从中能得到这些收获。
Or can they at least it's like, I know this is hard, but here's what you get out of it.
对吧?
Right?
就是啊。
Like Yeah.
老师真的能为这个做法辩解吗?
Could a teacher even justify this?
所以这也是部分原因,导致那段被删了。
So that's partially why that got edited.
再举个例子,我们调整了PEP中各部分的顺序,把理由部分放到了规范说明之后。
But like, as another example, we just changed the order of the fields, the sections in the PEP such that the rationale comes after the specification.
明白了。
Okay.
以前的顺序是先来个摘要,给你一段简短的概述,
Because the way the order used to go is just go abstract to give you the quick one paragraph blurb
是的。
Yeah.
让你能快速判断:这值不值得往下看。
So that you can kinda go, do I care or not?
是的。
Yeah.
我到底为什么要花这么多时间和精力,让这件事成为现实?
The the motivation as to why the heck am I putting in so much time and effort in my life to have this become a thing?
对。
Right.
以前的结构是先讲设计动机,再讲具体规范,这写起来总显得很别扭——比如,你还没读规范呢,我就得先解释为什么规范要这么设计。
And then it used to be the rationale as to the design of it and then the specification of the actual thing, which always led to some weird contortions when writing because like, okay, I haven't actually had you read the specification yet, but I have to give the rationale as to why I did the spec this way.
所以你不得不在动机或理由部分,先把规范的内容大概写一遍。
So you kinda have to, like, half write the spec out in the motivation in the, rationale section
明白。
Uh-huh.
然后到了规范部分,还得再写一遍。
And then do it again in the spec.
好的。
Okay.
现在顺序颠倒过来了。
That order has been reversed now.
现在你先说明为什么要这么做,接着给出具体规范,等规范写完之后,你才说:好吧。
Now after you give the rational as to why you're doing this, you get the very you give the specific spec, and then after you give the spec, you're like, okay.
既然你已经知道规范是什么了,那我来解释一下为什么这样设计。
Now that you know what the spec is, here's why I designed it this way.
这个改变其实才刚发生,大概两三个月前吧。
And that just happened, like, literally, I don't know, two months, three months ago.
真厉害。
Wow.
明白了。
Okay.
最近才改的啊。
Really recent change.
是的。
Yeah.
是的
Yeah.
对
Yeah.
明白
Okay.
今年肯定了
Definitely this year.
所以我们一直在调整它
So we're still we're always playing with it.
我们又重新投入进去了
We're sold to it again.
如果你听培训团队的说法,他们提到想优化PEP流程,让操作更顺畅
If you listen to the training council, they talk about wanting to improve the PEP process, make it easier to work with stuff.
这些改动正好和那些目标相关
And this is some of the stuff that kind of ties into it.
就是说,咱们干脆让写这些文档和理解起来都更简单点。
It's like, let's just make it easier to write these docs and understand them.
是的。
Yeah.
对。
Yeah.
完全同意。
Totally.
我想深入看看 PEP 816。
So I wanna dig into PEP 816.
我觉得这个可能会简短一点。
I think this one will be a little briefer.
也许吧。
Maybe.
不过也许吧。
But Maybe.
我们之前聊过 WASI 和 WASM,也就是 WebAssembly。
We've talked about WASI and WASM, WebAssembly.
对的。
Yep.
好几次对话都提到过这个话题。
Several of our conversations have been about it.
这次内容就简短一点。
And so this one is just short.
WASI 支持,就是 w a s I 的支持。
WASI support, w a s I support.
没错。
Mhmm.
我再把摘要读一遍,快速过一下。
I'll read the abstract again, kinda make it quick.
这份 PEP 说明了 CPython 对 WASI 的预期支持。
The PEP outlines the expected support for WASI by CPython.
它包含了足够的细节,让你知道在每个 CPython 版本中,应该支持哪个版本的 WASI 和 WASI SDK,而官方的 WASI 支持则在 PEP 11 中明确说明。
It contains enough details to know what WASI and WASI SDK version is expected to be supported for any release of CPython while official WASI support is specified in PEP 11.
对的。
Mhmm.
所以它的意思是,只要你用某个版本的 Python,就能清楚该用哪个版本的 WASI SDK,以及它应该支持哪些功能。
So it it's kind of saying, if you have a version of Python, you will know what target as far
作为
as
WASI SDK 的版本,你该用哪个,系统会明确告诉你。
WASI SDK you're expected to use and is expected to support.
之前有没有人遇到过这种情况,比如:
Was there an experience people had of like, hey.
我用的是 Python 3.13,结果这些东西根本没法用?
I'm using Python three thirteen, and this stuff just doesn't work?
还是说,之前的情况就像一片无法无天的荒野?
Or like, what was was it kinda like a Wild West situation?
当时到底发生了什么,让你觉得必须把这一点更明确地固定下来?
What was going on that you needed to kinda lock this more down?
主要是想提前把问题解决掉,别等它真的成了麻烦。
It it was more getting ahead of things before it became a problem.
明白了。
Okay.
说到WASI,至少在用C语言这类语言编译成WASI时,有两个关键版本需要注意。
So when it comes to WASI, there's kinda two key at least when you're compiling a C based programming language for WASI Yeah.
所谓的‘宿主’是运行程序的WASI运行时,而‘访客’则是运行在其中的程序本身。
As what's called a guest, because the host is the WASI runtime that runs the thing, and the guest is the thing running in it.
所以在WASI的术语里,Python就是访客,运行在WASM运行时这个宿主之上。
So Python is a guest in WASI nomenclature within the host of, like, WASM time.
明白了。
Okay.
所以关键在于,你得关注这两个版本号。
So the deal is there's two version numbers to care about.
有一个WASI版本。
There's the WASI version.
对。
Right.
它决定了这个运行环境能提供哪些功能。
And that defines basically what functionality is available to that guest.
目前我们用的是普遍称为P1或者预览版1的版本,你也可以勉强把它看作0.1版。
And right now, we're using what's called commonly called P one or preview one, or you can kind of squint and pretend it's 0.1.
好的。
Okay.
还有你所使用的WASI SDK版本。
And then there's the WASI SDK version you tied to.
这一点很重要,因为WASI SDK在ABI层面,也就是二进制层面,不保证向前或向后兼容。
And the reason that was important is the WASI SDK has no forwards or backwards compatibility guarantees at the ABI level, So at the binary level.
好的。
Okay.
对吧?
Right?
所以
So
这些就是我们支持的功能,不能再往上或往下兼容了。
This is these are the features we support, and there's no, like, moving up or down from here.
是的。
Yeah.
没错。
Exactly.
对吧?
Right?
如果他们改了某些 POSIX 级别的底层系统调用,那是允许的。
Like, we might if they change how some, like, POSIX level, low level system call thing works, they're allowed to.
如果你用 WASI SDK 32 编译的程序,不一定能在 WASI SDK 33 上正常运行,这完全没有保证。
And there's no guarantee that if you compiled for WASI SDK version 32, that would actually also work in WASI SDK 33.
好的。
Okay.
对吧?
Right?
我们的目标是最终为 WASI 实现 wheel 支持。
So the goal is to eventually have wheel support for WASI.
为此,我们得先让整个社区达成一致,确定大家都统一编译到哪个标准,因为现在这些标准根本对不上。
So in order to have that, we kind of need to agree as a community as to what we're all going to compile to because once again, they don't line up and agree.
比如说,我们让 NumPy 能在 WASI 上编译成功。
So let's say we got NumPy to compile for WASI.
但如果 NumPy 单方面决定,就用 WASI SDK 32 来支持 Python 3.15,
Well, if NumPy just decided, let's just do WASI SK 32 for Python three fifteen.
行吧?
Okay?
那其他领域的人,比如不是数据科学圈的,就麻烦了——毕竟数据科学圈在这方面组织得挺好的,大家都围绕特定的 SDK 和编译器版本来协调。
And then someone else that was like outside the data science community, because the data science community is really good about kind of organizing around this kind of thing around specific SDKs and compiler versions and stuff.
好的。
Okay.
另一个人却决定,你知道吗,我们就用29吧。
Someone else decided to go like, yeah, you know what?
不知道为什么,他们就选了29。
We're gonna do we're gonna do 29 for whatever reason.
这样一来,它们就未必能兼容了。
Well, now those don't necessarily work together.
没错。
Right.
没法同时加载它们。
You can't load them together.
没法把它们拼在一起。
You can't stitch them together.
对吧?
Right?
因为它们针对的是完全不同的东西,可能根本加载不了。
Because they're totally targeting different things and they might not load up.
好吧。
So Okay.
祝你好运。
Good luck.
其中一部分原因在于Python本身,也就是核心的CPython语言。
So part of it is is not somewhat is Python itself, the core CPython language.
没错。
But Yes.
这是希望其他打包工具的开发者能意识到:要是能支持Python的WebAssembly,那打包工具也跟着支持就好了。
It's it's for other developers of packaging to say, hey, it'd be great if you supported WebAssembly with Python, and your packaging did it too.
为了避免这么多麻烦,如果你打算把项目迁移到Python 3.15,那就用这个WASI SDK版本。
And in order to not have so many pain points, if you're gonna do you know, move your stuff to three fifteen as far as, you know, Python, you would use this WASI SDK version.
对。
Yes.
而且这样能确保 CPython 能在那个版本上正常编译。
Also, it makes sure that CPython actually compiles on that version.
明白了。
Okay.
比如,WASI SDK 新增一些功能时,会加入对某些 POSIX 函数的支持。
Like, there have been times when new features land in the WASI in WASI SDK that add, like, POSIX support for some function
明白了。
Okay.
是在 C 层面的。
At the c level.
但实现的方式跟我们预期的不一样。
And it's not implemented the way we expect it.
对吧?
Right?
我记不清具体是哪个版本了,但有一次 WASM SDK 升级,他们加了一个 POSIX 函数,结果跟我们想的完全不一样。
I can't remember what version it was, but there was a WASM SDK version bump where they implemented some some POSIX function and it didn't match up with what we expected.
是的
Uh-huh.
结果我们的测试失败了。
And we got test failures.
明白了。
Okay.
所以我不得不去处理,直接假设这个功能根本不存在。
So I actually had to go in and say, assume this is not this doesn't exist.
从 Python 的角度看,这个功能本来就是可选的。
It was optional from a Python perspective.
我还专门去联系了 WASI SDK 的开发团队,跟他们说:我们的测试套件——顺便说一句,如果有人需要一个符合 POSIX 标准的测试套件,CPython 做得非常好。
But I had to go, like, and I talked to the WASI SDK developers like, hey, our test suite, which by the way, if anyone ever needs a POSIX compliant test suite, CPython is very good at that.
因为年代久远,我们早就把各种情况都测过了。
Being this old, we have test everything.
明白了。
Okay.
如果你需要C编译器的测试套件,我们这儿也很强。
And also if you need a C Compiler test suite, we're pretty good too.
多亏了CPython,我亲自在GCC和LLVM里发现了不少bug。
I personally found bugs in both GCC and LLVM, thanks to CPython.
真的厉害。
So Wow.
我可不是在开玩笑。
I'm not kidding.
我就去找他们,说:我们的测试套件显示,这应该是正确的行为。
So I went to them and was like, hey, our test suite says this is how it should work.
他们说:如果你仔细看POSIX规范,其实并不要求这样。
It's like, well, if you read the POSI expect very specifically, it says this is not necessary.
但我们的预期就是这样,所以干脆在Python里把这个功能关掉。
Like, well, this is what we expect, so I'm gonna just turn it off for Python.
我就这么做了。
And I did.
然后测试就通过了。
And then the test suite passed.
关键是,如果你用这个 Python 版本为目标平台编译,测试套件里就会出现这个值。
But, like, point is if you compile Python for that target in that Python version, you get a test value in the test suite.
明白了。
Okay.
其他 Python 版本不会遇到这个问题,因为它们还没加入这个函数,不管是 SDK 的哪个版本都一样。
While other Python versions wouldn't have that problem with that with or with the SDK version of some other version because they hadn't had that function put in yet.
对吧?
Right?
所以这其实只是生态系统里的一次协调,为了将来在 PEP 里为 WASI 的轮子添加平台标签,我确实希望最终能做成,但同时我也想说,对于 CPython,我和团队是一体的——我就是‘我们’的一部分。
So it's only just coordination across the ecosystem for a future PEP to add wheel support by creating a platform tag for WASI wheels, which I do hope to eventually do, But it's also saying, hey, for CPython, I'm gonna we slash I, because I'm part of the we here.
我们确保 CPython 能用这个版本的 WASI SDK 正常编译。
Say, we're making sure that CPython can compile with this version of the WASI SDK.
我们不保证旧版本或新版本都能正常运行。
We make no guarantees that older versions will work and newer versions will work.
但至少这个版本,你可以拿去用,提交上去,应该是能正常运行的。
But at least this version, you can take it, you can file it, and it should work.
如果不行,就报个bug。
And if it doesn't, file a bug.
这确实是个bug。
It's a bug.
明白了。
Okay.
而对CPython本身来说,另一个关键点是WASI的版本。
And then but the really other key thing for CPython itself is that WASI version.
比如,还没正式发布的WASI 0.3版本,会加入套接字支持。
Because for instance, WASI 0.3, which isn't out quite yet, is adding sockets.
对吧?
Right?
而且在0.3.1版本里,还会逐步加入线程支持。
And it's also adding, eventually some threading support in zero point three point one, basically.
我觉得从技术上讲,这样并不会导致版本号升级,但 0.3 版本迟早也会加上线程支持。
I think it's technically not gonna be a version bump that way, but 0.3 will also be adding threads at some point.
好的。
Okay.
所以接下来的问题是,我们是不是要正式宣布支持 sockets?
So that's also gonna be like, okay, are we gonna say sockets are supported officially?
因为目前在 CPython 里,WASI 的 sockets 功能是被禁用的。
Because right now sockets are turned off in for WASI in CPython.
对。
Right.
正式地来说。
Officially.
但等这个功能上线后,我的计划是正式启用它,让 socket 支持成为官方功能。
But when that comes in, my plan is to turn it on officially so there is officially socket support.
不过如果我要这么做,就得确保自己能真正测试并让它正常运行。
But if I do that, I gotta make sure I can actually test it and work it.
我想能明确地说:是的,官方支持套接字,如果出现相关问题,我们能判断是咱们这边的问题还是WASI的问题,也知道该向谁提交bug报告。
I wanna be able to say like, yes, officially, if there's a bug with the socket support, we can either figure out if it's us or if it's WASI and who do we file the bug with, but we actually officially support it.
明白了。
Okay.
但现在的情况是:不,我们不支持套接字。
Versus right now, it's like, nope, we we don't support socket.
套接字根本不在支持范围内。
Socket is not a thing.
我们目前只支持WASI 1.0,也就是0.1版本,那个版本根本不包含套接字功能。
We only support WASI p one, which is 0.1, which has no socket in it.
所以就是:不行。
So it's just like, nope.
这事不考虑。
Not not going there.
压根儿就不在这范围里。
And it's just not it.
所以这本质上是记录决策过程以及我们最终采用哪个版本的方式。
So it's it's basically a way to document how the decision's made and what version we're gonna snap to Right.
同时也明确大家的预期,以及在什么情况下才可能更改这个版本号。
And kind of what the expectation is and what it would take to ever change that number after Okay.
一旦出现特殊情况。
Something happens.
所以这份PEP明确说了,等到beta一发布的时候。
So the PEP specifically says, come beta one Yeah.
只要主分支和构建机器人上用的是哪个WASI支持版本和WASI SDK版本,我们就用那个。
Whatever WASI support version support and WASI SDK version is in main plus on the build bots.
换句话说,我们最终会采用的就是当前主分支上的那个版本。
That is basically the version we're gonna go with.
如果出现不一致的情况,由负责WASI支持的维护者来决定如何处理。
If there's any discrepancy, it's at the discretion of the people who are listed as the maintainers of WASI support.
如果在beta一发布之后,你确实需要升级WASI SDK版本,那这个决定权在WASI支持的维护者手里。
If you need to change the WASI SDK version for some reason after beta one, that is the call of the maintainers of WASI support.
但如果你需要更改 WASI 版本,那可就麻烦多了。
But if you need to change the WASI version, because that's way more work.
对吧?
Right?
正如我所说,一旦我们支持 0.3 版,就会加入套接字功能。
As I said, like, once we support 0.3, we're gonna have sockets.
是的。
Yeah.
对吧?
Right?
这可不是个小跨越。
That's not a small leap.
这确实需要指导委员会批准。
That actually requires steering council approval.
明白了。
Okay.
因为我们现在改变的是对 CPython 所承诺支持的内容,这比 PEP SDK 的改动要大得多,PEP SDK 那个几乎只是个修复。
Because we're changing what we're promising we're gonna support in CPython at much greater level than PEP SDK where it's like, it's almost a bug fix.
明白了。
Alright.
对。
Yeah.
所以这基本上就是 PEP 里说的。
So that's basically what the PEP says.
然后你提到的 PEP 11,只是列出了支持的平台。
And then PEP 11, as you mentioned, that's just the PEP that lists platforms
支持的平台。
Supported platforms.
是的。
Yeah.
好的。
Okay.
没错。
Exactly.
所以这本质上是一个说明流程的PEP。
So it's just basically a PEP that outlines the process.
这确实是一个专门针对流程的PEP,因为我们有不同的PEP类型。
It's actually a process PEP specifically because we have different kinds of PEPs.
大多数都是标准类的。
Most are just standards track.
比如,当我为打包提出标准类PEP时。
Like, so when I forward standards track for packaging
对。
Yeah.
但我们也有流程类PEP,只是说明我们正在遵循的流程。
But we do have process PEPs that just say like, here's the process we are following for something.
而这次,正是这样。
And in this case Right.
这是我们目前在 CPython 中标准化 WASI 支持所遵循的流程。
This is the process we are following on how to standardize WASI support in CPython itself.
所以它目前是活跃状态,还没最终确定,因为还没完成。
And so it's active, not final, because it's not considered done.
它只是正在被积极推行中。
It's just considered actively in being enforced.
对。
Right.
而且支持级别是会动态调整的,有高低之分。
And there's actual levels of support that things move up and down
我们确实讨论过这一点。
that we've Yeah.
说来有点讽刺,写完这个 PEP 之后,我还没来得及把 beta 1 的支持细节写下来。
Discussed I will say, somewhat ironically, after writing this PEP, I have not gotten around to writing down the support for beta one.
我本来打算在 PEP 11 里写清楚 3.15.0 beta 1 的支持情况。
I thought three fifteen zero beta one in PEP 11 yet.
我待办事项里有这一项。
It's on my to do list.
但会是 PEP 1 和 WASI SDK 33,正好在 beta 1 发布的前一个周五推出的。
But it is gonna be p one and WASI SDK 33, which happened to come out the Friday before beta one hit.
所以幸运的是,我有足够时间把这部分完成。
So luckily, I had enough time to get that done in there.
我们有时间把所有内容都测试一遍,是的。
We got time to test it all and yeah.
我
I
对。
Yeah.
对。
Yeah.
关于这个,我有几个小问题。
Got a couple quick questions on that.
是的。
Yeah.
你提到过,当我想到 POSIX 时,我首先会想到文件系统相关的访问。
You talked about how when I think of POSIX, I think of access to mostly file system stuff.
比如,跨平台的访问,对吧。
Like, you know, access across, like Mhmm.
在计算机系统内部的那些操作。
That in in inside of a, you know, computer system.
线程。
Threads.
原来是这样,我明白了。
Is it does it go oh, okay.
它涉及的范围不止这些。
It goes beyond just that.
哦,确实如此。
Oh, yeah.
本应如此
Supposed to be
整个系统里的权限归属。
ownership across all of that.
谁能在哪儿做什么?
Like, who who can do what and where?
它是
It's
没错。
a Yeah.
比如线程。
So like, p threads.
如果你听过线程,那就是 POSIX 线程。
If you've ever heard of P threads, that's POSIX threads.
明白了。
Okay.
P就是这个意思。
That's what the P is.
对。
Yeah.
很多都跟文件系统有关,但更重要的是怎么跟操作系统交互。
A lot of it's file system, but it's also like how to talk to the OS.
明白了。
Okay.
它其实就是
It's a
很多内容都在于如何
lot of how how
说到底,核心就是怎么跟操作系统沟通。
it's it's basically how do you talk to the OS is really what it comes down to.
对吧?
Right?
是的
Yeah.
所以就是系统、线程,像PID啊、进程这些内容,所有这类东西。
So it's systems, threads, like PIDs, the process stuff, like all that kind stuff.
这些都是
That's all
POSIX。
POSIX.
很好。
Great.
所以Python拥有庞大的标准库,这是否也是帮助它覆盖这些不同方面的原因之一?
So the fact that Python is a large standard library, is that also one of the things that helps with, like, you're saying, making sure it covers all these different bases?
这算不算其中一个因素?
Is that maybe part of it?
是的
Yeah.
这确实是其中一部分。
It's definitely a part of it.
你看看 os 模块就知道了。
If you look at the OS module.
对吧?
Right?
是的。
Yeah.
里面的东西可多了。
A lot of stuff in there.
明白了。
Okay.
诸如此类。
Etcetera.
标准库中很多跨平台代码都需要基于 POSIX,因为这是在不同 Linux 发行版之间保持一致的唯一方式。
Various places where we try to write cross platform code in the standard library requires working with POSIX because that's the only consistent way to work across Linux distros.
对吧?
Right?
而且通常在 Mac 上也能用。
And usually also works with Mac.
然后就是 Windows 了。
And then there's Windows.
是的。
Yeah.
所以一般就是 POSIX 和 Windows 这两个目标平台。
And so usually, it's POSIX and Windows, and that's the two gs target.
通常这样就能通吃了,这也是为什么在 Unix 世界里,POSIX 作为标准这么方便。
And usually, that works across the board, which is why POSIX is so convenient to have in the world as a standard in the Unix world.
最后我想提一下这个,不过希望别扯太远。
The last thing I wanna kinda mention on this one, and hopefully, won't go too deep on this.
但是呢。
But k.
你对这个感兴趣的地方在哪里?
What is your interest in this?
也就是说,是什么让你对 WebAssembly 感到兴奋?
Like, what excites you about WebAssembly?
是它的潜力,现有的项目,还是未来的前景?
The potential of it, existing projects, the future of it.
我把浏览器看作一种能在手机、平板、电脑上运行的通用平台,对吧。
I understand the browser as this kinda universal thing that runs on a phone, a tablet, a computer Mhmm.
还有电视之类的设备。
Your TV, what have you.
我们之前大概提过这一点,但我想再问问,你到底为什么这么兴奋?
And I I think we briefly touched on this before, but, like, what is your excitement?
你为什么对这个这么投入?
Why why are you invested in this?
我以前在这档播客里说过,我接触 WebAssembly 的初衷,是想把 Python 带进浏览器。
I think I've said it on this podcast previously, but the reason I got into WebAssembly was to try to get Python into the browser.
关于 Bayt 播客
Bayt 提供中文+原文双语音频和字幕,帮助你打破语言障碍,轻松听懂全球优质播客。