Package the python package yourself

If you want to realize it quickly , please see the operation steps (no explanation) . If you want to see the explanation text, jump to the detailed explanation . The order is not particularly clear, please forgive me...

Operation steps (no explanation)

Construction of two types of package, one is for local use only their own, can not be issued, if you want to upload to PYPIon, you need to register an account, apply for aAPI Token

Build folder

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

Fill file content
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.

Bale

# 打包
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值

use

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

Detailed explanation

New file directory structure

Create a new folder and name it whatever you want, for example packaging_tutorial, create our package under this folder.
Create a package folder, for example example_pkg, create a new folder under this folder __init__.py,
you can __init__.pywrite your own package content in the file, or it can be empty.
Once you have created this structure, you will want to run all the tutorials in the top-level folder Order, so make sure cd packaging_tutorial.
example_pkg/__init__.pyThe directory needs to be imported as a package, or it can be just an empty file.

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()

Now you will create some files to package this project and prepare it for release. Create the new files listed below and place them in the root directory of the project—you will add content to them following the steps below.

  1. Create a testfolder
    testthat is a folder of unit test files. Leave it empty for now.
  2. Create setup . py
    setup . pyis the build script of setuptools. It tells setuptools information about your package (such as name and version) and which code files to include.
    Open setup.pyand enter the following. Update the package name to include your user name (for example, example-pkg-theacodes). This ensures that you have a unique package name and that your package will not conflict with packages uploaded by others.
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',
)

Insert picture description here

  1. Create 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. Create 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.

The final directory structure is shown below

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

Edit content

Installation setuptoolsandwheel

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

In the setup.pydirectory where the run the following command:

python setup.py sdist bdist_wheel

Will be generated in the current directory

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

Package release

Go to test.pypi to register an account, add your own API token to
installtwine

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

After the installation is complete, run all the files Twineuploaded dist:

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

This will let you enter just apply API token
username: __token
password: pypi-+ 你的token值
Actually, I tried it entered directly in test.pypi registered on the account password is also possible. But the official website tutorial recommends to use token, there may be some mystery. The result of a successful upload is shown in the figure below:
Insert picture description here
Then you will find that your package is uploaded to it testpypiand you can download and install it.

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

After the installation is complete, you can import the test locally.
Insert picture description here

Reference

Packaging Python Projects
python How to package the code written by yourself for others to use (write setup and packaging)
Command Line Scripts In
Python, use setup.py and console_scripts parameters to create installation packages and shell commands

Guess you like

Origin blog.csdn.net/weixin_39333120/article/details/108614930