用Python导入模块的所有方式有哪些? [英] What are all the ways to import modules in Python?

查看:130
本文介绍了用Python导入模块的所有方式有哪些?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经进行了一些研究,发现了以下文章: http://effbot .org/zone/import-confusion.htm .尽管这似乎是一个很好的指南,但它还是在1999年写的.我正在使用Python 3.4.3,因此我认为某些事情已更改,这让我感到担忧,因为我不想学习不适用的内容.因此,在Python 3中,详细导入包和模块的所有方式有哪些?哪些方法最常见,应该优先使用?

I've done some research, and I came across the following article: http://effbot.org/zone/import-confusion.htm. While this seems to be a great guide, it was written in 1999, a while back. I'm am using Python 3.4.3, so I am thinking that some things have changed, which worries me, because I don't want to learn what is not applicable. Therefore, in Python 3, what are all of the ways to import packages and modules, in detail? Which ways are the most common and should be used above others?

推荐答案

对普通用法而言唯一重要的方法是该页面上列出的前三种方法:

The only ways that matter for ordinary usage are the first three ways listed on that page:

  • import module
  • from module import this, that, tother
  • from module import *
  • import module
  • from module import this, that, tother
  • from module import *

这些在Python 3中没有改变.(有关Python查找module.py文件以加载module的位置的一些详细信息已进行了调整,但是导入本身的行为仍然可以按照页面上的描述进行您已链接.)

These haven't changed in Python 3. (Some of the details about where Python looks for the module.py file to load module have been tweaked, but the behavior of the import itself still works as described on the page you linked.)

在Python 3之前但自那篇文章以来,已添加了一项内容.那是明确的相对进口.这些使您可以执行from ..module import blah之类的操作.这种导入只能在包内部使用.它允许包中的模块以相对于包的方式引用同一包中的其他模块(即,无需指定如何导入顶级包).您可以在 PEP 328 中阅读详细信息.但是,即使这基本上只是您链接到的页面上提到的from module import blah样式语法的新变种.

One thing has been added, before Python 3 but since that article. That is explicit relative imports. These let you do things like from ..module import blah. This kind of import can only be used from inside a package; it lets modules in a package refer to other modules in the same package in a way that is relative to the package (i.e., without having to specify how to import the top-level package). You can read the details in PEP 328. Even this, though, is basically just a new variation on the from module import blah style syntax mentioned on the page you linked to.

__import__在Python 3中仍然可以使用.这是一个内部函数,只有在做一些不寻常的事情时才需要使用.这同样适用于importlib模块(和不推荐使用的imp模块)中的各种功能.这些导入功能的精确程度各不相同,但是对于我只想导入并使用该模块"的常规用法,您基本上不需要使用它们.仅当您要执行诸如动态导入名称在运行时才知道的模块之类的操作时,才需要使用它们.

__import__ also still works in Python 3. This is an internal function that you only would need to use if doing something rather unusual. The same applies to various functions in the importlib module (and the deprecated imp module). The exact level of wizardliness of these importing functions varies from one to another, but for ordinary usage of "I just want to import this module and use it", you essentially never need to use them. They're only needed if you want to do something like dynamically import a module whose name isn't known until runtime.

这篇关于用Python导入模块的所有方式有哪些?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