xone

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

模块分为三种:

1、自定义模块

2、第三方模块

3、内置模块

不能导入一个跟内置模块同名的自定义模块。

运行一个py文件,会导入当前文件所在文件夹的路径到sys.path里。

导入模块就是到sys.path所有的路径里查找这个模块,如果有就导入成功,没有就失败。 

导入目录的本质就是执行该目录下面的__init__.py文件 

 注意:两个程序不要互相调用模块

如果文件夹下有__init__.py文件,就称这个文件夹为包,这个时候可以直接导入这个文件夹

zhou@ubuntu:~/python/01-模块$ ll TestMsg/
total 28
drwxrwxr-x 3 zhou zhou 4096 1月   9 23:00 ./
drwxr-xr-x 3 zhou zhou 4096 1月   9 23:00 ../
-rw-rw-r-- 1 zhou zhou   72 1月   9 22:41 __init__.py
-rw-r--r-- 1 zhou zhou   30 1月   9 15:26 main.py
drwxr-xr-x 2 zhou zhou 4096 1月   9 22:41 __pycache__/
-rw-r--r-- 1 zhou zhou   42 1月   9 22:16 recvmsg.py
-rw-r--r-- 1 zhou zhou   42 1月   9 22:15 sendmsg.py
zhou@ubuntu:~/python/01-模块$ cat TestMsg/__init__.py
__all__ = ["sendmsg", "recvmsg"] #控制当前包里想要被导入的代码
from . import sendmsg #如果想直接import 包名,需要加入这一行;如果用from . import * 不用加这一行
zhou@ubuntu:~/python/01-模块$ ipython3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import TestMsg

In [2]: TestMsg.sendmsg.test1()
-----test1-----

  

zhou@ubuntu:~/python/01-模块$ cat TestMsg/__init__.py
__all__ = ["sendmsg", "recvmsg"]
#from . import sendmsg #注销这一行后用import 包名,导入失败
zhou@ubuntu:~/python/01-模块$ ipython3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
Type "copyright", "credits" or "license" for more information.

IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import TestMsg

In [2]: TestMsg.sendmsg.test1()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-b0b1a37d886d> in <module>()
----> 1 TestMsg.sendmsg.test1()

AttributeError: module 'TestMsg' has no attribute 'sendmsg'

  

 

 模块的制作和发布

zhou@ubuntu:~/python/02-模块发布$ cat TestMsg/__init__.py 
__all__ = ["sendmsg", "recvmsg"]
from . import *
zhou@ubuntu:~/python/02-模块发布$ cat TestMsg/sendmsg.py 
def test1():
    print("-----test1-----")
zhou@ubuntu:~/python/02-模块发布$ cat TestMsg/recvmsg.py 
def test2():
    print("-----test2-----")

zhou@ubuntu:~/python/02-模块发布$ tree
.
├── setup.py
└── TestMsg
    ├── __init__.py
    ├── main.py
    ├── recvmsg.py
    └── sendmsg.py
zhou@ubuntu:~/python/02-模块发布$ cat setup.py
from distutils.core import setup
 
setup(name="zzm", version="1.0", description="zzm's module", author="zzm", py_modules=['TestMsg.sendmsg', 'TestMsg.recvmsg'])
#py_modules后面的列表中需指明该包中需要包含哪些模块。


#构建模块
zhou@ubuntu:~/python/02-模块发布$ 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/recvmsg.py -> build/lib/TestMsg
zhou@ubuntu:~/python/02-模块发布$ tree
.
├── build
│   └── lib
│       └── TestMsg
│           ├── __init__.py
│           ├── recvmsg.py
│           └── sendmsg.py
├── setup.py
└── TestMsg
    ├── __init__.py
    ├── main.py
    ├── recvmsg.py
    └── sendmsg.py
 
4 directories, 8 files

#生成发布压缩包
zhou@ubuntu:~/python/02-模块发布$ 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 zzm-1.0
creating zzm-1.0/TestMsg
making hard links in zzm-1.0...
hard linking setup.py -> zzm-1.0
hard linking TestMsg/__init__.py -> zzm-1.0/TestMsg
hard linking TestMsg/recvmsg.py -> zzm-1.0/TestMsg
hard linking TestMsg/sendmsg.py -> zzm-1.0/TestMsg
creating dist
Creating tar archive
removing 'zzm-1.0' (and everything under it)
zhou@ubuntu:~/python/02-模块发布$ tree
.
├── build
│   └── lib
│       └── TestMsg
│           ├── __init__.py
│           ├── recvmsg.py
│           └── sendmsg.py
├── dist
│   └── zzm-1.0.tar.gz
├── MANIFEST
├── setup.py
└── TestMsg
    ├── __init__.py
    ├── main.py
    ├── recvmsg.py
    └── sendmsg.py
 
