python工程代码如何打包

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

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

name = "example_pkg"

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

    /example_pkg
    /example_pkg
      __init__.py
    setup.py
    LICENSE
    README.md
    
  2. 创建 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="[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",
        ],
    )
    
  • 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/.
  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.
    
  2. 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

你可能感兴趣的:(python工程代码如何打包)