首页 > 其他分享 >第二章 共享代码

第二章 共享代码

时间:2022-10-20 16:23:38浏览次数:73  
标签:__ 02 1.0 setup py ex 共享 第二章 代码

一、将模块中实现单一功能的代码块变成函数(方法)

 

def function_name(arg1,arg2,...):

...

 

return

 

二、注释代码

代码注释至少应该包含两部分:

第一部分,放在整个模块的头部位置,用于描述模块

第二部分,放在函数的起始位置,用于描述单个函数

 

三、准备发布

*本篇文章介绍的是使用distutils + setup.py 打包发布自定义的python包。

#再次强调一下,本验证工作环境:window10,python3.8,编辑工具 为 Geany 

说明:Python只把含__init__.py文件的目录当成包(Python3.2之后的版本不需要再额外的去专门创建一个__init__.py文件)。最简情况下,__init__.py只是一个空文件,但该文件也可以执行包的初始化代码,或设置__all__变量。当import指定的包时,此包内的__init__.py会被隐性执行,且只执行一次。

 

1)创建一个文件夹 #ex(文件夹绝对路径e:\pthw\HFPython\ex)

2)新建一个setup.py的文件 #ex文件夹下面已经包含了ex_01.py,ex_02.py 两个文件

 

*******************************************************

from distutils.core import setup #distutils + setup.py 

 

setup(

    name = '文件夹名称',

    verision = '1.0.0',

    py_modules = ['文件名1','文件名2',...],

    author = 'Oldman',

    author_email = '[email protected]',

    url = '主页网址',

    description = '关于文件包的描述',

    )

 

3)实际验证:

 

***************code:*****************************

from distutils.core import setup

 

setup(

  #package_dir = {'':'..\dir'} #setup的相对路径;

  #packages = ['package1','package2',...] #包含路经下的文件夹名,必须带__init__.py,可以多个;

  #package_data = {'':['config\name.txt']} #配置文件名

  #py_modules = ['module1','module2',...] #不属于包的module脚本

  #上述内容还没有经过验证,待更新

  name = 'ex',

  version = '1.0.0',

  py_modules = ['ex_01','ex_02',],

  author = 'Oldman',

  author_email = '[email protected]',

  url = '主页网址',

  description = 'Head First Python Exercises',

  )

 

**********************执行结果***********************************

E:\Pthw\HFPython\ex>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, README.rst

 writing manifest file 'MANIFEST'

creating ex-1.0.0

making hard links in ex-1.0.0...

hard linking ex_01.py -> ex-1.0.0

hard linking ex_02.py -> ex-1.0.0

hard linking setup.py -> ex-1.0.0

creating dist

Creating tar archive

removing 'ex-1.0.0' (and everything under it)

********************安装到本地副本***************************** 

E:\Pthw\HFPython\ex>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
copying ex_01.py -> build\lib
copying ex_02.py -> build\lib
running install_lib
copying build\lib\ex_01.py -> C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages
copying build\lib\ex_02.py -> C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages
byte-compiling C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages\ex_01.py to ex_01.cpython-38.pyc
byte-compiling C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages\ex_02.py to ex_02.cpython-38.pyc
running install_egg_info
Writing C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib\site-packages\ex-1.0.0-py3.8.egg-info

E:\Pthw\HFPython\ex>

标签:__,02,1.0,setup,py,ex,共享,第二章,代码
From: https://www.cnblogs.com/mmking/p/16809854.html

相关文章