♚ \

Author: Haze big, the programming, into life

Blog: zhihu.com/people/mai-da-3

background

When we install Python libraries, we usually do

pip install xxx
Copy the code

It’s so cool and convenient! Can we do the same when we write libraries that we think are great and want to share them?

Environmental requirements

1. Have a PyPI · The Python Package Index account

steps

1. Create a directory structure such that the following commands are example_pkg/__init__

/packaging_tutorial
  /example_pkg
    __init__.py
Copy the code

2. Edit example_pkg/__init__.py and write the following

name = "example_pkg"
Copy the code

3. Create example_pkg/setup.py

Setup. Py is setuptools build script to inform setuptools we want to be uploaded to the PYPI library information (name, version information, description and environmental requirements, etc.)

Here is an example of setup.py

import setuptools

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

setuptools.setup(
    name="example-pkg-your-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",].)Copy the code

Parameter analysis:

  • Name: the library
  • Version: indicates the version number
  • Author: the author
  • Author_email: author_email: author_email: author_email: author_email: author_email: author_email: author_email
  • Description:
  • Long_description: detailed description (usually written in readme.md)
  • Long_description_content_type: syntax for descriptions in readme. md (usually markdown)
  • Url: library/project home page, usually we host the project on GitHub, just put the GitHub address of the project
  • Setuptools.find_packages () : Setuptools.find_packages () : setuptools.find_packages()
  • Classifiers: Specifies Python versions, licenses, operating systems that the library depends on, etc

4. Create example_pkg/ readme. md and write your own description of the library to it

# 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.
Copy the code

Create example_pkg\LICENSE, LICENSE is important for every library we create, it tells us what our library can be used for (commercial, disclaimable, etc.); Generally we choose MIT

MIT License

Copyright (c) [year] [fullname]

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.
Copy the code

MIT LICENSE basically means

  • Software can be used and changed at will
  • It can be free, it can be charged
  • This license file must be included in the source file of the software
  • I’m not offering this software to break the law, and if you’re using it to break the law, that’s none of my business
  • You’ve done something wrong with this software, and the responsibility is yours, not the other contributors’

For setup.py, you can refer to the maida library I wrote earlier. I’ll put the address at the end of this article.

For details about how to select your own LICENSE, see the link in the following section

https://choosealicense.com
Copy the code

6. Make sure we have the latest SetupTools and wheel and Twine installed. Here is the install/update command

python3 -m pip install --user --upgrade setuptools wheel twine
Copy the code

7. Package our library/project

python -m pip install --upgrade pip
Copy the code

In the current directory we should see the following:

dist/
  example_pkg_your_username-0.01.-py3-none-any.whl
  example_pkg_your_username-0.01..tar.gz
Copy the code

8. Upload the packaged libraries/projects to PYPI using Twine

Note: the PYPI account password is required, at this point only the PYPI test server is uploaded, not PIP install the library/project

python3 -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Copy the code

We should see the following interface:

Uploading distributions to https://test.pypi.org/legacy/
Enter your username: [your username]
Enter your password:
Uploading example_pkg_your_username-0.01.-py3-none-any.whl
100% | █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ |4.65k/4.65k [00:01<00:00.2.88kB/s]
Uploading example_pkg_your_username-0.01..tar.gz
100% | █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ |4.25k/4.25k [00:01<00:00.3.05kB/s]
Copy the code

After the upload is successful, we can go to the PYPI test server to check whether the upload is successful. If the upload is successful, it means that the upload can be successfully uploaded to the PYPI official server. PYPI test server address:

https://test.pypi.org/project/example-pkg-your-username/
Copy the code

The PYPI test server administrator will delete the library from time to time, so you still have to upload it to the server.

Since I uploaded the library to the test server earlier, we can try to search and see

To test whether the library uploaded to the test server can be used, use the following command

python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps example-pkg-your-username
Copy the code

Roughly the following will occur:

Collecting example-pkg-your-username
  Downloading https://test-files.pythonhosted.org/packages/... / example - PKG - your - username - 0.0.1 - py3 - none - any. WHL
Installing collected packages: example-pkg-your-username
Successfully installed example-pkg-your-username-0.01.
Copy the code

If our library installed from the test server works, we can start uploading it to the PYPI server for everyone to use. (The reason for this step is that the first time we upload a library, we always upload a “failed library” because the directory structure is not good, the unpackaged library can be used properly, but the packaged library can not be used, etc., so that someone will not install our failed library.)

A simple test to see if it can be used normally is as follows. However, we still need to call the specific functions inside to see if they can be used normally. We will not introduce them here

>>> import example_pkg
>>> example_pkg.name
'example_pkg'
Copy the code

9. Upload the library to PYPI official service

twine upload dist/ *Copy the code

After successful upload, the library can be directly installed PIP

Upload the PYPI sample library:

https://github.com/LZC6244/maida
Copy the code

If you are not sure about directory structure or other things, please refer to my library (the structure is simple for beginners), or the library we usually use (for example, I often use scrapy can also refer to the main page of scrapy).

Install a dependent library for your own library:

setup(
    ...
    install_requires=[
        'Third party libraries your library depends on (you can also specify the version)',
        # exapmle
        'pyautogui'.'the Django > = 1.11,! = 1.11.1, < = 2 ',].)Copy the code

Official document portal

https://packaging.python.org/tutorials/packaging-projects/
Copy the code