In [5]: import os

In [6]: os.__file__
Out[6]: '/usr/local/lib/python2.7/os.pyc' In [7]: import random In [8]: random.__file__
Out[8]: '/usr/local/lib/python2.7/random.pyc' In [9]: ll /usr/local/lib/python2.7/ |head
total 12276
-rw-r--r-- 1 root 18619 Sep 23 07:20 _abcoll.py
-rw-r--r-- 1 root 26098 Sep 23 07:20 _abcoll.pyc
-rw-r--r-- 1 root 26098 Sep 23 07:21 _abcoll.pyo
-rw-r--r-- 1 root 7145 Sep 23 07:20 abc.py
-rw-r--r-- 1 root 6187 Sep 23 07:20 abc.pyc
-rw-r--r-- 1 root 6131 Sep 23 07:21 abc.pyo
-rw-r--r-- 1 root 34547 Sep 23 07:20 aifc.py
-rw-r--r-- 1 root 30740 Sep 23 07:20 aifc.pyc
-rw-r--r-- 1 root 30740 Sep 23 07:21 aifc.pyo

##############制作简单的模块#############

vim sendmsg.py
#!/usr/local/bin/python3
# -*- coding:utf-8 -*- def test1():
print('---test1---') [root@master module]# vim main.py
#!/usr/local/bin/python3
# -*- coding:utf-8 -*- import sendmsg sendmsg.test1 [root@master module]# python3 main.py
---test1--- [root@master module]# ll
total 16
-rw-r--r-- 1 root root 82 Oct 16 10:15 main.py
drwxr-xr-x 2 root root 4096 Oct 16 10:16 __pycache__
-rw-r--r-- 1 root root 87 Oct 16 10:13 sendmsg.py
-rw-r--r-- 1 root root 260 Oct 16 10:15 sendmsg.pyc
[root@master module]# tree
.
├── main.py
├── __pycache__
│   └── sendmsg.cpython-35.pyc
├── sendmsg.py
└── sendmsg.pyc 1 directory, 4 files
You have new mail in /var/spool/mail/root
[root@master module]# pwd
/home/weixin/module

提示:尽量少使用*号

############################包和模块的概念#################

简单点就是:.py 文件就是模块

包:在一个文件夹里面有py文件,又有__init__.py文件的就是包

[root@master bao]# ll
total 4
drwxr-xr-x 2 root root 4096 Oct 16 14:27 Testmsg ##这是一个目录
[root@master bao]# tree
.
└── Testmsg
├── __init__.py
├── recevemsg.py
└── sendmsg.py 备注:##在python3会认为Testmsg这就是一个包,在没有__init__.py文件的情况下,可以import Testmsg 成功,但是无法使用里面的模块;python2则一定要有
__init__.py文件的情况下,才可以import Testmsg 成功,但是也无法使用里面的模块。 __init__.py 文件只要在import Testmsg 就会被执行;
怎么使用模块呢????
[root@master Testmsg]# vim __init__.py
#!/usr/local/bin/python3
# -*- coding:utf-8 -*- from . import recevemsg
from . import sendmsg [root@master bao]# ipython
In [1]: import Testmsg In [2]: Testmsg.recevemsg.rmsg()
---recevemmessage--- In [3]: Testmsg.sendmsg.smsg()
---sendmessage--- [root@master bao]# python3
Python 3.5.4 (default, Oct 7 2017, 12:39:20)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import Testmsg
>>> Testmsg.sendmsg.smsg()
---sendmessage---
>>> Testmsg.recevemsg.rmsg()
---recevemmessage---

>>> Testmsg.__file__
'/home/weixin/module/bao/Testmsg/__init__.py'
>>> import os
>>> os.__file__
'/usr/local/lib/python3.5/os.py'

###################发布自己的模块####################

[root@master 01-发布模块]# ll
total 12-rw-r--r-- 1 root root 162 Oct 16 16:02 setup.py
drwxr-xr-x 3 root root 4096 Oct 16 16:00 Testmsg
[root@master 01-发布模块]# pwd
/home/weixin/module/01-发布模块

[root@master 01-发布模块]# tree Testmsg/
Testmsg/
├── __init__.py
├── __init__.pyc
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── recevemsg.cpython-35.pyc
│   └── sendmsg.cpython-35.pyc
├── recevemsg.py
├── recevemsg.pyc
├── sendmsg.py
└── sendmsg.pyc

