广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python import 机制
  • 424
分享到

python import 机制

机制pythonimport 2023-01-31 01:01:04 424人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

python 环境初始化过程中就会将sys module加载到内存中,但是为了local空间更干净,需要用户亲自导入,通知Python.module对象实际上是一个dict在维护着,hello.__dict__打印出属性和属性值


python 环境初始化过程中就会将sys module加载到内存中,但是为了local空间更干净,需要用户亲自导入,通知Python.module对象实际上是一个dict在维护着,hello.__dict__打印出属性和属性值,hello.__builtins__其实就是__builtins__.__dict__,
 

  1. >>> type(__builtins__) 
  2. <type 'module'> 
  3. >>> type(hello.__builtins__) 
  4. <type 'dict'> 
  5. liaoxinxi@tbase /home/liaoxinxi/start $ ls 
  6. hello.py hello.pyc world.py world.pyc 
  7. liaoxinxi@tbase /home/liaoxinxi/start $ touch __init__.py 


嵌套导入
只会影响到自己的本地空间,所有的import操作,不管在什么地方,时间都会影响到全局module集合,这样做的话就是一次导入,其他地方就不用导入啦

  1. >>> import world 
  2. >>> dir(world) 
  3. ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'hello', 'sys'] 
  4. >>> dir(world.hello) 
  5. ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'b'] 
  6. >>> import sys 
  7. >>> sys.modules['hello'] 
  8. <module 'hello' from 'hello.pyc'> 
  9. >>> id(world.hello) 
  10. 3075426604L 
  11. >>> id(sys.modules['hello']) 
  12. 3075426604L 
  13. >>> import hello 
  14. >>> id(hello) 
  15. 3075426604L 



package机制
在module的基础上python增加了package的机制,如果说module是文件的话,那package就是文件夹机制,必须在文件夹有__init__.py文件存在,在导入start.hello后并在sys.modules导入start下面的其他模块,这和module有点不一样,dir(start)的时候已经有了__path__属性,这个属性就是告诉接下来的导入如import start.world,只会在start的路径下找了,就快了很多

  1. >>> import start.hello 
  2. >>> dir() 
  3. ['__builtins__', '__doc__', '__name__', '__package__', 'start'] 
  4. >>> dir(start) 
  5. ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'hello'] 
  6. >>> sys.modules['start.world'] 
  7. Traceback (most recent call last): 
  8. File "<stdin>", line 1, in <module> 
  9. NameError: name 'sys' is not defined 
  10. >>> import sys 
  11. >>> sys.modules['start.world'] 
  12. Traceback (most recent call last): 
  13. File "<stdin>", line 1, in <module> 
  14. KeyError: 'start.world' 
  15. >>> sys.modules['start.hello'] 
  16. <module 'start.hello' from 'start/hello.pyc'> 
  17. >>> id(start) 
  18. 3075320012L 
  19. >>> id(start.hello) 
  20. 3075320108L 
  21. >>> id(sys.modules['start.hello']) 
  22. 3075320108L 



from和imort关系
from start import hello,其实本质都是是一样的,都是导入了start.hello,只是在local空间加入的符号不一样,from是加入hello,对应着start.hello,而import start.hello则是加入start符号

  1. >>> dir() 
  2. ['__builtins__', '__doc__', '__name__', '__package__'] 
  3. >>> from start import hello 
  4. >>> dir() 
  5. ['__builtins__', '__doc__', '__name__', '__package__', 'hello'] 
  6. >>> import sys 
  7. >>> sys.modules['hello'] 
  8. Traceback (most recent call last): 
  9. File "<stdin>", line 1, in <module> 
  10. KeyError: 'hello' 
  11. >>> sys.modules['start.hello'] 
  12. <module 'start.hello' from 'start/hello.pyc'> 
  13. >>> sys.modules['start'] 
  14. <module 'start' from 'start/__init__.pyc'> 



精确导入
精确控制某个package下的module的某个属性的导入,在sys.modules下可以看到start,和start.hello到已经存在,只是在local空间,只能用到符号a,再仔细看看,其实hello也在当前空间中可用,也就是说sys.modules下存的就是module(包括module和package,不包括module的具体属性),dir()输出的当前空间

  1. >>> from start.hello import a 
  2. >>> a 
  3. >>> start.hello.b 
  4. Traceback (most recent call last): 
  5. File "<stdin>", line 1, in <module> 
  6. NameError: name 'start' is not defined 
  7. >>> dir() 
  8. ['__builtins__', '__doc__', '__name__', '__package__', 'a', 'hello', 'sys'] 
  9. >>> hello.b 
  10. >>> sys.modules['start'] 
  11. <module 'start' from 'start/__init__.pyc'> 
  12. >>> sys.modules['start.hello'] 
  13. <module 'start.hello' from 'start/hello.pyc'> 
  14. >>> sys.modules['hello'] 
  15. Traceback (most recent call last): 
  16. File "<stdin>", line 1, in <module> 
  17. KeyError: 'hello' 
  18. >>> sys.modules['a'] 
  19. Traceback (most recent call last): 
  20. File "<stdin>", line 1, in <module> 
  21. KeyError: 'a' 


符号重命名
没有将start加入到当前空间中,但是sys.module有,这和没重命名是不一样的,没有重命名的话,符号空间中是有start的。

  1. >>> import start.hello as hello 
  2. >>> dir() 
  3. ['__builtins__', '__doc__', '__name__', '__package__', 'hello'] 
  4. >>> sys.modules['start.hello'] 
  5. Traceback (most recent call last): 
  6. File "<stdin>", line 1, in <module> 
  7. NameError: name 'sys' is not defined 
  8. >>> import sys 
  9. >>> sys.modules['start.hello'] 
  10. <module 'start.hello' from 'start/hello.pyc'> 
  11. >>> sys.modules['hello'] 
  12. Traceback (most recent call last): 
  13. File "<stdin>", line 1, in <module> 
  14. KeyError: 'hello' 



