Python从入门到进阶(十七)——源码打包

一 源码打包

  • 为什么打包源码
    如果你想让你的实现的python代码,通过pip install 方式供所有人进行下载;那就需要将代码上传到PyPi上,这样才能让所有人使用;

1.1 如何打包源码:

  • 前提条件:
1、有一个pypi官网账号;注册地址[https://pypi.org/account/register/]
2、更新pip版本到最新:py -m pip install --upgrade pip
3、通过pip安装twine:要使用twine来上传代码;
4、安装编译工具:pip install --upgrade build
  • 创建项目结构:
    第一步,创建本地目录结构
daletou/
	src/
		daletou/
			__init__.py
			dlt.py

以上除了src和__init__.py为固定值,其他都可以自定义;目录结构需保持一致.

其中__init__.py是为了将目录作为包导入,默认可以为空.
dlt.py是包中的一个模块,主要是提供的功能供下载人调用

如在dlt.py中输入如下代码:
代码中提供的一个函数,返回指定的字符串;
此功能是最终上传的pypi后提供给外部的方法;

from random import sample

def random_dlt(num=1,reds_pre=None,blue_pre=None):
    result=[]

    for n in range(num):
        if reds_pre is None:
            reds=sample([n for n in range(1,36)],5)
        if blue_pre is None:
            blues=sample([n for n in range(1,13)],2)

        reds.sort()
        blues.sort()
        readballs=list(map(str,reds))
        blueballs=list(map(str,blues))
        result.append(' '.join(readballs)+'+'+' '.join(blueballs))
    return '\n'.join(result)

第二步:创建上传所需的文件;
最终得文件结构如下:

daletou/
	LICENSE
	pyproject.toml
	README.md
	setup.py
	src/
		daletou/
			__init__.py
			dlt.py
  • 文件说明:
    pyproject.toml告诉构建工具构建项目所需的内容。
    打开pyproject.toml并输入以下内容:
[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"

README.md是包的描述信息,markdown格式书写

# study Package
This is a simple study package.

setup.py是setuptools的构建脚本.它告诉setuptools您的包(例如名称和版本)以及要包含的代码文件
打开setup.py并输入以下内容。更改name 以包含您的用户名;这可确保您拥有唯一的包名称,并且您的包不会与其他人按照本教程上传的包冲突。

import setuptools

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

setuptools.setup(
    name="daletou",
    version="0.0.1",
    author="Author",
    author_email="author@example.com",
    description="example package",
    long_description=long_description,
    long_description_content_type="text/markdown",
    #url="",
    #project_urls={},
    package_dir={"": "src"},
    packages=setuptools.find_packages(where="src"),
    python_requires=">=3.6",
)

部分属性说明:

  • name是您的包的分发名称。这可以是任何名称;
    version是包版本。
  • author并author_email用于标识包的作者。
  • description是一个简短的、一句话的包摘要。
  • long_description是包的详细说明。
  • long_description_content_type: 描述使用什么类型的标记。
  • url是项目主页的 URL。可不写。
  • project_urls 显示的任意数量的链接。通常是文档、问题跟踪器等。
  • package_dir是一个字典,src目录被指定为根包。
  • packages 包含在分发包中的所有 Python导入包的列表;
  • python_requires给出项目支持的 Python 版本。
还有个配置文件setup.cfg,相对于setup.py,此文件配置是静态元数据,内容基本不变,推荐使用setup.py配置.
具体可参考pypi官网解释.

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.
  • 编译打包
    在pyproject.toml文件同级目录,打开命令行工具;
    执行命令:
python -m build

开始编译打包

G:\BaiduNetdiskDownload\VS2019Space\PypiUpload\FenglingStudyFirst>python -m build
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools>=60)
* Getting build dependencies for sdist...
running egg_info
creating src\fengling.egg-info
writing src\fengling.egg-info\PKG-INFO
writing dependency_links to src\fengling.egg-info\dependency_links.txt
writing top-level names to src\fengling.egg-info\top_level.txt
writing manifest file 'src\fengling.egg-info\SOURCES.txt'
reading manifest file 'src\fengling.egg-info\SOURCES.txt'
adding license file 'LICENSE.txt'
writing manifest file 'src\fengling.egg-info\SOURCES.txt'
* Building sdist...
running sdist
running egg_info
writing src\fengling.egg-info\PKG-INFO
writing dependency_links to src\fengling.egg-info\dependency_links.txt
writing top-level names to src\fengling.egg-info\top_level.txt
reading manifest file 'src\fengling.egg-info\SOURCES.txt'
adding license file 'LICENSE.txt'
writing manifest file 'src\fengling.egg-info\SOURCES.txt'
running check
creating fengling-1.0.1
creating fengling-1.0.1\src
creating fengling-1.0.1\src\FenglingStudy
creating fengling-1.0.1\src\fengling.egg-info
copying files to fengling-1.0.1...
copying LICENSE.txt -> fengling-1.0.1
copying README.md -> fengling-1.0.1
copying pyproject.toml -> fengling-1.0.1
copying setup.py -> fengling-1.0.1
copying src\FenglingStudy\__init__.py -> fengling-1.0.1\src\FenglingStudy
copying src\FenglingStudy\dlt.py -> fengling-1.0.1\src\FenglingStudy
copying src\fengling.egg-info\PKG-INFO -> fengling-1.0.1\src\fengling.egg-info
copying src\fengling.egg-info\SOURCES.txt -> fengling-1.0.1\src\fengling.egg-info
copying src\fengling.egg-info\dependency_links.txt -> fengling-1.0.1\src\fengling.egg-info
copying src\fengling.egg-info\top_level.txt -> fengling-1.0.1\src\fengling.egg-info
Writing fengling-1.0.1\setup.cfg
Creating tar archive
removing 'fengling-1.0.1' (and everything under it)
* Building wheel from sdist
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools>=60)
* Getting build dependencies for wheel...
running egg_info
writing src\fengling.egg-info\PKG-INFO
writing dependency_links to src\fengling.egg-info\dependency_links.txt
writing top-level names to src\fengling.egg-info\top_level.txt
reading manifest file 'src\fengling.egg-info\SOURCES.txt'
adding license file 'LICENSE.txt'
writing manifest file 'src\fengling.egg-info\SOURCES.txt'
* Installing packages in isolated environment... (wheel)
* Building wheel...
running bdist_wheel
running build
running build_py
creating build
creating build\lib
creating build\lib\FenglingStudy
copying src\FenglingStudy\dlt.py -> build\lib\FenglingStudy
copying src\FenglingStudy\__init__.py -> build\lib\FenglingStudy
installing to build\bdist.win-amd64\wheel
running install
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wheel
creating build\bdist.win-amd64\wheel\FenglingStudy
copying build\lib\FenglingStudy\dlt.py -> build\bdist.win-amd64\wheel\.\FenglingStudy
copying build\lib\FenglingStudy\__init__.py -> build\bdist.win-amd64\wheel\.\FenglingStudy
running install_egg_info
running egg_info
writing src\fengling.egg-info\PKG-INFO
writing dependency_links to src\fengling.egg-info\dependency_links.txt
writing top-level names to src\fengling.egg-info\top_level.txt
reading manifest file 'src\fengling.egg-info\SOURCES.txt'
adding license file 'LICENSE.txt'
writing manifest file 'src\fengling.egg-info\SOURCES.txt'
Copying src\fengling.egg-info to build\bdist.win-amd64\wheel\.\fengling-1.0.1-py3.11.egg-info
running install_scripts
creating build\bdist.win-amd64\wheel\fengling-1.0.1.dist-info\WHEEL
creating 'G:\BaiduNetdiskDownload\VS2019Space\PypiUpload\FenglingStudyFirst\dist\.tmp-kh2j9j80\fengling-1.0.1-py3-none-any.whl' and adding 'build\bdist.win-amd64\wheel' to it
adding 'FenglingStudy/__init__.py'
adding 'FenglingStudy/dlt.py'
adding 'fengling-1.0.1.dist-info/LICENSE.txt'
adding 'fengling-1.0.1.dist-info/METADATA'
adding 'fengling-1.0.1.dist-info/WHEEL'
adding 'fengling-1.0.1.dist-info/top_level.txt'
adding 'fengling-1.0.1.dist-info/RECORD'
removing build\bdist.win-amd64\wheel
Successfully built fengling-1.0.1.tar.gz and fengling-1.0.1-py3-none-any.whl