[root@master 01-发布模块]# cat setup.py     #这个文件需要创建,并加入如下内容
from distutils.core import setup

setup(name="fush",version="1.0",description="fush’s module",author="fush",py_modules=["Testmsg.sendmsg",'Testmsg.recevemsg'])

备注:py_modules=["Testmsg.sendmsg",'Testmsg.recevemsg']   Testmsg 是包名也就是文件夹,sendmsg和recevemsg 是模块名

最后执行:

[root@master 01-发布模块]# python3 setup.py build
running build
running build_py
creating build
creating build/lib
creating build/lib/Testmsg
copying Testmsg/__init__.py -> build/lib/Testmsg
copying Testmsg/sendmsg.py -> build/lib/Testmsg
copying Testmsg/recevemsg.py -> build/lib/Testmsg

此时:

[root@master 01-发布模块]# tree
.
├── build
│   └── lib
│   └── Testmsg
│   ├── __init__.py
│   ├── recevemsg.py
│   └── sendmsg.py
├── setup.py
└── Testmsg
├── __init__.py
├── __init__.pyc
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── recevemsg.cpython-35.pyc
│   └── sendmsg.cpython-35.pyc
├── recevemsg.py
├── recevemsg.pyc
├── sendmsg.py
└── sendmsg.pyc

再执行:

[root@master 01-发布模块]# python3 setup.py sdist
running sdist
running check
warning: check: missing required meta-data: url

warning: check: missing meta-data: if 'author' supplied, 'author_email' must be supplied too

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 fush-1.0
creating fush-1.0/Testmsg
making hard links in fush-1.0...
hard linking setup.py -> fush-1.0
hard linking Testmsg/__init__.py -> fush-1.0/Testmsg
hard linking Testmsg/recevemsg.py -> fush-1.0/Testmsg
hard linking Testmsg/sendmsg.py -> fush-1.0/Testmsg
creating dist
Creating tar archive
removing 'fush-1.0' (and everything under it)

此时:

[root@master 01-发布模块]# tree
.
├── build
│   └── lib
│   └── Testmsg
│   ├── __init__.py
│   ├── recevemsg.py
│   └── sendmsg.py
├── dist
│   └── fush-1.0.tar.gz
├── MANIFEST
├── setup.py
└── Testmsg
├── __init__.py
├── __init__.pyc
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── recevemsg.cpython-35.pyc
│   └── sendmsg.cpython-35.pyc
├── recevemsg.py
├── recevemsg.pyc
├── sendmsg.py
└── sendmsg.pyc

再接下来(模拟下载模块安装):

[root@master 01-发布模块]# ll
total 20
drwxr-xr-x 3 root root 4096 Oct 16 16:02 build
drwxr-xr-x 2 root root 4096 Oct 16 16:09 dist
-rw-r--r-- 1 root root 112 Oct 16 16:09 MANIFEST
-rw-r--r-- 1 root root 162 Oct 16 16:02 setup.py
drwxr-xr-x 3 root root 4096 Oct 16 16:00 Testmsg
You have new mail in /var/spool/mail/root
[root@master 01-发布模块]# cd dist/
[root@master dist]# ll
total 4
-rw-r--r-- 1 root root 602 Oct 16 16:09 fush-1.0.tar.gz
[root@master dist]# cp ./fush-1.0.tar.gz ~/
[root@master dist]# cd
[root@master ~]# ll
total 236
-rw-------. 1 root root 1096 May 31 2016 anaconda-ks.cfg
-rw-r--r-- 1 root root 602 Oct 16 16:17 fush-1.0.tar.gz
-rw-r--r-- 1 root root 0 Oct 6 23:11 fush.txt
-rw-r--r--. 1 root root 9795 May 31 2016 install.log
-rw-r--r--. 1 root root 3091 May 31 2016 install.log.syslog
drwxr-xr-x 2 root root 4096 Oct 16 11:32 log
drwxr-xr-x 2 root root 4096 Oct 7 04:47 tool
-rw-r--r-- 1 root root 201676 Sep 26 22:08 wc.jar
-rw-r--r-- 1 root root 333 Oct 10 14:33 存放家具oop.py
[root@master ~]# tar xf fush-1.0.tar.gz
[root@master ~]# ll
total 240
-rw-------. 1 root root 1096 May 31 2016 anaconda-ks.cfg
drwxr-xr-x 3 root root 4096 Oct 16 16:09 fush-1.0
-rw-r--r-- 1 root root 602 Oct 16 16:17 fush-1.0.tar.gz
-rw-r--r-- 1 root root 0 Oct 6 23:11 fush.txt
-rw-r--r--. 1 root root 9795 May 31 2016 install.log
-rw-r--r--. 1 root root 3091 May 31 2016 install.log.syslog
drwxr-xr-x 2 root root 4096 Oct 16 11:32 log
drwxr-xr-x 2 root root 4096 Oct 7 04:47 tool
-rw-r--r-- 1 root root 201676 Sep 26 22:08 wc.jar
-rw-r--r-- 1 root root 333 Oct 10 14:33 存放家具oop.py
[root@master ~]# tree fush-1.0      ##跟之前制作的内容是一样的,只有PKG-INFO是多出来的
fush-1.0
├── PKG-INFO
├── setup.py
└── Testmsg
├── __init__.py
├── recevemsg.py
└── sendmsg.py

