I recently made my PyPI debut. So I will write a registration procedure for those who are thinking of making a PyPI debut for the same purpose.
It seems to be an abbreviation for Python Package Index. A service for managing Python packages, anyone can register a package. The packages registered here can be installed with `` `pip install```. By the way, it seems to read Pipey Eye.
First, prepare the package to be registered. This time, I will create a package called "pypipkg" as an example. The file structure looks like this. Prepare the information necessary for registering PyPI such as setup.py directly under the pypipkg directory, and also place the pypipkg source directory.
pypipkg $ tree
├── MANIFEST.in
├── README.rst
├── pypipkg
│ ├── __init__.py
│ ├── dependency.py
│ ├── scripts
│ │ ├── __init__.py
│ │ └── command.py
│ └── verify.py
├── requirements.txt
└── setup.py
The configuration prepared for the first time is like the above. Later, the directories needed for pip will be added during the registration process.
pypipkg $ tree
├── MANIFEST.in
├── README.rst
├── dist
│ ├── pypipkg-1.0.0.tar.gz
│ ├── pypipkg-1.0.1.tar.gz
│ ├── pypipkg-1.0.2.tar.gz
│ ├── pypipkg-1.0.3.tar.gz
│ └── pypipkg-1.0.4.tar.gz
├── pypipkg
├── pypipkg.egg-info
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ ├── dependency_links.txt
│ ├── entry_points.txt
│ ├── requires.txt
│ └── top_level.txt
├── requirements.txt
└── setup.py
Setup.py is important for PyPi registration. Based on the information in this setup.py, it can be recognized as a package and registered. Probably the most troublesome to create setup.py with PyPI registration.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import os
from setuptools import setup, find_packages
try:
with open('README.rst') as f:
readme = f.read()
except IOError:
readme = ''
def _requires_from_file(filename):
return open(filename).read().splitlines()
# version
here = os.path.dirname(os.path.abspath(__file__))
version = next((line.split('=')[1].strip().replace("'", '')
for line in open(os.path.join(here,
'pypipkg',
'__init__.py'))
if line.startswith('__version__ = ')),
'0.0.dev0')
setup(
name="pypipkg",
version=version,
url='https://github.com/user/pypipkg',
author='kinpira',
author_email='[email protected]',
maintainer='kinpira',
maintainer_email='[email protected]',
description='Package Dependency: Validates package requirements',
long_description=readme,
packages=find_packages(),
install_requires=_requires_from_file('requirements.txt'),
license="MIT",
classifiers=[
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'License :: OSI Approved :: MIT License',
],
entry_points="""
# -*- Entry points: -*-
[console_scripts]
pkgdep = pypipkg.scripts.command:main
""",
)
At first glance, it feels like you're doing a lot of difficult things, but the important thing is in setup (). It is OK if you write each setting item here. The items specified in the setup.py example above are as follows.
install_requires=[
"hoge",
],
In summary, the MIT License is the following license.
- Anyone can use this software for free and unlimited. However, the copyright notice and this license notice must appear on all copies or important parts of the software.
- The author or copyright holder is not responsible for the software.
classifiers: Related categories
The one that seems to be hooked on the search by category in PyPI (Category list)
entry_points: Create a command that provides the functionality of the package
How to write: `` `command name = script path: function to execute ```
This information is also explained in the python documentation.
After creating setup.py, this time we are using the method of specifying requirements.txt, so create a "MANIFEST.in" file. MANIFEST.in can be included in the package by specifying it when you want the package to intentionally include files that are not included in the target python directory.
By the way, if requirements.txt etc. are specified like this time, if MANIFEST.in does not exist, an error will occur because requirements.txt does not exist at the time of installation.
When you're ready to package, start by packaging and testing locally. If you want to test locally, you can package it with the `` `python setup.py develop``` command and install it in site-packages.
$ cd ~/pypipkg
$ python setup.py develop
This will allow you to call it with import in your python project. The command created by entry_points in setup.py can also be used locally, so you can check if it works as a package without any problem.
By registering with TestPyPI before registering with the production PyPI, you can perform a series of operations such as checking the display of the PyPI page and installing, just like the production.
First of all, you need to register an account as in the actual production, so create an account.
↓ The TestPyPI page is written as TESTING SITE like this.
pypirc
If you want to use TestPyPi, you need to create .pypirc in the HOME directory as shown below. (Reference)
$ vim ~/.pypirc
[distutils]
index-servers =
pypi
pypitest
[pypi]
repository=https://pypi.python.org/pypi
username=your_username
password=your_password
[pypitest]
repository=https://testpypi.python.org/pypi
username=your_username
password=your_password
TestPyPI register Once pypirc is created, go directly under the package where setup.py is located and use the register command to register the package information in TestPyPI.
python setup.py register -r https://testpypi.python.org/pypi
TestPyPI search After registering the information with register, it seems that you can check it with the search command. (In my case, it didn't work out for some reason)
pip search --index https://testpypi.python.org/pypi pypipkg
TestPyPI upload The register command only registers the package information, but the gzip (source code) of the package itself has not been uploaded yet, so registration is completed only after uploading the substance with the upload command.
python setup.py sdist upload -r https://testpypi.python.org/pypi
TestPyPI install After uploading, you can install with ``` pip install` ``.
pip install --index-url https://testpypi.python.org/simple/ pypipkg
I think you have successfully installed it. Once you've confirmed this, all you have to do is publish it to the production PyPI.
Now let's publish it to the production PyPI.
You need to register an account here as well, so create an account.
https://pypi.python.org/
Once you have an account, we will register. The procedure is the same as for TestPyPI.
PyPI register First, register with the register command.
python setup.py register
PyPI upload Next, use the upload command to upload the entity.
python setup.py sdist upload
Yes! Now that registration is complete, let's check it on the PyPI site. Like this
It's published perfectly, so you can now install it with `pip install`
!
At first I thought that setup.py was troublesome, but once I wrote it, the registration work after that was very easy. Besides, the environment for testing is properly prepared, so I was able to make my PyPI debut with less resistance than I expected.
Recommended Posts