PyPI
Python包索引(Python Package Index, PyPI)为Internet上的第三方Python模块提供了一个集中的存储库,我们平时pip install的Python库都来自这个存储库,我们也可以将自己写好的模块上传到PyPI上面开源,让世界上所有的人都能看到你的python模块。
为了测试,我写了一个能完成经纬度转换为地理位置的小小demo(用的geopy库),代码如下:
from geopy.geocoders import Nominatim
"""author :longlong
purpose :practice
the function of the module:input latitude,and then convert latitude to location """
def print_position(lat,lng):
geolocator = Nominatim()
location = geolocator.reverse(str(lat)+','+str(lng),timeout=None)
print(location.address)
print_position函数有两个参数,分别是纬度和经度,功能是打印出所对应的地理信息。
准备发布
为了共享创建的这个模块,需要准备一个发布(distribution)的环境。python中,所谓的distribution是指一个文件集合,这些文件联合在一起允许你构建、打包和发布你的模块。
1. 新建一个文件夹,命名为lat_to_pos
2. 在lat_to_pos中创建一个setup.py文件,代码是你的个人信息以及模块信息,以上述代码为基准的setup.py代码如下:
from distutils.core import setup
setup(
name = 'lat_to_pos',
version = '1.0.0',
py_modules = ['lat_to_pos'],
author = 'longlong',
author_email = '1294054316@qq.com',
url = 'http://www.sunlainglong.cn',
description = 'convert latitude to location',
)
构建发布
1. 在lat_to_pos文件夹中打开一个终端窗口,键入命令:python setup.py sdist
- lat_to_pos
- bulid
- lib
- lat_to_pos.py - dist
- lat__to_pos-1.0.0.tar.gz 发布包 - MANIFEST 包含发布中的文件列表
- lat_to_pos.py python模块
- setup.py 元数据
- bulid
并且,现在就可以本地导入模块使用了,代码如下: