python工程代码如何打包

  1. 创建一个空文件夹 example_pkg, 再添加一个同名的子文件夹,包含一个init.py文件. 如下图所示:

     /example_pkg
      /example_pkg
      \__init\__.py
    
  2. 在 __init__.py 文件里面添加如下数据:

name = “example_pkg”

  • 添加 package 的其他文件

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

    /example_pkg
    /example_pkg
      __init__.py
    setup.py
    LICENSE
    README.md
  • 创建 setup.py 文件
import setuptools
    with open("README.md", "r") as fh:
        long_description = fh.read()
        setuptools.setup(
            name="example_pkg",
            version="0.0.1",
            author="Example Author",
            author_email="author@example.com",
            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",
            ],
        )
  • name 包名. 这可以是任何名字,只要包含字母、数字、”和’-‘。它也不能在pypi.org上使用。.
  • version 是包的版本 PEP 440
  • author and author_email 用于确定包装的作者
  • description 描述.
  • long_description 是对包裹的详细描述。这在Python包索引上的包裹细节包上显示。在这种情况下,冗长的描述是从“README”中加载的。这是一种常见的模式。
  • long_description_content_type 告诉索引什么类型的标记用于长描述。在这种情况下,它是Markdown.
  • url 是项目主页的URL。对于许多项目,这将只是一个链接到GitHub、GitLab、Bitbucket或类似的代码托管服务。
  • packages is a list of all Python import packages that should be included in the distribution package. Instead of listing each package manually, we can use find_packages() to automatically discover all packages and subpackages. In this case, the list of packages will be example_pkg as that’s the only package present.
  • classifiers tell the index and pip some additional metadata about your package. In this case, the package is only compatible with Python 3, is licensed under the MIT license, and is OS-independent. You should always include at least which version(s) of Python your package works on, which license your package is available under, and which operating systems your package will work on. For a complete list of classifiers, see https://pypi.org/classifiers/.

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

Creating a LICENSE

对于上传到Python包索引的每个包裹来说,包含一个许可证是很重要的。这告诉用户安装包的条款,他们可以使用你的包。为了帮助选择许可证,请参阅https://choosealicense.com/。一旦您选择了许可,打开许可并输入许可文本。例如,如果你选择了MIT的许可

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.

  1. 生成分布档案

    • 确保安装了最新版本的setuptoolswheel

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

image.png

  • 现在,在安装的同一目录下运行这个命令setup.py所在路径:

python3 setup.py sdist bdist_wheel

引用块内容

  • 这个命令应该输出大量的文本,一旦完成,就应该在dist目录中生成两个文件:
dist/
    example_pkg-0.0.1-py3-none-any.whl
    example_pkg-0.0.1.tar.gz

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值