1.定义一个函数
函数已经发布,则升级时,为新添加的变量设定缺省值的设定可以保证不同版本函数的兼容。
2.函数转化为模块
函数保存到一个适当命名的文件中nester.py.
3.模块发布
文件夹命名为文件,包含发布的元数据。首先从python发布工具导入“setup”函数
setup(
name = 'nester',version ='1.0.0',py_modules =['nester'],auther ='a'
auther_email ='a@s'
description =''
)
4.构建发布
功能,打开cmd命令行,定位到nester文件夹,输入:
setup.py install
文件夹。
5.上传到PyPI共享
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';">linux:python2.7 setup.py sdist upload
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';">windows:C:\Python27\python.exe <span style="font-size:13.3333339691162px;">setup.py sdist upload
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">6.debug过程
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">import模块报错,主要有以下错因:
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">(1)汉字注释。解决:开头添加#coding: utf-8
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;">(2)书上用python3,实际用python2.7.在同行输出有问题
<p style="font-size:13.3333339691162px;"><span style="font-family:'FangSong_GB2312';"><span style="font-size:13.3333339691162px;"><span style="border:0px;font-size:15px;color:rgb(46,46,46);font-family:'Microsoft YaHei','宋体','Myriad Pro',Lato,'Helvetica Neue',Helvetica,Arial,sans-serif;line-height:26.6666679382324px;">Python
2
<span style="border:0px;font-size:15px;color:rgb(46,sans-serif;line-height:26.6666679382324px;">Python 3
Python 2.7.6
Hello,World!
Hello,World!
text print more text on the same line
<span style="border:0px;font-family:'Microsoft YaHei',sans-serif;">
<span style="border:0px;font-size:15px;color:rgb(46,sans-serif;line-height:26.6666679382324px;">(3)NameError:
- <code class="language-python">print('Python',python_version())
- print('Hello,World!')
- print("some text,",end="")
- print(' print more text on the same line')
结果
Python 3.4.1
Hello,World!
some text,print more text on the same line
print 'Hello,World!'
报错
File "
",line 1
print 'Hello,World!'
^
name 'sys' is not defined<span style="color:#2e2e2e;"><span style="font-size:15px;line-height:26.6666679382324px;">解决:import sys<span style="color:#2e2e2e;"><span style="font-size:15px;line-height:26.6666679382324px;">代码更改为:<span style="border:0px;font-size:15px;color:rgb(46,sans-serif;line-height:26.6666679382324px;">
<pre name="code" class="python">#coding: utf-8
- <code class="language-python">
import sys
def print_lol(the_list,fh=sys.stdout):
"""
格式化输出列表(包含嵌套和非嵌套列表),一次显示一行,嵌套列表可以缩进
indent:是否缩进;level:tab缩进个数;fh为写入文件地址,默认显示到屏幕
"""
for each_item in the_list:
if isinstance(each_item,fh)
else:
if indent:
for tab_stop in range(level):
file=fh
print "\t",;print file
file=fh
print(each_item,file)
导入成功: