Python之import

1  Import

When program grows bigger, it‘s good to break it into different modules. A module is a file containing Python definitions and statements. Python modules have a filename and end with the extension .py.

Definitions inside a module can be imported to another module or the interactive interpreter in Python. We use the import keyword. For example, we can import the math module by typing in import math

>>> import math
>>> math.pi
3.141592653589793

Now all the definitions inside math module are available. We can also import some specific attributes and functions only, using the from keyword.
  For example:

>>> from math import pi
>>> pi
3.141592653589793
   While importing a module, Python looks at several places defined in sys.path. It is a list of directory locations.
>>> import sys
>>> sys.path
[‘‘,
 ‘C:\\Python33\\Lib\\idlelib‘,
 ‘C:\\Windows\\system32\\python33.zip‘,
 ‘C:\\Python33\\DLLs‘,
 ‘C:\\Python33\\lib‘,
 ‘C:\\Python33‘,
 ‘C:\\Python33\\lib\\site-packages‘]
 We can add our own location to this list as well.

2  Ways to Import a Module

 Python provides at least three different ways to import modules.

  • import X  imports the module X, and creates a reference to that module in the current namespace. In other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.
  • from X import *  imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). In other words, after you’ve run this statement, you can simply use a plain nameto refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.
  • from X import a, b, c  imports the module X, and creates references in the current namespace to the given objects. In other words, you can now use a and b and c in your program.
  • Finally, X = __import__(‘X’) works like import X, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.
 
时间: 2024-08-02 11:00:32

Python之import的相关文章

python的import与from...import的不同之处

在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,我们就可以使用了.这类似于C语言中的include头文件,Python中我们用import导入我们需要的模块. import sys print('================Python import mode=========================='); print ('The

python中import的机制与实现

原文出处: 刘畅(@你猜我猜不猜猜你是谁) 概述 Python 是一门优美简单.功能强大的动态语言.在刚刚接触这门语言时,我们会被其优美的格式.简洁的语法和无穷无尽的类库所震撼.在真正的将python应用到实际的项目中,你会遇到一些无法避免的问题.最让人困惑不解的问题有二类,一个 编码问题,另一个则是引用问题. 本文主要讨论关于Python中import的机制与实现.以及介绍一些有意思的Python Hooks. Python 类库引入机制 首先,看一个简单的例子: """

python 的import机制2

http://blog.csdn.net/sirodeng/article/details/17095591 python 的import机制,以备忘: python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用.   如果想使用非当前模块中的代码,需要使用Import,这个大家都知道. 如果你要使用的模块(py文件)和当前模块在同一目录,只要import相应的文件

python之import引用

关于python的import引用的最大关键是init.py文件的作用,这个文件对于import的方法使用至关重要. 这个是我在搭建自动化框架过程中用到的import的方法使用. 比如说,我现在login.py想引用bottom底下的log.py的时候,这个时候,我们如何引用呢? from bottom import log 又比如说,我现在想在test文件中引用login.py那这个时候如何引用? from case.login import login 又比如说,我想在test.py中引用l

python的import与from…import的区别

[转]http://blog.csdn.net/windone0109/article/details/8996184 在python中用import或者from-import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,就可以使用了.这类似于C语言中的include头文件. import sys print('======================Python import mode====

python中import或from...   import用法

在python用import或from...import来导入相应的模块. 模块是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,我们就可以使用了.例如: from common import * import util import miniCurl import os os.getcwd()

python动态import某个文件夹下的模块

因为有  "用户上传脚本,动态运行"的需求,所以要动态地import用户上传的模块 所以写了个函数动态地import # -*- coding: utf-8 -*- import os import sys import os.path import Queue import commands def test(rootDir): #判断传入的路径下是否有"__init__.py"这个文件了,如果没有则创建,否则import会认为没有这个moudle if os.p

python的import与from...import的不同之处(转载)

在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,我们就可以使用了.这类似于C语言中的include头文件,Python中我们用import导入我们需要的模块. eg: import sysprint('================Python import mode==========================');print ('T

python中import和from...import区别

在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中,我们就可以使用了.这类似于C语言中的include头文件,Python中我们用import导入我们需要的模块. eg: import sysprint('================Python import mode==========================');print ('T

Python如何import文件夹下的文件

Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里的文件,PHP中只需要给对路径就OK.Python中则不同,下面来看看这个例子. 目录结构: a.py 要 import dir目录下的 b.py 文件.a.py代码如下: 1 2 3 4 5 6 # coding=utf-8 "import dir 目录下的 b.py 文件" impor