[Python Modules]

 1. a module is a python source file. 

 2. a package is a directory with a __init__.py file within it.

  1) 如果目录下没有__init__.py文件, 则python不会认为这个目录是个package, 这意味着 import package 以及 from package import * 以及 from package import xxx 都会失败.

  2) 无论是import package还是from package import *都会执行__init__.py, 不同的是, 当执行from package import *时, python会自动import __all__中的文件.

  3)__all__对于单一的Module同样有效

 3. import哪个符号, 那么该符号就会加入当前作用域中.
 4. from module import *,不会导入以 "_"(单下划线开头的名字),“__”(双下划线)的name mangling只在Class的命名空间中才有

  

  虽然import *不会导入"_"前缀的名字,但是import module的话仍旧可以访问到,所以在一个module中,不能以“_”的手段来定义私有变量

参考:

1、http://www.blogjava.net/cpegtop/articles/383806.html

Importing * From a Package

  If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py.

  from xxx import * 不会导入xxx包下的所有submodule,除非该submodule在__init__.py中会被引用到。

Intra-package References

  

Python Modules的更多相关文章

  1. 解决yum升级的问题“There was a problem importing one of the Python modules”

    yum命令升级的时候,报出这个错误. There was a problem importing one of the Python modules required to run yum. The ...

  2. Python Modules and Packages – An Introduction

    This article explores Python modules and Python packages, two mechanisms that facilitate modular pro ...

  3. Installing Python Modules

    Email: distutils-sig@python.org As a popular open source development project, Python has an active s ...

  4. python modules and packages

    https://realpython.com/python-modules-packages/ 在软件开发中,一个module是具有一些相关功能的软件集合,比如,当你在开发一个游戏时,可能会有一个模块 ...

  5. [Python] Reuse Code in Multiple Projects with Python Modules

    A module is a function extracted to a file. This allows you to import the function and use it in any ...

  6. [Python Modules] unittest.mock

    五夜光寒,照来积雪平于栈.西风何限,自起披衣看. 对此茫茫,不觉成长叹.何时旦,晓星欲散,飞起平沙雁. 在某个Python程序中看到这么一行 from unittest import mock 看起来 ...

  7. centos系统重装python或yum 报There was a problem importing one of the Python modules required to run yum. The error leading to this problem was:错误

    sudo vim /usr/bin/yum #修个python所在的路径,例如 #/usr/local/bin/python2.6 或 /usr/local/bin/python2.7要原本你的系统原 ...

  8. 升级 python 2.6.6 系统到 2.7.10 版本

    CentOS 6 系统默认 Python 版本是:2.6.6 平时在使用中遇到很多的库要求是 2.7.x 版本的库,比如使用 ConfigParser 库,在 2.6 版本库就不支持没有 value ...

  9. K-Means clusternig example with Python and Scikit-learn(推荐)

    https://www.pythonprogramming.net/flat-clustering-machine-learning-python-scikit-learn/ Unsupervised ...

随机推荐

  1. Nginx实践01-ngnix编译安装-测试

    1.下载nginx安装包 下载地址:http://nginx.org/en/download.html(里面有nginx各个版本) 解压到指定目录: 解压出来的目录简单介绍: src:软件的所有源代码 ...

  2. Struts2基本使用(三)--数据交互

    Struts2中的数据交互 在Struts2中我们不必再使用request.getParameter()这种方式来获取前台发送到服务器的参数. 我们可以在服务器端的Java类中直接声明一个和前台发送数 ...

  3. NodeJS反序列化漏洞利用

    原文来自:http://www.4hou.com/web/13024.html node.js是一个服务器端的运行环境,封装了Google V8引擎,V8引擎执行JavaScript速度非常快,性能非 ...

  4. centos 搭建 docker sentry

    1.安装 http://blog.51cto.com/linuxg/2052927 2 .实际调试: 项目: <title>hghh</title> <script sr ...

  5. 二叉树的前序、中序、后序遍历 python

    话不多说,直接上代码 class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None cl ...

  6. IOS layoutSubviews总结

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit - (void)layoutSubviews - (voi ...

  7. MongoDB数据库的特点以及结构

    mongodb标签:非关系型数据库       文档型数据库           最像关系型的非关系型数据库 特点: 1. 由c++编写的数据库管理系统 2. 支持丰富的增删改查功能 3. 支持丰富的 ...

  8. 使用TortoiseGit+码云管理项目代码

    1.下载安装msysgit. 2.下载安装tortoisegit. 3.创建ssh密钥. 开始–所有程序–TortoiseGit–PuTTYgen 生成方法:点击“Generate”后,鼠标在key下 ...

  9. Oracle:"ORA-00942: 表或视图不存在"

    情景 项目中使用Powerdesigner设计数据结构,在Powerdesigner中数据表和字段都区分了大小写,并生成了Oracle表,在执行Sql脚本时遇到以下问题:“ORA-00942: 表或视 ...

  10. ringojs 基于jvm 的javascript 平台试用

    ringojs 是一个基于jvm 的javascript 平台,支持commonjs 模块模式 安装 下载包配置环境变量,或者使用docker,测试使用docker dockerfile deb 包安 ...