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.
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.
distutils
in setup.py
setuptools
distutils
in .pypirc
python setup.py register
or python setup.py sdist upload
twine
, so use this.The following preparations are required before publishing a package to PyPI.
.pypirc
with account credentials (optional)Create a PyPI account below.
.pypirc
with account credentialsIf 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.
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
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',
],
)
name
: The name of the package. Match with the actual package folder nameversion
: The version of the package. You will need to change this when updating.packages
: Specifies the packages to install. Note that if you have a subpackage, you need to specify it. This package is all! In that case, use find_packages
.: List any libraries that the package depends on. The notation is the same as the format obtained with
pip freeze`classifiers
/ keywords
: You can write tags and keywords when searching for packages. The list of classifiers
is hereIn 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.
You may also want to take a look at the well-known library setup.py
.
With this setup.py
, you can package your library.
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.
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/*
Recommended Posts