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
目录
相关文章
|
2月前
|
Python
【Python】已解决:AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding’
【Python】已解决:AttributeError: module ‘sys’ has no attribute ‘setdefaultencoding’
57 0
|
2月前
|
Unix API Python
【Python】已完美解决:(Python3.8异常)AttributeError: module ‘time‘ has no attribute ‘clock‘
【Python】已完美解决:(Python3.8异常)AttributeError: module ‘time‘ has no attribute ‘clock‘
50 0
|
1月前
|
数据处理 Python
【Python】解决tqdm ‘module‘ object is not callable
在使用tqdm库时遇到的“'module' object is not callable”错误,并给出了正确的导入方式以及一些使用tqdm的常见示例。
47 1
|
17天前
|
关系型数据库 MySQL Linux
【Azure 应用服务】[App Service For Linux(Function) ] Python ModuleNotFoundError: No module named 'MySQLdb'
【Azure 应用服务】[App Service For Linux(Function) ] Python ModuleNotFoundError: No module named 'MySQLdb'
|
2月前
|
Linux 网络安全 开发者
【Python】已解决:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module i
【Python】已解决:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module i
200 3
|
2月前
|
机器学习/深度学习 IDE TensorFlow
【Python】已解决ModuleNotFoundError: No module named ‘tensorflow‘
【Python】已解决ModuleNotFoundError: No module named ‘tensorflow‘
37 1
|
2月前
|
Python
【Python】已解决ModuleNotFoundError: No module named ‘requests’
【Python】已解决ModuleNotFoundError: No module named ‘requests’
193 2
|
2月前
|
自然语言处理 开发者 Python
【Python】已解决:ModuleNotFoundError: No module named ‘nltk’
【Python】已解决:ModuleNotFoundError: No module named ‘nltk’
42 0
【Python】已解决:ModuleNotFoundError: No module named ‘nltk’
|
2月前
|
自然语言处理 开发者 Python
【Python】已解决:ModuleNotFoundError: No module named ‘nltk‘
【Python】已解决:ModuleNotFoundError: No module named ‘nltk‘
53 1
|
2月前
|
机器学习/深度学习 Python
【Python】已解决:ModuleNotFoundError: No module named ‘paddle’
【Python】已解决:ModuleNotFoundError: No module named ‘paddle’
85 1
下一篇
DDNS