【python | linux11】一篇文章让你完全理解什么是模块引用

101 阅读3分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第28天,点击查看活动详情

🍁作者简介:🏅云计算领域优质创作者🏅新星计划第三季python赛道TOP1🏅 阿里云ACE认证高级工程师🏅
✒️个人主页:小鹏linux
💊个人社区:小鹏linux(个人社区)欢迎您的加入!

1.模块介绍

在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码会越来越长,越来越不容易维护,为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多程序语言都采用这种组织代码的方式。在python中,一个.py文件就称之为一个模块(module)

2.使用模块的好处

最大的好处是大大提高了代码的可维护性。其次,编写代码不必从零开始。当一个模块编写完毕,就可以被其他地方引用。我们在编写程序的时候,也常常引用其他模块,包括Python内置的模块和来自第三方模块

3.查看模块位置

模块名。file

In [1]: import osIn [2]: os.file                #查看os模块所在位置Out[2]: '/usr/local/lib/python3.7/os.py'

4.模块的意义

模块好比是工具包,想要使用这个工具包中的工具(就好比函数),就需要导入这个模块。
[root@localhost ~]# vim sendmsg.py
def test1():
    print("----sendmsg---test1----")
def test2():
    print("----sendmsg---test2----")

[root@localhost ~]# vim main.py
import sendmsg
#使用模块
sendmsg.test1()
sendmsg.test2()

[root@localhost ~]# python3 main.py
----sendmsg---test1----
----sendmsg---test2----

5.模块的引用

5.1模块名.函数名

In [1]: import math  
In [2]: math.sqrt(9)
Out[2]: 3.0
In [7]: import sendmsg
In [8]: sendmsg.test1()
----sendmsg---test1----

5.2引入方式的改变

有时候我们只需要用到模块中的某个函数,只需要引入该函数即可,此时可以用下面的方法实现:from 模块名 import 函数名1,函数名2
[root@localhost ~]# vim sendmsg.py
def test1():
    print("----sendmsg---test1----")
def test2():
    print("----sendmsg---test2----")

[root@localhost ~]# vim main.py
from sendmsg import test1
from sendmsg import test2   #或者写from sendmsg import test1,test2 再或者用*代替test,test2
test1()
test2()

[root@localhost ~]# python3 main.py
----sendmsg---test1----
----sendmsg---test2----

5.3导入某个模块的函数

语法如下:from modname import name1[,name2[, ...naneN]]使用as起别名
In [1]: import random
In [2]: random.randint(0,10)
Out[2]: 5

In [4]: import random as r
In [5]: r.randint(0,10)
Out[5]: 7

5.4系统加载模块位置(加载顺序从上到下)

In [10]: import sys
In [11]: sys.path
Out[11]:
['/usr/local/bin''/usr/local/lib/python37.zip''/usr/local/lib/python3.7''/usr/local/lib/python3.7/lib-dynload''''/usr/local/lib/python3.7/site-packages''/usr/local/lib/python3.7/site-packages/IPython/extensions''/root/.ipython']

5.5在任何位置识别

[root@localhost ~]# vim sendmsg.py
#全局变量name="张三"def test1():        print("----sendmsg---test1----")def test2():        print("----sendmsg---test2----")class Person(object):        #类属性        age=18        __address="中国"        def init(self,name):                self.name=name        def eat(self):                print("%s在吃东西"%self.name)if name=='main':        print(name)        test1()        test2()        p=Person("老王")        print(p.age)        print(p.name)        p.eat()
[root@localhost ~]# python3 sendmsg.py张三----sendmsg---test1--------sendmsg---test2----18老王老王在吃东西
[root@localhost ~]# cp sendmsg.py  /usr/local/lib/python3.7/site-packages
[root@localhost ~]# cd /
[root@localhost ~]# ipython
In [1]: import sendmsgIn [2]: sendmsg.nameOut[2]: '张三'In [3]: sendmsg.test1Out[3]: <function sendmsg.test1()>In [4]: sendmsg.test1()----sendmsg---test1----In [5]: p=sendmsg.Person("老李")In [6]: p.eat()老李在吃东西In [7]: sendmsg.__file__Out[7]: '/usr/local/lib/python3.7/site-packages/sendmsg.py'

5.6 all变量(_ all _)

如果一个文件中有_ all _变量,那么也就意味着这个变量中的元素,会被from xxx import *时导入

当有些功能你不想让别人用的时候就可以使用这种方式。

[root@localhost site-packages]# rm -rf ./sendmsg.py[root@localhost site-packages]# cd ~[root@localhost ~]# vim sendmsg.py
all=['name','test1','Person']#全局变量name="张三"def test1():        print("----sendmsg---test1----")def test2():        print("----sendmsg---test2----")class Person(object):        #类属性        age=18        __address="中国"        def init(self,name):                self.name=name        def eat(self):                print("%s在吃东西"%self.name)if name=='main':        print(name)        test1()        test2()        p=Person("老王")        print(p.age)        print(p.name)        p.eat()
[root@localhost ~]# vim main.py
from sendmsg import *print(name)test1()p = Person("李四")p.eat()test2()
[root@localhost ~]# python3 main.py张三----sendmsg---test1----李四在吃东西
In [1]: from sendmsg import *In [2]: test1()----sendmsg---test1----In [3]: test2()---------------------------------------------------------------------------NameError                                 Traceback (most recent call last) in ----> 1 test2()NameError: name 'test2' is not definedIn [4]: import sendmsgIn [5]: sendmsg.test2()----sendmsg---test2----

👑👑👑结束语👑👑👑