Publish / upload a library created in Python to PyPI

Once you have created a useful tool, I would like everyone to use it. In Python, there is a site to register a package called PyPI, and by registering here, you can install it with pip install etc.

In this document, I would like to introduce the procedure.

Note: About the old method

If you search on Google thinking ..., you will find many old methods that are no longer recommended. Please note that the following is a typical example of the old method.

Advance preparation

The following preparations are required before publishing a package to PyPI.

Registering an account on PyPI

Create a PyPI account below.

PyPI Manual user registration

Placement of .pypirc with account credentials

If you find it awkward to enter your username and password each time you upload, you can create a .pypirc directly under your home directory and include your credentials there.

[pypi]
username = <username>
password = <password>

However, please note that this file is not encrypted, and the contents will be exposed in clear text.

Register with PyPI

Now, let's register with PyPI. For the sake of explanation, let's take a library with the following structure as an example.

YOUR-PROJECT-FOLDER
├── CHANGES.txt (OPTIONAL)
├── LICENSE.txt
├── README
├── setup.py
└── your_package (FOLDER)
    ├── __init__.py
    ├── ...
    └── subpackage (FOLDER)
        ├── __init__.py

Preparing setup.py

An important part of this configuration is setup.py, which describes the library packaging process. The contents will be as follows.

setup.py


from setuptools import setup


requires = ["requests>=2.14.2"]


setup(
    name='your_package',
    version='0.1',
    description='Awesome library',
    url='https://github.com/whatever/whatever',
    author='yourname',
    author_email='[email protected]',
    license='MIT',
    keywords='sample setuptools development',
    packages=[
        "your_package",
        "your_package.subpackage",
    ],
    install_requires=requires,
    classifiers=[
        'Programming Language :: Python :: 3.6',
    ],
)

In addition, there are many examples of posting the archive link of GitHub on download_url, but it is unnecessary unless there is a request from the GitHub side (in the first place, it should not be necessary if uploading). Many libraries also do not mention download_url in setup.py.

You can actually do a lot more, but basically it's okay if the above are met. There is an official sample project for details, so I think you should refer to that.

pypa/sampleproject

You may also want to take a look at the well-known library setup.py. With this setup.py, you can package your library.

Library packaging

After creating setup.py, you can package it with the following command.

python setup.py sdist

sdist means" source distribution ", and as the name implies, it is a source code-based distribution. Therefore, when performing pip install, the build process based on the description of setup.py will be executed on the user side.

If this build process differs depending on the OS, Python 2 and 3 ... and so on, it is possible to distribute a pre-built package. Wheel is used for that. With the advent of this wheel, it's almost impossible to get an error on Windows.

To use wheel, first install wheel.

pip install wheel

After installing wheel, you will be able to use the bdist_wheel option, which will be used for packaging.

python setup.py bdist_wheel

I won't go into detail here because the usage of wheel is deep, but if you want to provide a secure installation on various platforms, you should consider the wheel format.

Library upload

Now, let's upload it. First, install the twine required for uploading in advance.

pip install twine

All you have to do is upload the packaged folder.

twine upload dist/*

reference

Recommended Posts

Publish / upload a library created in Python to PyPI
[For beginners] How to register a library created in Python in PyPI
[Python] Created a method to convert radix in 1 second
A memorandum to register the library written in Hy in PyPI
Developed a library to get Kindle collection list in Python
Created a Python library to write complex comprehensions and reduce in an easy-to-read manner
I created a Python library to call the LINE WORKS API
I created a password tool in Python.
How to send a visualization image of data created in Python to Typetalk
How to use the C library in Python
How to clear tuples in a list (Python)
To execute a Python enumerate function in JavaScript
How to embed a variable in a python string
I want to create a window in Python
How to use Python Image Library in python3 series
How to create a JSON file in Python
A memo created in a package and registered in PyPI
A clever way to time processing in Python
Steps to develop a web application in Python
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
[Python] Created a class to play sin waves in the background with pyaudio
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
Try to make a Python module in C language
I want to write in Python! (2) Let's write a test
[Python] How to expand variables in a character string
Create a plugin to run Python Doctest in Vim (2)
I tried to implement a pseudo pachislot in Python
Create a plugin to run Python Doctest in Vim (1)
I want to randomly sample a file in Python
Things to note when initializing a list in Python
Created a Python library DateTimeRange that handles time ranges
[Python] A convenient library that converts kanji to hiragana
Introduction to Linear Algebra in Python: A = LU Decomposition
[GCF + Python] How to upload Excel to GCS and create a new table in BigQuery
Take a screenshot in Python
Create a function in Python
To flush stdout in Python
A road to intermediate Python
Login to website in Python
Upload a file to Dropbox
Overriding library functions in Python
Speech to speech in python [text to speech]
How to develop in Python
Draw a heart in Python
Post to Slack in Python
How to debug the Python standard library in Visual Studio
Published a library that hides character data in Python images
How to import Python library set up in EFS to Lambda
A story about how to specify a relative path in python.
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
A simple way to avoid multiple for loops in Python
I published my own Python baseball library to Packaging & PyPI
A standard way to develop and distribute packages in Python
I tried "How to get a method decorated in Python"