自己打包python package

想要快速实现就看操作步骤(无解释),想要看带说明文字的,跳转至详细讲解。讲的条理不是特别清楚,还请见谅……

操作步骤(无解释)

分两种构建包,一种是只是自己本地使用,可以不发行,如果想要上传至PYPI上,就需要去注册账号,申请一个API Token

建文件夹

packaging_tutorial   #外层文件夹,命名随意
├── LICENSE   		 #许可证,本地使用的话,不需要该文件
├── README.md        #本地使用的话,不需要该文件
├── example_pkg      #包名
│   └── __init__.py  #函数文件
├── setup.py         #构建文件,有关包的信息(比如名称和版本)
└── tests            #测试文件所在文件夹

填充文件内容
setup.py

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
    version="0.0.1",
    author="Example Author",
    author_email="[email protected]",
    description="A small example package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)
```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.

打包

# 打包
python -m pip install --user --upgrade setuptools wheel
python setup.py sdist bdist_wheel
# 发行,本地使用无需
python -m pip install --user --upgrade twine
python -m twine upload --repository testpypi dist/*
# 第二句指令会让你输入账号密码
#去官网申请API token
# username: __token
# password: pypi-你的token值

使用

# 发行版安装
python -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-pkg-YOUR-USERNAME-HERE

详细讲解

新建文件目录结构

新建一个文件夹,命名随意,如packaging_tutorial,在这个文件夹底下创建我们的包。
建立包文件夹,如example_pkg,在该文件夹下新建__init__.py,
可以在__init__.py文件中编写自己的包内容,也可以为空.
一旦您创建了这个结构,您将希望在顶级文件夹中运行本教程中的所有命令,因此请确保cd packaging_tutorial
example_pkg/__init__.py需要以包的形式导入目录,也可以只是一个空文件。

packaging_tutorial
└── example_pkg
    └── __init__.py
#__init__.py
#!/usr/bin/env python

#-*- coding:utf-8 -*-
def demo():
	print("This is a demo package!")

if __name__ == '__main__':
	demo()

现在您将创建一些文件来打包这个项目,并为发布做好准备。创建下面列出的新文件,并将它们放在项目的根目录中—您将按照以下步骤向它们添加内容。

  1. 创建一个test文件夹
    test是单元测试文件的文件夹。暂时让它空着。
  2. 创建setup . py
    setup . py是setuptools的构建脚本。它告诉setuptools有关您的包的信息(比如名称和版本)以及要包含哪些代码文件。
    打开setup.py并输入以下内容。更新包名以包含您的用户名(例如,example-pkg-theacodes),这确保您有一个惟一的包名,并且您的包不会与其他人上传的包冲突。
with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
    version="0.0.1",
    author="Example Author",
    author_email="[email protected]",
    description="A small example package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

在这里插入图片描述

  1. 创建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.
  1. 创建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.

最后得到的目录结构如下图所示

packaging_tutorial
├── LICENSE
├── README.md
├── example_pkg
│   └── __init__.py
├── setup.py
└── tests

编辑内容

安装 setuptoolswheel

python -m pip install --user --upgrade setuptools wheel

setup.py所在的目录下运行如下命令:

python setup.py sdist bdist_wheel

在当前目录下会生成

dist/
  example_pkg_YOUR_USERNAME_HERE-0.0.1-py3-none-any.whl
  example_pkg_YOUR_USERNAME_HERE-0.0.1.tar.gz

打包发行

test.pypi注册账号,添加属于自己的 API token
安装twine

python -m pip install --user --upgrade twine

安装完成后,运行Twine上传dist下的所有文件:

python -m twine upload --repository testpypi dist/*

这里会让你输入刚刚申请的API token
username:__token
password:pypi-+你的token值
其实我试了一下直接输入在test.pypi上注册的账号密码也是可以的。但是官网教程推荐的是用token,可能有什么玄机吧。上传成功的结果如下图所示:
在这里插入图片描述
然后你就会发现你的包上传到了testpypi上,可以下载安装了。

python -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-pkg-YOUR-USERNAME-HERE

安装完成后,就可以在本地导入测试了。
在这里插入图片描述

参考资料

Packaging Python Projects
python如何将自己写的代码打包供他人使用(写setup和打包)
Command Line Scripts
Python中, 使用setup.py和console_scripts参数创建安装包和shell命令

猜你喜欢

转载自blog.csdn.net/weixin_39333120/article/details/108614930