制作PyPI包

2023-05-16

1. 环境

本文介绍使用setup.py生成pip可以安装的python包以及使用git信息自动生成软件包版本。

1.1 python 、pip版本

$ python3 --version 
Python 3.7.3
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)

1.2 使用Virtual Environment

$ pip3 install virtualenv
$ virtualenv -p /usr/bin/python3.7 setupvenv
$ source setupvenv/bin/activate

2. 代码

2.1 创建目录

src目录下的helloworld为包名

$ mkdir ~/setuppy
$ cd ~/setuppy/  
$ mkdir running
$ mkdir -p running/src
$ mkdir -p running/src/helloworld

2.2 制作helloworld PyPI包步骤

2.2.1 代码编写

(setupvenv)$ cd running/src/helloworld
(setupvenv)$ touch __init__.py
(setupvenv)$ cat __main__.py
import os

def run_helloworld():
    print("hello, world")
if __name__ =='__main__':
    run_helloworld()

查看目录结构

(setupvenv)$ tree
.
└── running
   └── src
       └── helloworld
           ├── __init__.py
           └── __main__.py

3 directories, 2 files

2.2.2 初始化仓库

(setupvenv)$ cd ~/setuppy
(setupvenv)$ git init 
(setupvenv)$ cat .gitignore
#running
/running/*.trs
/running/*.pyc
/running/tags
/running/src/helloworld/tags
/running/src/helloworld/__pycache__
#packaging
/running/.eggs
/running/*.egg
/running/dist
/running/src/*.egg-info
/running/*wheel-store*
/running/src/helloworld/version.py
/running/src/helloworld/log
(setupvenv)$ git add .
(setupvenv)$ git commit -m 'helloworld PyPI test'

查看commit信息后打上tag

(setupvenv)$ git log
commit 846352c5247acab490ce94da11778038718612d3 (HEAD -> master)
(setupvenv)$  git tag -a 'v0.0.1' 846352c5247acab490ce94da11778038718612d3

2.2.3 setup.py和setup.cfg

在~/setuppy/running/目录下编写setup.py和setup.cfg,语法格式请参考官方文档。
使用use_scm_version自动为包添加版本

  1. setup.py中的内容
(setupvenv)$ cat setup.py
from setuptools import __version__, setup

if int(__version__.split(".")[0]) < 41:
    raise RuntimeError("setuptools >= 41 required to build")   

setup(
    use_scm_version={
        "root": "..",
        "relative_to": __file__,
    },
    setup_requires=["setuptools_scm >= 2"],
)

relative_to是指相对于那里,通常设为setup.py所在目录; root是指定Git库的根目录的相对位置,这里示例的…表示上一级目录,可按需指定。

  1. setup.cfg中的内容
(setupvenv)$ cat setup.cfg
[metadata]
name = helloworld
description = helloworld test
long_description = file: README.md
long_description_content_type = text/markdown
url =
author = xiaoming
author_email = xiaoming@abc.com
maintainer = xiaoming
maintainer_email = xiaoming@abc.com
license = MIT
license_file = LICENSE
platforms = any
classifiers =
    Development Status :: 5 - Production/Stable
    Intended Audience :: Developers
    License :: OSI Approved :: MIT License
    Operating System :: Linux
    Programming Language :: Python :: 3.7
    Programming Language :: Python :: Implementation :: CPython
    Programming Language :: Python :: Implementation :: PyPy
    Topic :: Software Development :: Libraries
    Topic :: Software Development :: Testing
    Topic :: Utilities
keywords = helloworld
project_urls =
    Source=''
    Tracker=''

[options]
packages = find:
install_requires =
python_requires = >=3.7
package_dir =
    =src
zip_safe = True

[options.packages.find]
where = src

[options.entry_points]
console_scripts =
    helloworld=helloworld.__main__:run_helloworld

[options.extras_require]

[options.package_data]

[sdist]
formats = gztar

[bdist_wheel]
universal = true

[tool:pytest]
markers =
    slow
junit_family = xunit2
addopts = --tb=auto -ra --showlocals --no-success-flaky-report
env =
    PYTHONWARNINGS=ignore:DEPRECATION::pip._internal.cli.base_command
    PYTHONIOENCODING=utf-8

注意
install_requires可有添加此包需要依赖的其他pip包。
options.entry_points,此选项为我们安装完python包后,可以直接使用的命令入口。

  1. setup.cfg需要用到README.md和LICENSE
    创建README.md和LICENSE文件,这两个文件填写自己的项目说明和license,暂时为空文件
(setupvenv)$ cat README.md
(setupvenv)$ cat LICENSE
  1. 此时目录结构
(setupvenv)$ tree
.
└── running
    ├── LICENSE
    ├── README.md
    ├── setup.cfg
    ├── setup.py
    └── src
        └── helloworld
            ├── __init__.py
            └── __main__.py

3 directories, 6 files

2.2.4 制作

  1. 制作
(setupvenv)$ cd ~/setuppy/running
(setupvenv)$ python setup.py sdist

在~/setuppy/running/dist目录下生成PyPI包

(setupvenv)$ ls dist/
helloworld-0.0.1.tar.gz

2) 安装

(setupvenv)$ pip install dist/helloworld-0.0.1.tar.gz
Processing ./dist/helloworld-0.0.1.tar.gz
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: helloworld
  Building wheel for helloworld (setup.py) ... done
  Created wheel for helloworld: filename=helloworld-0.0.1-py2.py3-none-any.whl size=2374 sha256=29a88f5ccbc6aa4f7773832d2a11f459bc4b92a44e4ced232c0786bfb953dd85
  Stored in directory: /home/xiaoming/.cache/pip/wheels/5d/75/04/2ef9e8bce00eca52ec0944394766c491bee2581c0953a4bec5
Successfully built helloworld
Installing collected packages: helloworld
Successfully installed helloworld-0.0.1
  1. 查看
(setupvenv)$ pip show helloworld
Name: helloworld
Version: 0.0.1
Summary: helloworld test
Home-page: 
Author: xiaoming
Author-email: xiaoming@abc.com
License: MIT
Location: /home/xiaoming/tmp/setupvenv/lib/python3.7/site-packages
Requires: 
Required-by: 

  1. 使用
(setupvenv)$ helloworld 
hello, world
  1. 存在的问题
    想在running/src目录下自动生成version.py,填充当前包的版本号,setup.py如下:
(lmgrunvenv) $ cat setup.py                                       
 import os                                                                                                   
 from setuptools import __version__, setup                                                                   
                                                                                                             
 if int(__version__.split(".")[0]) < 41:                                                                     
     raise RuntimeError("setuptools >= 41 required to build")                                                
                                                                                                             
 setup(                                                                                                      
     use_scm_version={                                                                                       
         "root": "..",                                                                                       
         "relative_to": __file__,                                                                            
         "write_to": os.path.join("src/helloworld", "version.py"),                                           
         "write_to_template": 'from __future__ import  unicode_literals\n\n__version__ = "{version}"\n',     
     },                                                                                                      
     setup_requires=["setuptools_scm >= 2"],                                                                 
 )                                                                                                                                                         

错误1:
制作包时报错如下:

...
FileNotFoundError: [Errno 2] No such file or directory: '../src/helloworld/version.py'

根据上述所示的root的目录去查找src/helloworld/version.py是找不到的,因为还有一层running目录
错误2:
将write_to更改为"write_to": os.path.join(“running/src/helloworld”, “version.py”),
虽然包可以制作成功,但是安装时报错:

...
FileNotFoundError: [Errno 2] No such file or directory: '../running/src/helloworld/version.py' 

安装时,running目录没有打包在包中,所以无法找到…/running/src/helloworld/version.py。

对于git信息和setup.py不在同一级目录又想往包中写入version的方式,目前还没找到合适的方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

制作PyPI包 的相关文章

  • python whl文件_python之PypI打包whl文件

    一 简单介绍 python中我们经常会用到第三方的包作为工具 xff0c 比如爬虫解析工具 xff0c 网络请求工具等 之所以要把它封装成包 xff0c 意识为了技术与业务分离 xff0c 二是为了能多 项目多平台共用 python里面用到
  • 上传自己封装的python包到PyPI

    请参考 https www jianshu com p 81fe5a5cd27a
  • 在PyPI上发布自己的Python包(一)

    文章目录 发布PyPI 简单 0 GitHub 1 环境 2 准备 2 1 注册PyPI账号 2 2 安装环境 3 开始 3 1 新建文件夹 3 2 上传 3 3 测试 发布PyPI 简单 0 GitHub https github com
  • 如何下载整个 pypi Python 包索引

    我正在尝试找到一种方法来下载整个 PyPi 索引 并且仅下载索引 没有代码文件 我想分析许可证类型 以便能够排除许可证类型限制过多的库 我已经在网上查看并浏览了用户指南 但如果答案就在那里 我却无法理解 嗯 你可以使用PyPi 的简单索引无
  • --use-mirrors 的规范替代品

    PyPI 可能不可靠 不幸的是 我有很多 Travis CI 构建失败的情况 因为 pip 无法安装我的要求之一 lxml 是最臭名昭著的罪犯 各种在线资源推荐 use mirrorsflag 到目前为止已经为我解决了这个问题 然而 use
  • pip install --user 时 Python console_scripts 不起作用

    我将代码包装到 python 包中 现在我希望它也可以从命令行 linux 运行 因此 我将 console scripts 标签添加到 setup py 中 当我以 root 身份安装它时 一切似乎都工作正常 我可以从命令行运行程序 不过
  • 为什么使用 pip 而不是 easy_install?

    A tweet reads 不要使用 easy install 除非你 就像在自己脸上捅刀一样 使用点 为什么使用 pip 而不是 easy install 难道不是错误主要在于 PyPI 和包作者 如果作者将垃圾源 tarball 例如
  • setuptools、easy_install 和自定义 pypi 服务器

    我有一个正在运行的自定义 pypi 服务器 我想知道如何将所有引用指向https pypi python org https pypi python org 从那里到我的自定义服务器 我希望能够涵盖用户调用的情况pip easy insta
  • 在 2020 年 Python 2 生命周期结束后,我还可以通过 Pip 安装依赖包吗?

    我正在使用 Python 2 7 和 python pip 从下载所有依赖包requirements txt用于运行我的项目的文件 据我们所知 Python 2 将于 2020 年结束生命 我担心无法从 pip 安装我的依赖包 因为pip
  • 由 twine python 发布的包未出现在存储库中

    我正在尝试将我的 python 包发布到私有存储库 我是按照官方指南来的https packaging python org en latest tutorials packaging projects https packaging py
  • 在 Python 包中包含 *.pyd 文件

    我有一个 python 模块模块 pyd一旦手动将其放入 python 安装文件夹的站点包中 它就可以正常工作 当我将解决方案上传到云环境时 问题就开始了 构建包要求我将每个模块作为要安装的包传递pip install module 我创建
  • 如何使用 qemu 编译和构建 aarch64 的 python 包?

    我正在尝试为一个包构建 python 轮子 lap https github com gatagat lap 为了aarch64建筑学 我的主机环境是 WSL2 和 Ubuntu 20 04docker 目标是BuildrootGNU Li
  • 如何在启用双因素的情况下将包上传到 PyPi?

    我想将包上传到 Pypi 因此我创建了一个帐户并尝试按照手册进行操作 帐户 看起来我无法从中创建项目pypi org直接地 我安装了twine我做到了 python3 m twine上传dist 这次我出现了以下错误 HTTPError 4
  • 从 pypi 进行 pip install 可以,但从 testpypi 失败(找不到需求)

    我正在尝试创建我的第一个 python 包 为了不搞砸整个交易 我一直在尝试将其上传到 testpypi 服务器 这似乎很顺利 sdist 创建并上传没有显示任何错误 但是 当我尝试将其安装到新的 virtualenv 时https tes
  • 从私有 pypiserver 安装 python 包

    我在 nginx 代理后面设置了一个 pypiserver 它使用 htpasswd 进行身份验证 我目前可以上传 sdists 但我不知道如何下载它们 我希望能够在运行时下载它们setup py test并以某种方式使用pip 这可能吗
  • PyPI 区分大小写吗?

    PyPI 如何处理区分大小写 例如 这个问题 https stackoverflow com questions 17460747 change case of package name on pypi记录 PyPI 拒绝新包foobar
  • pip 安装和自定义索引 url

    我在尝试使用安装时遇到以下异常pip Retrying Retry total 4 connect None read None redirect None after connection broken by ProtocolError
  • 找不到绳线(-bash:绳线:找不到命令)

    我正在尝试使用 twine 在 pypi 上发布我的第一个 python 包 当然会首先添加 test pypi 我遵循了官方指南https packaging python org tutorials packaging projects
  • 如何在 setup.py 中指定多个作者/电子邮件

    我们为 Twitter 应用程序编写了一个小包装 并将此信息发布到http pypi python org http pypi python org 但 setup py 仅包含一个用于指定作者的电子邮件 姓名的字段 我如何在以下字段中指定
  • 使用 `--pre` 选项时,pip 不匹配预发布版本

    假设您已经发布了两个预发行版 package 0 0 1 dev0 package 0 0 2 dev0 My install requires部分在setup py states package gt 0 0 2 lt 1 0 0 现在

随机推荐

  • 如何善用家中闲置的带宽资源赚钱(2020版)

    CDN的全称是Content Delivery Network xff0c 即内容分发网络 xff0c 依靠部署在各地的边缘服务器 xff0c 通过中心平台的负载均衡 内容分发 调度等功能模块 xff0c 使用户就近获取所需内容 xff0c
  • 一招将闲置宽带完美利用起来

    随着我们生活水平的提高以及国家对信息化建设的推动 xff0c 大部分家庭的宽带已经进入了高速时代 xff0c 100 200M到处可见 xff0c 甚至于500M也不是什么新鲜事儿了 xff0c 宽带的速率是提高了 xff0c 不过问题也来
  • 十一、 Debian忘记密码重置

    其方式是在GRUB引导菜单下按 e 进入编辑模式直接修改用户密码 重启VPS xff0c 可以在面板重启也可以在VNC上面使用发送 CTRL 43 ALT 43 DEL 按钮直接重启 xff0c 在图示处按 e 键 xff08 若出现BIO
  • 加入共享宽带,让你的闲置宽带循环利用再变现

    共享经济是近些年来发展的一个热点名词 xff0c 因此大家也会看到一些非常多的共享产品出现在市面上 比如说大家熟悉的共享单车 xff0c 共享汽车共享充电宝等等 xff0c 但是不知道大家有没有听说过共享宽带呢 xff1f 宽带几乎是家家户
  • 一招让NAS自给自足

    网络带来了许多便利 xff0c 但又给生活带来了很多烦恼 xff0c 比如微信文档总是过期 xff0c 关键内容经常找不到 xff0c 照片备份太散乱 最近听朋友说听说前任离婚了 xff0c 我突然想重温下与她昨日的温情 xff0c 可是翻
  • 百度网盘撸用户羊毛是怎么一回事

    最近百度网盘事件闹得沸沸扬扬 xff0c 很多吃瓜小伙伴对这次事件的来龙去买不太清楚 xff0c 今天就给大家八一八百度网盘如何反撸用户引发众怒 百度对于该计划的说明 xff1a 用户参加该计划可贡献闲置网络带宽和电脑存储空间给百度 xff
  • 业务流程节点信息提示

    xfeff xfeff 该模块中主要是为了明确用户操作 让用户具体的知道该进行哪一步操作 xff0c 在登陆系统后 xff0c 系统首页中会有下面类似的流程图 xff1a 当用户完成一项操作后 xff0c 要根据流程提示其他用户进行下一步操
  • UbuntuWSL操作PA的BUG记录——AM_HOME环境变量的设定

    2021年5月更 xff0c 发现WSL2是真的香 xff0c 下次还用 x1f604 2021年4月 血亏 xff0c 建议老实用虚拟机做 xff0c WSL还是有很多未完善的地方 xff0c 不适合新手瞎折腾 问题描述 xff1a 当使
  • windows11编译OpenCV4.5.0 with CUDA(附注意事项)

    windows11编译OpenCV4 5 0 with CUDA 从OpenCV4 2 0 版本开始允许使用 Nvidia GPU 来加速推理 本文介绍最近使用windows11系统编译带CUDA的OpenCV4 5 0的过程 文中使用 特
  • OpenCV—矩阵数据类型转换cv::convertTo

    OpenCV 矩阵数据类型转换cv convertTo 函数 void convertTo OutputArray m int rtype double alpha 61 1 double beta 61 0 const 参数 m 目标矩阵
  • Mysql LIMIT使用

    原文出处 xff1a http www jb51 net article 62851 htm Mysql中limit的用法 xff1a 在我们使用查询语句的时候 xff0c 经常要返回前几条或者中间某几行数据 xff0c 这个时候怎么办呢
  • cookies的理解与chrome查看cookie

    Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存 xff0c 或是从客户端的硬盘读取数据的一种技术 Cookies是当你浏览某网站时 xff0c 由Web服务器置于你硬盘上的一个非常小的文本文件 xff0c 它可以记录
  • 【Qt】Qt多线程开发—实现多线程设计的四种方法

    Qt 使用Qt实现多线程设计的四种方法 文章目录 Qt 使用Qt实现多线程设计的四种方法一 写在前面二 方法一 QThread xff1a 带有可选事件循环的底层API三 方法二 QThreadPool和QRunnable xff1a 重用
  • OpenStack Designate系统架构分析

    前言 OpenStack提供了云计算数据中心所必不可少的用户认证和授权 计算 存储 网络等功能 xff0c 网上已经有不少的文章介绍这些功能的配置 架构分析以及代码详解 但是 针对OpenStack Designate所提供的DNSaaS服
  • 【Qt】Qt线程同步之QWaitCondition

    Qt 线程同步之QWaitCondition 文章目录 Qt 线程同步之QWaitCondition一 简介二 成员函数API xff08 2 1 xff09 等待 wait xff08 2 2 xff09 唤醒一个线程 xff08 2 3
  • Linux下如何用命令连接有线网络

    因为进入tty命令界面 xff0c 想要下东西 xff0c 需要用命令连接有线网络直接输入命令 sudo dhclient eth0
  • Debian下制作deb包

    1 安装相应的编译工具 apt get install dh make dpkg dev debhelper fakeroot build essential Docker中执行dh make出现如下错误 xff1a Cannot get
  • electron在龙芯平台上本地安装使用和打包(二)

    已经打包好的适合在mips平台运行的electron quick start xff0c 点击下载 1 安装electron前的准备 从http www loongnix org index php Electron下载所需软件包 xff0
  • mips版本electron在龙芯平台上的安装

    本文主要讲述如何安装mips版本的electron xff0c 和上两篇不同的是 xff0c 此安装方法只需要用户修改一行代码即可完成 mips架构用户只需要关注本文章的3 2章节 目前用户可以通过此方法安装mips版本的4 1 3 xff
  • 制作PyPI包

    1 环境 本文介绍使用setup py生成pip可以安装的python包以及使用git信息自动生成软件包版本 1 1 python pip版本 python3 version Python 3 7 3 pip 18 1 from usr l