5 directories, 10 files

#安装制作好的模块
zhou@ubuntu:~/python/02-模块发布$ cp dist/zzm-1.0.tar.gz ~
zhou@ubuntu:~$ tar xf zzm-1.0.tar.gz
zhou@ubuntu:~$ cd zzm-1.0/
zhou@ubuntu:~/zzm-1.0$ tree
.
├── build
│   └── lib
│       └── TestMsg
│           ├── __init__.py
│           ├── recvmsg.py
│           └── sendmsg.py
├── PKG-INFO
├── setup.py
└── TestMsg
    ├── __init__.py
    ├── recvmsg.py
    └── sendmsg.py
 
zhou@ubuntu:~/zzm-1.0$ cat PKG-INFO
Metadata-Version: 1.0
Name: zzm
Version: 1.0
Summary: zzm's module
Home-page: UNKNOWN
Author: zzm
Author-email: UNKNOWN
License: UNKNOWN
Description: UNKNOWN
Platform: UNKNOWN
 
zhou@ubuntu:~/zzm-1.0$ sudo python3 setup.py install
running install
running build
running build_py
running install_lib
creating /usr/local/lib/python3.6/dist-packages/TestMsg
copying build/lib/TestMsg/recvmsg.py -> /usr/local/lib/python3.6/dist-packages/TestMsg
copying build/lib/TestMsg/sendmsg.py -> /usr/local/lib/python3.6/dist-packages/TestMsg
copying build/lib/TestMsg/__init__.py -> /usr/local/lib/python3.6/dist-packages/TestMsg
byte-compiling /usr/local/lib/python3.6/dist-packages/TestMsg/recvmsg.py to recvmsg.cpython-36.pyc
byte-compiling /usr/local/lib/python3.6/dist-packages/TestMsg/sendmsg.py to sendmsg.cpython-36.pyc
byte-compiling /usr/local/lib/python3.6/dist-packages/TestMsg/__init__.py to __init__.cpython-36.pyc
running install_egg_info
Writing /usr/local/lib/python3.6/dist-packages/zzm-1.0.egg-info

#测试安装的模块
zhou@ubuntu:~/zzm-1.0$ cd
zhou@ubuntu:~$ ipython3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
Type "copyright", "credits" or "license" for more information.
 
IPython 5.5.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
 
In [1]: import TestMsg
 
In [2]: TestMsg.sendmsg.test1()
-----test1-----

  

在程序中重新导入模块  

In [5]: import TestMsg

In [6]: from imp import *

In [7]: reload(TestMsg)
Out[7]: <module 'TestMsg' from '/home/zhou/python/01-模块/TestMsg/__init__.py'>

  

  

  

 

 

 

 

from  xxx import xxx

可以导入,函数,类,

实例: 

目录结构

account.py

def login():
    print('login')

def logout():
    print('logout')

def register():
    print('register')

导入方式一:

index.py

from lib import account

account.login()

以上代码执行结果

login

导入方式二:

index.py

from lib.account import logout

logout()

以上代码执行结果

logout

导入方式三:

index.py

from lib.account import logout as aaa
aaa()

以上代码执行结果

logout

导入方式四:(不推荐)

index.py

import lib.account

lib.account.login()

以上代码执行结果

login

导入目录:

目录结构:

__init__.py

def f1():
    return 'F1'

test.py

from src import plugins

ret = plugins.f1()
print(ret)

以上代码执行结果

F1

添加sys.path路径

添加当前文件的上级目录路径到sys.path

import os,sys

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

拼接路径

import os,sys

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
print(BASE_DIR)
print(os.path.join(BASE_DIR,'bin1','test.py'))

以上代码执行结果

F:\zhou\python\day1
F:\zhou\python\day1\bin1\test.py

__name__ 是当前模块名,当模块被直接运行时模块名为 __main__ 。

if __name__ == '__main__'
    xxxx

以上代码的意思是,当模块被直接运行时,代码块xxxx将被运行,当模块是被导入时,代码块不被运行。

 

posted on 2017-05-02 17:59  周小百  阅读(395)  评论(0编辑  收藏  举报