1 directory, 5 files
[root@master ~]# cat fush-1.0/PKG-INFO
Metadata-Version: 1.0
Name: fush
Version: 1.0
Summary: fush’s module
Home-page: UNKNOWN
Author: fush
Author-email: UNKNOWN
License: UNKNOWN
Description: UNKNOWN
Platform: UNKNOWN

最后安装:

[root@master ~]# cd fush-1.0
[root@master fush-1.0]# ll
total 12
-rw-r--r-- 1 root root 181 Oct 16 16:09 PKG-INFO
-rw-r--r-- 1 root root 162 Oct 16 16:02 setup.py
drwxr-xr-x 2 root root 4096 Oct 16 16:09 Testmsg
[root@master fush-1.0]# python3 setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/Testmsg
copying Testmsg/__init__.py -> build/lib/Testmsg
copying Testmsg/sendmsg.py -> build/lib/Testmsg
copying Testmsg/recevemsg.py -> build/lib/Testmsg
running install_lib
creating /usr/local/lib/python3.5/site-packages/Testmsg
copying build/lib/Testmsg/__init__.py -> /usr/local/lib/python3.5/site-packages/Testmsg
copying build/lib/Testmsg/sendmsg.py -> /usr/local/lib/python3.5/site-packages/Testmsg
copying build/lib/Testmsg/recevemsg.py -> /usr/local/lib/python3.5/site-packages/Testmsg
byte-compiling /usr/local/lib/python3.5/site-packages/Testmsg/__init__.py to __init__.cpython-35.pyc
byte-compiling /usr/local/lib/python3.5/site-packages/Testmsg/sendmsg.py to sendmsg.cpython-35.pyc
byte-compiling /usr/local/lib/python3.5/site-packages/Testmsg/recevemsg.py to recevemsg.cpython-35.pyc
running install_egg_info
Writing /usr/local/lib/python3.5/site-packages/fush-1.0-py3.5.egg-info

测试:

[root@master ~]# python3

>>> import Testmsg

>>> Testmsg.sendmsg.smsg()
---sendmessage---
>>> from Testmsg import sendmsg
>>> sendmsg.smsg()
---sendmessage---

这样就OK 了。

#########模块的导入先后顺序##################

我们import module 的时候,python 是怎么查找的呢?

In [2]: import sys

In [3]: sys.path
Out[3]:
['',
'/usr/local/bin',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython'] 以上就是路劲,默认从当前路径找,然后从上/usr/local/bin到下开始找,由于这是一个列表,所以我们可以使用sys.path.append() 来添加路径;
例如:sys.path.append('/home/weixin')
In [4]: sys.path.append('/home/weixin') In [5]: sys.path
Out[5]:
['',
'/usr/local/bin',
'/usr/local/lib/python27.zip',
'/usr/local/lib/python2.7',
'/usr/local/lib/python2.7/plat-linux2',
'/usr/local/lib/python2.7/lib-tk',
'/usr/local/lib/python2.7/lib-old',
'/usr/local/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/site-packages/IPython/extensions',
'/root/.ipython',
'/home/weixin']

################模块重新导入##########

###############sys.argv###############

#!/usr/local/bin/python3
# -*- coding:utf-8 -*- import sys print(sys.argv)
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2]) [root@master weixin]# python sysargv.py fush tt uu
['sysargv.py', 'fush', 'tt', 'uu'] ##sys.argv
sysargv.py ##sys.agrv[0]
fush ##sys.argv[1]
tt ##sys.argv[2]
 

