python制作模块
自己写的函数,为了下一次方便用,做成模块
主要有这几个步骤:
1:准备发布
2:构建发布
3:导入模块并使用
1:准备发布
首先,我自己写的一个打印出列表(含嵌套列表),打印出列表中的每个数据项,文件名为print_list.py
#encoding=utf8 """这是print_list模块,提供了一个名为print_list()的函数,这个函数的作用是打印列表,其中有可能包含嵌套列表""" def print_list(lists):
"""这个函数取一个位置参数,名为lists,这可以是任何python列表(也可以是包含嵌套列表的列表),所指定的列表中的每个数据项会
(递归的)输出到屏幕上,各数据项占一行"""
for each_item in lists:
if isinstance(each_item,list):
print_list(each_item)
else:
print(each_item)
然后,准备setup.py文件,在这个文件里包含有关发布的元数据
from distutils.core import setup setup(
name="print_list",
version="1.0.0",
py_modules=['print_list'],
author="lile",
author_email="836217653@qq.com",
url = "http://www.cnblogs.com/lemon-le/",
description="A simple printer of print_list lists"
)
2:构建发布
把print_list.py与setup.py放在同一个目录下,并且在这个目录下,执行python3 setup.py sdist构建一个发布文件
lile@vst:~/python/heihei$ python3 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 print_list-1.0.0
making hard links in print_list-1.0.0...
hard linking print_list.py -> print_list-1.0.0
hard linking setup.py -> print_list-1.0.0
creating dist
Creating tar archive
removing 'print_list-1.0.0' (and everything under it)
然后,将发布安装到本地副本中,也就是安装到python3的安装目录下面去sudo python3 setup.py install
lile@vst:~/python/heihei$ sudo python3 setup.py install
[sudo] password for lile:
running install
running build
running build_py
creating build
creating build/lib
copying print_list.py -> build/lib
running install_lib
running install_egg_info
Removing /usr/local/lib/python3.4/dist-packages/print_list-1.0.0.egg-info
Writing /usr/local/lib/python3.4/dist-packages/print_list-1.0.0.egg-info
3:导入模块并使用
然后就可以使用刚刚这个模块了,设置一个列表变量,倒入print_list模块,并调用print_list函数打印出列表的所有数据项
lile@vst:~/python/heihei$ python3
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import print_list
>>> a=["Sun","Rain",["simle",["heihei","Today is beautiful day"]]]
>>> print_list.print_list(a)
Sun
Rain
simle
heihei
Today is beautiful day
python制作模块的更多相关文章
- Python——制作模块
步骤一:创建包 步骤二:编辑示例模块代码 __init__调用: 步骤三:创建setup.py from distutils.core import setup setup(name="pa ...
- 如何制作python安装模块(setup.py)
Python模块的安装方法: 1. 单文件模块:直接把文件拷贝到$python_dir/lib 2. 多文件模块,带setup.py:python setup.py install 3. egg文件, ...
- python制作简单excel统计报表2之操作excel的模块openpyxl简单用法
python制作简单excel统计报表2之操作excel的模块openpyxl简单用法 # coding=utf-8 from openpyxl import Workbook, load_workb ...
- python 常用模块(转载)
转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:B ...
- 使用python制作ArcGIS插件(4)界面交互
使用python制作ArcGIS插件(4)界面交互 by 李远祥 插件界面部分,除了一开始在设计器中设计的这些界面元素之外,还可以与操作系统进行一些输入输出的交互,这部分的实现全部在pythonadd ...
- 使用python制作ArcGIS插件(3)ArcPy的使用说明
使用python制作ArcGIS插件(3)ArcPy的使用说明 by 李远祥 ArcPy 是一个以成功的 arcgisscripting 模块为基础并继承了 arcgisscripting 功能进而构 ...
- 使用python制作ArcGIS插件(1)工具介绍
使用python制作ArcGIS插件(1)工具介绍 by 李远祥 ArcGIS从10.0开始支持addin(ArcGIS软件中又叫作加载项)的方式进行插件制作.相对于以往9.x系列,addin的无论是 ...
- python高级-模块(14)
一.python中的模块 有过C语言编程经验的朋友都知道在C语言中如果要引用sqrt函数,必须用语句#include <math.h>引入math.h这个头文件,否则是无法正常进行调用的. ...
- Python之模块和包
一.模块 1.什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...
随机推荐
- DAY15 模块
一.模块 1.1 模块的定义:模块就是一系列功能的集合体 1.2 模块的四种存在方式: 1.使用python编写的.py文件(任一py文件都可以作为模块) 2.包:一堆py文件的集合体 3.使用C编写 ...
- Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String
题意:有一个串s,n个串模式串t,问s的子串中长度最小的包含t k次的长度是多少 题解:把所有t建ac自动机,把s在ac自动机上匹配.保存每个模式串在s中出现的位置.这里由于t两两不同最多只有xsqr ...
- [luogu P3648] [APIO2014]序列分割
[luogu P3648] [APIO2014]序列分割 题目描述 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列分割成k+1个非空的子序列.为了得到k+1个子序 ...
- angular学习2
1.为了在angular里面使用bootstrap,可以如下操作 (1)停止正在运行的终端指令:ctrl+c (2)在终端里面输入:npm install bootstrap --save (3)在V ...
- C++获取数组的长度
C++获取数组的长度 #include<iostream> using namespace std; template<class T> int length(T& a ...
- 使用Python爬取代理ip
本文主要代码用于有代理网站http://www.kuaidaili.com/free/intr中的代理ip爬取,爬虫使用过程中需要输入含有代理ip的网页链接. 测试ip是否可以用 import tel ...
- ubuntu 16.04 编译安装 trl8291cu系列 无线网卡驱动
1 先 下载git包 和相关编译工具 sudo apt-get update sudo apt-get install git linux-headers-generic build-essentia ...
- CT ubuntu 16.04安装 adobe flash player
sudo apt-get install flashplugin-installer chrome 升级 chrome://chrome-urls/ chrome://components/ 找到A ...
- Linux:CentOS 7系统的安装
相信有看过我写的博文就知道我写的第一篇博文就是CentOS 7系统的安装,不过是在虚拟机中安装的,而且还是直接加载镜像文件进去的,不过这次我就通过PE来安装,来证实下PE是否可以用来安装Linux系统 ...
- oracle 链接
<properties> <!--配置Hibernate方言 --> <property name="hibernate.dialect" value ...