总结:import 机制的实现:

维护一个全家的module pool
解析和搜索module路径的书状结构
对不同文件格式的动态加载机制
归根到底就是import x.y.z,而from,as只会改变当前的命名空间,import x.y.z会将x,x.y,x.y,z导入sys.modules,而命名空间只有x.y.z
另外import * from a package,并不会导入package下面的模块,除非在__init__.py添加了__all__=[“hello”,“world”],这样就会导入hello和world

 

 

--结束END--

本文标题: python import 机制

本文链接: https://www.lsjlt.com/news/185089.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python import 机制
    Python 环境初始化过程中就会将sys module加载到内存中,但是为了local空间更干净,需要用户亲自导入,通知python.module对象实际上是一个dict在维护着,hello.__dict__打印出属性和属性值...
    99+
    2023-01-31
    机制 python import
  • Python中import机制详解
    Python语言中import的使用很简单,直接使用 import module_name 语句导入即可。这里我主要写一下"import"的本质。 Python官方 定义:Python code in on...
    99+
    2022-06-04
    详解 机制 Python
  • Python入门基础之import机制
    目录 一、前言 1.1 什么是 import 机制? 1.2 import 是如何执行的? 二、import 机制概览 三、import 勾子(import hooks) ...
    99+
    2022-11-11
  • python的import机制如何实现
    本篇内容主要讲解“python的import机制如何实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python的import机制如何实现”吧!import 机制功能Python 的 impo...
    99+
    2023-06-30
  • python的import 机制是怎么实现的
    目录import 机制功能单模块导入级联导入from & importimport & asfrom & import & as与module对象有关...
    99+
    2022-11-11
  • Python import
    python 的import是需要将项目目录加到python的环境变量里面这样才能用项目的相对路径来导入文件要运行文件加入import os import sys BASEDIR = os.path.dirname(os.path.absp...
    99+
    2023-01-31
    Python import
  • Python的import 机制中如何实现远程导入模块
    本篇文章为大家展示了Python的import 机制中如何实现远程导入模块,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。所谓的模块导入,是指在一个模块中使用另一个模块的代码的操作,它有利于代码的复用...
    99+
    2023-06-02
  • python import MySQLd
    警告:Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated  from sets impor...
    99+
    2023-01-31
    python import MySQLd
  • Python--import---语法-
    --import指令前文提到 import 指令是用来载入 module 的,如果需要,也会顺道做编译的事。但 import 指令,还会做一件重要的事情就是把 import 的那个 module 的代码执行一遍,这件事情很重要。Python...
    99+
    2023-01-31
    语法 Python import
  • python import 与 from
    在python用import或者from...import来导入相应的模块。模块其实就一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,我们就可以使用了。pycharm 工具#...
    99+
    2023-01-31
    python import
  • python import media模
    media模块不是系统的标准模块,要单独下载的! 本人python版本2.72 下面说一下我的下载和安装步骤: 下载:Python Imaging Library 1.1.7 for Python 2.7-->安装 下载pyg...
    99+
    2023-01-31
    python import media
  • Python import 【总结】
    Python import总结可能网上很多文章或博客都没解释清楚,作者自己也苦心于Python的import。至此,把自己的总结的分享给大家,本文不做基础讲解,仅说明疑惑的地方。新版本的Pycharm 2017.1.1,对自己定义的模块都有...
    99+
    2023-01-31
    Python import
  • linux7中python Import
    linux7中python ImportError: No module named pymc 处理方法 系统环境 #cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core)...
    99+
    2023-01-31
    python Import
  • python中的import
    python中的import操作有点类似于C语言中的#include,但又有很大的不同。在c语言中,#include是编译前将另一个文件包含进当前文件中。在python中导入并非只把一个文件文本插入另一个文件而已。导入其实是运行时的运算,程...
    99+
    2023-01-31
    python import
  • python中import和from-import的区别解析
    目录. import导入模块的路径两种方式. import 模块名 和 from 模块名 import * 是不同的;.重新导入模块的方法import和from-import的显著区...
    99+
    2022-12-08
    python中import和from-import的区别 python中import from-import
  • python import的用法
    1、dir(keywork)报错>>> dir(keyword) Traceback (most recent call last):   File "<stdin>", line 1, in <modu...
    99+
    2023-01-31
    python import
  • python中的import,reloa
    import 作用: 导入/引入一个python标准模块,其中包括.py文件、带有__init__.py文件的目录。e.g:[python] view plaincopy import module_name[,module1,...]  ...
    99+
    2023-01-31
    python import reloa
  • Python import同级modul
        看了一段Python的基础视频,正好赶上单位需要做个小工具。索性拿它练练手,刚刚开动就遇到一个新的问题:目录引用。简单的说,就是将不同的功能代码,分到不同的目录文件中,代码中涉及到同级目录调用,问题就出来了“SystemError:...
    99+
    2023-01-31
    Python import modul
  • python中import和from-import的区别是什么
    本文小编为大家详细介绍“python中import和from-import的区别是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“python中import和from-import的区别是什么”文章能帮助大家解决疑惑,下面跟着小编的思路...
    99+
    2023-07-04
  • Python import与from import使用和区别解读
    系统自带模块(库) ```cpp import re target = 'abc1234xyz' re.search('(\d+)', target) 但有时候,你可能会...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作