【Python】打包与发布(Packaging and distributing projects)

▚ 00 前期准备


📣 1. 以Unix/macOS系统为例。

📣 2. 确保pip为最新版本,可使用以下命令来更新pip:

python3 -m pip install --upgrade pip

在这里插入图片描述



▚ 01 创建一个简单的项目


1.1 新建项目的结构

  • 我们在目录packaging_tutorial下进行操作。
  • 项目名称为:example_package_wayne
  • 整个文件结构为:

1.2 设置文件内容

  • __init__.py:设置为空(简单起见)
  • calculator.py:实际的模块,这里是一个四则运算,内容为:
def calc(x, y, operator):
    result = 0
    if operator == "+":
        result = x + y
    if operator == "-":
        result = x - y
    if operator == "*":
        result = x * y
    if operator == "/":
        result = x / y
    return result
  • pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "example_package_wayne"
version = "0.0.1"
authors = [
  { name="Example Author", email="author@example.com" },
]
description = "A small example package"
readme = "README.md"
requires-python = ">=3.7"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

[project.urls]
"Homepage" = "https://github.com/pypa/sampleproject"
"Bug Tracker" = "https://github.com/pypa/sampleproject/issues"
  • README.md
# Example Package

This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.
  • LICENSE
Copyright (c) 2018 The Python Packaging Authority

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

在这里插入图片描述



▚ 02 生成发行版存档 (whl或tar.gz)


  • 上面列出的文件将自动包含在您的源代码发行版中。

  • 确保安装了最新版本的PyPAbuild,可使用以下命令来更新:

python3 -m pip install --upgrade build
  • 现在可以在pyproject. toml所在的目录下运行如下命令:
python3 -m build
  • 编译过程中,会产生如下的输出信息:
  • 该命令执行完后,会在dist目录中生成如下红框内的两个文件:
  • 其中,tar.gz文件是源发行版a source distribution ,而.whl文件是构建发行版a built distribution

在这里插入图片描述



▚ 03 本地安装/卸载该包&测试


3.1安装

  • packaging_tutorial目录下,执行如下命令:
pip3 install dist/example_package_wayne-0.0.1-py3-none-any.whl
  • 若显示以下内容,则表示安装该包成功!

3.2 测试

  • packaging_tutorial目录下,新建目录tests.
  • 在该tests目录下创建文件test_calclator.py,其内容为(这里介绍了三种导入与使用该包的方式):
# 三种导入包的方式
""" 方式一
import example_package_wayne.calculator  

print(example_package_wayne.calculator.calc(8,4,"+"))
print(example_package_wayne.calculator.calc(8,4,"-"))
print(example_package_wayne.calculator.calc(8,4,"*"))
print(example_package_wayne.calculator.calc(8,4,"/"))
"""

""" 方式二
from example_package_wayne import calculator

print(calculator.calc(8,4,"+"))
print(calculator.calc(8,4,"-"))
print(calculator.calc(8,4,"*"))
print(calculator.calc(8,4,"/"))
"""

# 方式三
from example_package_wayne.calculator import calc

print(calc(8,4,"+"))
print(calc(8,4,"-"))
print(calc(8,4,"*"))
print(calc(8,4,"/"))
  • 运行结果为:

3.3卸载

  • packaging_tutorial目录下,执行如下命令:
pip3 uninstall example_package_wayne


在这里插入图片描述



▚  参考文献:


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Continuous Integration and Delivery: A Concise Guide with Examples By 作者: Moritz Lenz ISBN-10 书号: 1484242807 ISBN-13 书号: 9781484242803 Edition 版本: 1st ed. 出版日期: 2018-12-29 pages 页数: (216 ) Gain the techniques and tools that enable a smooth and efficient software development process in this quick and practical guide on Python continuous integration (CI) and continuous delivery (CD). Based on example applications, this book introduces various kinds of testing and shows you how to set up automated systems that run these tests, and install applications in different environments in controlled ways. Python Continuous Integration and Delivery tackles the technical problems related to software development that are typically glossed over in pure programming texts. After reading this book, you’ll see that in today’s fast-moving world, no software project can afford to go through development, then an integration phase of unpredictable length and complexity, and finally be shipped to the customer — just to find out that the resulting application didn’t quite fill their need. Instead, you’ll discover that practicing continuous integration and continuous delivery reduces the risks by keeping changes small and automating otherwise painful processes. What You Will Learn Carry out various kinds of testing, including unit testing and continuous integration testing, of your Python code using Jenkins Build packages and manage repositories Incorporate Ansible and Go for automated packaging and other deployments Manage more complex and robust deployments Cover Front Matter 1.Automated Testing 2.Unit Testing in Python 3.Continuous Integration with Jenkins 4.Continuous Delivery 5.Building Packages 6.Distributing Debian Packages 7.Package Deployment 8.A Virtual Playground for Automating Deployments 9.Building in the Pipeline with Go Continuous Delivery 10.Distributing and Deploying Packages in the Pipeline 11.Pipeline Improvements 12.Security 13.State Management 14.Conclusions and Outlook Back Matter
Big Data, MapReduce, Hadoop, and Spark with Python: Master Big Data Analytics and Data Wrangling with MapReduce Fundamentals using Hadoop, Spark, and Python by LazyProgrammer English | 15 Aug 2016 | ASIN: B01KH9YWSY | 58 Pages | AZW3/MOBI/EPUB/PDF (conv) | 1.07 MB What’s the big deal with big data? It was recently reported in the Wall Street Journal that the government is collecting so much data on its citizens that they can’t even use it effectively. A few “unicorns” have popped up in the past decade or so, promising to help solve the big data problems that billion dollar corporations and the people running your country can’t. It goes without saying that programming with frameworks that can do big data processing is a highly-coveted skill. Machine learning and artificial intelligence algorithms, which have garnered increased attention (and fear-mongering) in recent years, mainly due to the rise of deep learning, are completely dependent on data to learn. The more data the algorithm learns from, the smarter it can become. The problem is, the amount of data we collect has outpaced gains in CPU performance. Therefore, scalable methods for processing data are needed. In the early 2000s, Google invented MapReduce, a framework to systematically and methodically process big data in a scalable way by distributing the work across multiple machines. Later, the technology was adopted into an open-source framework called Hadoop, and then Spark emerged as a new big data framework which addressed some problems with MapReduce. In this book we will cover all 3 - the fundamental MapReduce paradigm, how to program with Hadoop, and how to program with Spark. Advance your Career If Spark is a better version of MapReduce, why are we even talking about it? Good question! Corporations, being slow-moving entities, are often still using Hadoop due to historical reasons. Just search for “big data” and “Hadoop” on LinkedIn and you will see that there are a large number

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值