add your python module to pypi.python.org-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

add your python module to pypi.python.org

简介:
本文将描述一下如何将我们自己写的模块加入到pypi.python.org.
创建一个空目录, 用来放将要上传的代码
[root@localhost ~]# mkdir pydigoal
[root@localhost ~]# cd pydigoal/

创建一个py文件, 存放你要上传的代码. 例如我这里要上传一个输出列表的函数.
[root@localhost pydigoal]# vi nester.py
"""this is comment
comment end"""

def print_lol(the_list):
  """this is function comment
  comment end"""
  for each_item in the_list:
    if isinstance(each_item, list):
      print_lol(each_item)
    else:
      print(each_item)

编写setup.py, 格式如下. 从distutils.core中导入setup函数.
这种导入的函数不需要写全路径, 如果是import distutils.core, 则需要写全路径distutils.core.setup
[root@localhost pydigoal]# vi setup.py
from distutils.core import setup
setup(
    name = 'nester',
    version = '1.0.0',
    py_modules = ['nester'],
    author = 'digoal',
    author_email = 'digoal@126.com',
    url = 'http://blog.163.com/digoal@126',
    description = 'a test module by digoal'
)

执行python setup.py sdist打包
[root@localhost pydigoal]# python setup.py sdist
running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default file list)

warning: sdist: standard file not found: should have one of README, README.txt

writing manifest file 'MANIFEST'
creating nester-1.0.0
making hard links in nester-1.0.0...
hard linking nester.py -> nester-1.0.0
hard linking setup.py -> nester-1.0.0
creating dist
Creating tar archive
removing 'nester-1.0.0' (and everything under it)

打包后如下 : 
[root@localhost pydigoal]# ll -R
.:
total 12
drwxr-xr-x 2 root root  32 Jan 26 17:25 dist
-rw-r--r-- 1 root root  62 Jan 26 17:25 MANIFEST
-rw-r--r-- 1 root root 232 Jan 26 17:22 nester.py
-rw-r--r-- 1 root root 263 Jan 26 17:24 setup.py

./dist:
total 4
-rw-r--r-- 1 root root 586 Jan 26 17:25 nester-1.0.0.tar.gz

打包好模块后, 可以在本地安装这个模块
[root@localhost pydigoal]# python setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying nester.py -> build/lib
running install_lib
copying build/lib/nester.py -> /usr/lib/python2.7/site-packages
byte-compiling /usr/lib/python2.7/site-packages/nester.py to nester.pyc
running install_egg_info
Writing /usr/lib/python2.7/site-packages/nester-1.0.0-py2.7.egg-info

如何使用本地已安装的模块呢?
很简单, 导入即可, 导入有两种方式, 一种是from, 另一种是直接import
一个要写全路径, 一个不需要写全路径
[root@localhost pydigoal]# cd
[root@localhost ~]# vi test.py
from nester import print_lol
  
list = ['hello',['i',['am','digoal']]]
print_lol(list)

[root@localhost ~]# python ./test.py
hello
i
am
digoal


以下方式导入的模块, 调用模块中的函数时, 需要写全路径
[root@localhost ~]# vi test.py
import nester

list = ['hello',['i',['am','digoal']]]
nester.print_lol(list)

[root@localhost ~]# python ./test.py
hello
i
am
digoal


接下来是将模块导入pypi.python.org
[root@localhost pydigoal]# python setup.py register
running register
running check
We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]: 
1
Username: digoal
Password: 
Registering nester to http://pypi.python.org/pypi
Server response (403): You are not allowed to store 'nester' package information

注意nester这个模块名已经被占用了, 所以需要改给名字再上传.
更改为digoal_nester
[root@localhost pydigoal]# vi setup.py 
from distutils.core import setup
setup(
    name = 'digoal_nester',
    version = '1.0.0',
    py_modules = ['digoal_nester'],
    author = 'digoal',
    author_email = 'digoal@126.com',
    url = 'http://blog.163.com/digoal@126',
    description = 'a test module by digoal'
)

文件名也改一下
[root@localhost pydigoal]# mv nester.py digoal_nester.py 

再次上传, 通过. 
第一次上传时, 需要输入在pypi注册的用户名密码.
[root@localhost pydigoal]# python setup.py register
running register
running check
We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]: 

Username: digoal
Password: 
Registering digoal_nester to http://pypi.python.org/pypi
Server response (200): OK
I can store your PyPI login so future submissions will be faster.
(the login will be stored in /root/.pypirc)
Save your login (y/N)?y


上传完以后, 就可以在pypi搜索到刚上传的模块了.
add your python module to pypi.python.org - 德哥@Digoal - PostgreSQL research

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章