Skip to Content
Python实用技能学习指南
book

Python实用技能学习指南

by Posts & Telecom Press, Robert Smallshire, Austin Bingham
February 2024
Intermediate to advanced
316 pages
3h 47m
Chinese
Packt Publishing
Content preview from Python实用技能学习指南

附录B 打包与分发

打包和分发Python代码可能是一个复杂且有时令人困惑的任务,特别是当项目有很多的依赖关系,或者涉及比直接Python代码更特殊的组件时。但是,在很多情况下,别人以标准方式访问你的代码非常简单,我们将在本节中看到如何使用标准的distutils模块打包。distutils的主要优点是它包含在Python标准库中。除了最简单的打包需求之外,你可能会想要查看一些设置工具,这些工具的功能超出了distutils的功能。

distutils模块允许你编写一个简单的Python脚本,该脚本知道如何将Python模块安装到任何Python的安装目录中,包括托管在一个虚拟环境中。按照惯例,这个脚本叫作setup.py,它位于项目结构的顶层。你可以执行这个脚本进行实际的安装。

让我们来看一个distutils的简单例子。我们将为在第11章编写的 palindrome模块创建一个基本的setup.py安装脚本。

要做的第一件事是创建一个目录来保存项目。该目录名为palindrome

$ mkdir palindrome
$ cd palindrome

在这个目录中放一份palindrome.py的副本:

"""palindrome.py - 检测回文数字""" import unittest def digits(x): """将整数转换为数字列表。 Args: x: 我们想要的数字。 Returns: 数字的列表,按照"x"的顺序排列。 >>> digits(4586378) [4, 5, 8, 6, 3, 7, 8] """ digs = [] while x != 0: div, mod = divmod(x, 10) digs.append(mod) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

人工智能技术与大数据

人工智能技术与大数据

Posts & Telecom Press, Anand Deshpande, Manish Kumar
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9781835889626