未加星标

Packaging and Publishing Python code

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

If you have been coding in python, even for a while, you must be familiar with the concept of ‘pip’ by now. It is a package management system used to install and manage software packages/libraries written in Python.

Then one may ask that where are all these packages/libraries stored? It is obvious that there must be a large “on-line repository” which stores all this code. And the answer to this question is Python Package Index (PyPI).


Packaging and Publishing Python code

PyPI is the official third-party software repository for Python. At the time of writing this article, PyPI was already hosting 95971 packages!

pip uses PyPI as the default source for packages and their dependencies. So whenever you type:

pip install package_name

pip will look for that package on PyPI and if found, it will download and install the package on your local system.

In this article, I will demonstrate how you can publish your own Python package on PyPI so that it is one-line installable and easily available to all other python users online! I will take the example of a sample package and show you the complete process. The example package is hosted on Github .

Step 1: Get the python scripts ready

First step is, of course, getting your python program (which you want to publish on PyPI) ready!

It could be any python script. Here I am using my own python script which I have named ‘ locator.py ‘ (I am using this name just for reference purpose. Feel free to save your python scripts by any name.) This file is available here .

Step 2: Getting the package-directory structure ready

This is the most important step. Now, we have to follow some pre-defined structure for our package’s directory.

As a reference, feel free to check out the Github repository of the sample project which is used in this tutorial. You can clone this repository and make some modifications to create your own package.

The directory structure has to be like this:


Packaging and Publishing Python code

Ok let’s discuss what all these files will contain.

setup.py : It is the most important file. It’s the file where various aspects of your project are configured. The primary feature of setup.py is that it contains a global setup() function. The keyword arguments to this function are how specific details of your project are defined.
You will need to install this setuptools

library using pip:

pip install setuptools

Here is how my setup.py looks like:

from setuptools import setup
# reading long description from file
with open('DESCRIPTION.txt') as file:
long_description = file.read()
# specify requirements of your package here
REQUIREMENTS = ['requests']
# some more details
CLASSIFIERS = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Internet',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
]
# calling the setup function
setup(name='mygmap',
version='1.0.0',
description='A small wrapper around google maps api',
long_description=long_description,
url='https://github.com/nikhilkumarsingh/mygmap',
author='Nikhil Kumar Singh',
[email protected]',
license='MIT',
packages=['geo'],
classifiers=CLASSIFIERS,
install_requires=REQUIREMENTS,
keywords='maps location address'
)

Let us see what different arguments of setup function do:

name : It is the name of your project. Your package will be listed by this name on PyPI. version : It is a string in which you can specify the current version of your project.
It is totally your choice how you want to set the scheme of the series of versions (You may use ‘1.0’ or ‘0.1’ or even ‘0.0.1’).
This version is displayed on PyPI for each release if you publish your project. Every-time you upload a new version, you will have to change this argument as well. description: A short description about the package. You can use long_description argument to write long descriptions. long_description: We can use rich text for better description of our files. The default file format is reStructuredText . You can have a look at DESCRIPTION.txt to get a feel of the syntax. url: A homepage URL for your project. This makes it easier for people to follow or contribute to your project. author, author_email: Details about the author license: specify the type of license you are using. classifiers: It is a list of strings in which we can specify more details about our project like its development status, topic, license and supported python versions for your project. You can see more classifiers here . install_requires: It can be used to specify what third-party libraries your package needs to run. These dependencies will be installed by pip when someone installs your package. keywords: List keywords to describe your project. DESCRIPTION.txt : This file contains the long description about our package to show on the PyPI page. We use reStructuredText file format here. Check the file used in our package here . LICENSE.txt : It is a good practice to set a license for the usage of your project. You can use any of the freely available templates. Most commonly used is the MIT license.
The license I am using for this project is available here .(You may replace the Author’s name for using this license in your projects) README.md : This file has got nothing to do with our PyPI package. It contains description to be shown on the Github page. You can use it for PyPI page too but it will need some more modifications to our code. For now, lets keep it simple. __init__.py : The primary use of __init__.py is to initialize a python package.
The inclusion of this file in a directory indicates to the Python interpreter that the directory should be treated like a Python package.
You can leave this file empty.

Step 3: Create your accounts

Now, its time to create an account on PyPI and Test PyPI .Test PyPI is just a testing site where we will upload our code first to see if everything works correctly or not.

Once accounts have been made, create this .pypirc file in home directory of your system and enter the account details.

[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

Note : If you are on a windows system, just type echo %USERPROFILE% in command prompt to know the home directory of your PC. Put the .pypirc file there.

Step 4: Upload the package

Finally, we are ready to upload our package on PyPI!

First of all, we will check if our package installs correctly on Test PyPI.
Open command prompt/terminal in the root directory of your package.

Run this in terminal:

python setup.py register -r pypitest

This will attempt to register your package against PyPI’s test server, just to make sure you’ve set up everything correctly.

Now, run this:

python setup.py sdist upload -r pypitest

You should get no errors, and should also now be able to see your library in the test PyPI repository .

Once you’ve successfully uploaded to PyPI Test, perform the same steps but point to the live PyPI server instead.

To register on PyPI, run:

python setup.py register -r pypi

Then, run:

python setup.py sdist upload -r pypi

And you’re all done! Your package is now publicly available on PyPI and could be easily installed by a simple pip command!

The package we created using this tutorial is available here .

Just type on terminal,

pip install your_package_name

to check if installation process is getting completed successfully.

References:

http://peterdowns.com/posts/first-time-with-pypi.html https://packaging.python.org/distributing/

This article is contributed by Nikhil Kumar . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: GitPythonERPTIADM
分页:12
转载请注明
本文标题:Packaging and Publishing Python code
本站链接:http://www.codesec.net/view/520791.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(105)