打包完成后,会生成dist文件和打包文件
在这里插入图片描述

本地安装模块

    将要发布的模块安装到自己本地计算机上。仍在 cmd 命令行模式下操作,进 setup.py 所在目 录,键入命令:
python setup.py install
G:\BaiduNetdiskDownload\VS2019Space\PypiUpload\FenglingStudyFirst>python setup.py install
running install
C:\Users\Fengling\AppData\Local\Programs\Python\Python311\Lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
C:\Users\Fengling\AppData\Local\Programs\Python\Python311\Lib\site-packages\setuptools\command\easy_install.py:144: EasyInstallDeprecationWarning: easy_install command is deprecated. Use build and pip and other standards-based tools.
  warnings.warn(
running bdist_egg
running egg_info
writing src\fengling.egg-info\PKG-INFO
writing dependency_links to src\fengling.egg-info\dependency_links.txt
writing top-level names to src\fengling.egg-info\top_level.txt
reading manifest file 'src\fengling.egg-info\SOURCES.txt'
adding license file 'LICENSE.txt'
writing manifest file 'src\fengling.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\FenglingStudy
copying src\FenglingStudy\dlt.py -> build\lib\FenglingStudy
copying src\FenglingStudy\__init__.py -> build\lib\FenglingStudy
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\FenglingStudy
copying build\lib\FenglingStudy\dlt.py -> build\bdist.win-amd64\egg\FenglingStudy
copying build\lib\FenglingStudy\__init__.py -> build\bdist.win-amd64\egg\FenglingStudy
byte-compiling build\bdist.win-amd64\egg\FenglingStudy\dlt.py to dlt.cpython-311.pyc
byte-compiling build\bdist.win-amd64\egg\FenglingStudy\__init__.py to __init__.cpython-311.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying src\fengling.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying src\fengling.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying src\fengling.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying src\fengling.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist\fengling-1.0.1-py3.11.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing fengling-1.0.1-py3.11.egg
Copying fengling-1.0.1-py3.11.egg to c:\users\fengling\appdata\local\programs\python\python311\lib\site-packages
Adding fengling 1.0.1 to easy-install.pth file

Installed c:\users\fengling\appdata\local\programs\python\python311\lib\site-packages\fengling-1.0.1-py3.11.egg
Processing dependencies for fengling==1.0.1
Finished processing dependencies for fengling==1.0.1

安装成功后,进入 C:\Users\Fengling\AppData\Local\Programs\Python\Python311\Lib\site-packages,第三方模块都安装的这里,python 解释器执行时也会搜索这个路径)
在这里插入图片描述
使用 import 导入该模块进行使用:

示例代码:

from FenglingStudy import dlt

result=dlt.random_dlt()
    print(result)

运行结果:

2 3 11 13 26+4 9

1.2 源码模块上传到Pypi

  1. 首先在Pypi官网进行注册登录
    https://pypi.org/account/register/
  2. 命令行源码模块目录执行命令
twine check dist/*
G:\BaiduNetdiskDownload\VS2019Space\PypiUpload\FenglingStudyFirst>twine check dist/*
Checking dist\fengling-1.0.1-py3.11.egg: PASSED
Checking dist\fengling-1.0.1.tar.gz: PASSED

检查是否存在问题,有问题提示,需要解决;
若无问题,执行以下命令上传;

twine upload dist/*
  • 验证是否可以安装:

访问上传成功的地址,是否存在你上传的包;
使用pip install *** 验证是否可以安装;

注意:如果使用的镜像不是官网,例如国内使用最多的清华镜像,可能需要等5分钟以上才能安装,镜像同步需要时间;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值