制作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里面用到
  • 制作PyPI包

    1 环境 本文介绍使用setup py生成pip可以安装的python包以及使用git信息自动生成软件包版本 1 1 python pip版本 python3 version Python 3 7 3 pip 18 1 from usr l
  • Python 项目打包并发布到私有 PyPI 服务器

    推广博客 xff1a Python 项目打包并发布到私有 PyPI 服务器
  • 上传自己封装的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
  • 在自定义包中安装 pip 时,如何修复 install_requires 列表中的“找不到满足要求的版本”?

    我正在尝试使用 twine 包构建自己的 Python 包 可通过 pip 安装 一切都很顺利 直到我尝试 pip 安装我的实际包 所以在上传到 PyPi 之后 所以我首先运行 python3 setup py sdist bdist wh
  • pip install --user 时 Python console_scripts 不起作用

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

    有没有办法找到所有使用 easy install 或 pip 安装的 Python PyPI 包 我的意思是 排除使用发行版工具安装的所有内容 在本例中是 Debian 上的 apt get pip freeze将输出已安装的软件包及其版本
  • 从本地 pypi 索引中删除包

    这与此类似question https stackoverflow com questions 20403387 how to remove a package from pypi但有一个例外 我想从本地 pypi 索引中删除该包的一些特定
  • 二进制轮无法使用twine上传到pypi上

    我正在尝试从 github actions 上传适用于 linux 和 windows 的 Pypi 包 使用linux我得到这个结果 Binary wheel xxx cp36 cp36m linux x86 64 whl has an
  • 为什么我无法使用 Pip 从 PyPI 安装软件包?

    最近我无法使用 Pip 下载任何软件包 每次出错时status 1 通常在跑步时setup py或建筑轮子 以下是我得到的完整回溯的两个示例 pip install miniupnpc Collecting miniupnpc Downlo
  • 如何在启用双因素的情况下将包上传到 PyPi?

    我想将包上传到 Pypi 因此我创建了一个帐户并尝试按照手册进行操作 帐户 看起来我无法从中创建项目pypi org直接地 我安装了twine我做到了 python3 m twine上传dist 这次我出现了以下错误 HTTPError 4
  • setup.py 不遵守 PIP_INDEX_URL

    我正在运行本地pypi服务器 https pypi python org pypi pypiserver 我可以通过使用以下命令指定它来从该服务器安装软件包 i的选项pip命令或通过设置PIP INDEX URL环境变量 当我安装具有先决条
  • 如何使用 anaconda conda 命令安装 PyPi 包

    使用 Anaconda Python 发行版时 安装无法直接通过 Anaconda 获取的 PyPi 包的最佳方法是什么 现在我正在使用 conda pipbuild pypi name conda install use local pa
  • pip 下载而不执行 setup.py

    如何下载发行版 可能是 sdist 而不可能执行setup py文件 可能包含恶意代码 我不想递归获取依赖项 只想下载指定发行版的一个文件 尝试无效 pip download no deps mydist 这是一个可重现的示例 演示了set
  • 使用 Python setuptools 的安装后脚本

    是否可以将安装后 Python 脚本文件指定为 setuptools setup py 文件的一部分 以便用户可以运行以下命令 python setup py install 在本地项目文件存档上 或者 pip install
  • 我可以制作 pip 可安装包而不在 pypi 中注册包吗?

    我正在尝试按照以下描述使用 python3 制作 pip 可安装包 Url here https marthall github io blog how to package a python app 我使用成功安装了我的包sudo pyt
  • 如何在 PyPI 中包含非 .py 文件?

    我是 PyPI 的新手 所以让我符合这一点 我试图在 PyPI 上放置一个包 但当我尝试使用 pip 安装它时遇到了一些麻烦 当我将文件上传到 PyPI 时 我收到一条警告 但 setup py 脚本完成时没有出现致命错误和 200 状态
  • 从 PyPi 下载轮子

    如何从列出的软件包中下载特定的车轮PyPi https pypi python org pypi 我假设我会使用wget or curl 但我不确定要使用哪些参数 众所周知 PyPI 很难内省 幸运的是 Debian 项目用于扫描 FTP
  • 跟踪 pypi 依赖项 - 谁在使用我的包

    无论如何 是否可以通过 pip 或 PyPi 来识别哪些项目 在 Pypi 上发布 可能正在使用我的包 也在 PyPi 上发布 我想确定每个包的用户群以及可能尝试积极与他们互动 预先感谢您的任何答案 即使我想做的事情是不可能的 这实际上是不

随机推荐

  • 如何善用家中闲置的带宽资源赚钱(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