python中的关键字import
来源:互联网 发布:ubuntu查看cpu详情 编辑:程序博客网 时间:2024/05/18 06:21
先看文件结构
[mytest]$ tree
.
|-- a.py
|-- b.py
`-- newtest
|-- c.py
`-- d.py
[mytest]$ cat a.py
def fun1():
print 'fun1 in a.py'
如果想在b.py中调用a.py中的函数func1(),可以这么做
[mytest]$ cat b.py
import a
a.fun1()
如果不想每次调用函数时都加上调用模块的名字(如本例中的'a'),可以这么做
[mytest]$ cat b.py
from a import fun1
fun1()
如果我们想再b.py中调用文件夹‘newtest’下得文件'c.py',如果向下面这么写,导入不会成功
[mytest]$ cat b.py
import newtest.c
[mytest]$ python b.py
Traceback (most recent call last):
File "b.py", line 1, in ?
import newtest.c
ImportError: No module named newtest.c
错误的原因是python要求每个文件夹下必须有__init__.py文件(即使里面为空),才能被当做一个package处理。这样我们在newtest文件夹下面增加一个文件名为__init__.py的空文件就可以了。另外,我们可以在__init__.py文件中对变量__all__进行赋值,在这个(__all__)list里面的文件才能被外部调用。
0 0
- python中的关键字import
- python中的import详解
- Python中的import
- python中的import机制
- python中的import介绍
- Python中的 "import" 和 "from ... import"
- python中的import 和 from .. import ..
- python中的import和from import
- 【Python】关键字import与super介绍
- Python 包、模块 概念 from 、import 关键字
- java中的关键字package和import
- python 中的相互import问题
- python中的import和from
- 关于Python中的import(一)
- 关于Python中的import(二)
- 关于Python中的import(一)
- 关于Python中的import(三)
- python中的python from __future__ import division
- hadoop笔记
- CreateDIBSection
- 程序的内存分配
- 如何获得设备环境句柄(HDC)--Win32 SDK
- hadoo安装日志
- python中的关键字import
- DECLARE_MESSAGE_MAP()说明
- 互斥量
- DB2数据库静态参数修改(归档模式和日志组数,都需要重启db)
- properties文件使用 spring(web项目)
- 好书推荐
- Genymotion Emulator:x86架構最快速的Android模擬器,加快Android程式開
- HPUX磁带管理命令及常见应用
- Web应用检查清单