python 模块和模块sys.argv的更多相关文章

  1. Python踩坑之 sys.argv[-1]代表什么

    平台:win10+python 3.7.0 一.sys说明: sys.argv这个函数是我们写python脚本中最常用的一个函数. sys是Python的一个「标准库」,也就是官方出的「模块」,是「S ...

  2. python 函数学习之sys.argv[1]

    一.sys 模块 sys是Python的一个「标准库」,也就是官方出的「模块」,是「System」的简写,封装了一些系统的信息和接口. 官方的文档参考:https://docs.python.org/ ...

  3. Python命令行参数sys.argv[]

    学习C语言的时候就没弄明白命令行参数的用法,在学习Pyton 的时候又遇到了命令行参数,在这里稍微学习了一下,稍微明白了一些在这里做个记录方便后面回顾复习. Sys.argv[]是用来获取命令行参数的 ...

  4. Python学习日记(八)—— 模块一(sys、os、hashlib、random、time、RE)

    模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...

  5. day18 python模块 random time sys os模块

    day18 python   一.random模块     取随机整数 import random print(random.randint(1,2))                 #顾头顾尾 p ...

  6. python关于操作文件的相关模块(os,sys,shutil,subprocess,configparser)

    一:os模块 os模块提供了许多允许你程序与操作系统直接交互的功能 功能 说明 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirna ...

  7. python之常见模块(time,datetime,random,os,sys,json,pickle)

    目录 time 为什么要有time模块,time模块有什么用?(自己总结) 1. 记录某一项操作的时间 2. 让某一块代码逻辑延迟执行 时间的形式 时间戳形式 格式化时间 结构化时间 时间转化 总结: ...

  8. python sys.argv[]

    sys.argv[]是用来获取命令行参数的,是一个由该脚本自身路径和其它输入的参数组成的List.sys.argv[0]表示代码本身文件路径. 这里,当我们执行python using_sys.py ...

  9. Python的sys.argv使用说明

    刚开始使用这个参数的时候,很不明白其含义.网上搜索很多都是贴的官网上面的一则实例,说看懂,就明白.可是,我看不懂.现在在回头看这个参数使用,并不是很麻烦. 举几个小例子就明白了. 创建一个脚本,内容如 ...

随机推荐

  1. Scut游戏服务器免费开源框架--快速开发(1)

    Scut快速开发(1) 1        开发环境 需要安装的软件 a)        VS2010开发工具(.Net Framework 4.0以上) 2        HelloWorld 2.1 ...

  2. zerorpc的安装

    1.简介及安装 rpc使构建分布式系统简单许多,在云计算的实现中有很广泛的应用 rpc可以是异步的 python实现rpc,可以使用标准库里的SimpleXMLRPCServer,另外zerorpc是 ...

  3. const mutable

    在C++中,由const修饰的成员函数的函数体内部,是不能够对成员变量进行修改的.这个特性被用来保证某些成员函数在实现过程中,避免由于程序员大意而对数据进行了错误的修改:同时也说明此成员函数是非修改性 ...

  4. sql的一些知识_高级

    1.视图 http://www.cnblogs.com/wang666/p/7885934.html 2.存储过程 http://www.cnblogs.com/wang666/p/7920748.h ...

  5. 百科知识 isz文件如何打开

    使用UltraISO可以打开

  6. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  7. .NET MVC 4 实现用户注册功能

    初学MVC,踩了不少坑,所以通过实现一个用户注册功能把近段时间学习到的知识梳理一遍,方便以后改进和查阅. 问题清单: l 为什么EF自动生成的表名后自动添加了s? l 如何为数据库初始化一些数据? l ...

  8. 生产制造追溯系统-通过微信小程序实现移动端报表平台

    前言 前两篇文章主要梳理了一下在生产过程中如何更高效.更稳定的实现条码打印,有不少园子里的朋友私信我,互相讨论了一些技术方面的问题,双方都各有收获,再此感谢博客园提供的这个交流平台,让五湖四海的朋友能 ...

  9. HTML5即将迎来黄金时代 轻应用再成行业焦点

    2015-01-23 11:03:09     来源:快鲤鱼 大众能看到的H5效果拜“微信”所赐,几乎每天都有H5页面的推广以及H5小游戏在微信上传播.其实,H5的大热与百度不无关系,2012年开始, ...

  10. windows下route命令详解(转载)

    1.具体功能        该命令用于在本地IP路由表中显示和修改条目.使用不带参数的ROUTE可以显示帮助.            2.语法详解        route [-f] [-p] [co ...