本文将描述一下如何将我们自己写的模块加入到pypi.python.org.
创建一个空目录, 用来放将要上传的代码
创建一个py文件, 存放你要上传的代码. 例如我这里要上传一个输出列表的函数.
[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
执行python setup.py sdist打包
打包后如下 :
打包好模块后, 可以在本地安装这个模块
[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
一个要写全路径, 一个不需要写全路径
以下方式导入的模块, 调用模块中的函数时, 需要写全路径
接下来是将模块导入pypi.python.org
[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注册的用户名密码.
上传完以后, 就可以在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搜索到刚上传的模块了.