Python 3.3 Tutorial Notes - 4:Modules

本文深入探讨Python中模块的导入机制,包括导入位置、执行时机、使用语法、避免常见错误及模块搜索路径等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. File name is the module name
2. where you call the "import", where the module will be put into the destination file. This means, if a module contains some code out of any functions, it will be executed in the destination file where it is imported. The execution happens only at the first "import".
3. from <lib> import <method1_name [, method2_name, ...]>
4. from lib import *, it will import every names except those beginning with an underscore(_)
5. "from lib import *" should be avoid except in interactive mode.
6. check __name__ if "__main__" to know if current model is main or imported by others.
7. Script parameter: sys.argv[1] (the first parameter, 0 is the command line itself)
8. Module search path (in below order)
     8.1 build-in modules
     8.2 path defined in sys.path (need import sys)
9. sys.path is initialized from these locations:
? the directory containing the input script (or the current directory).
? PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
? the installation-dependent default.
10.*.pyc and *.pyo, byte code
     10.0 The module   compileall  can create .pyc files (or .pyo files when -O is used) for all modules in a directory.  http://docs.python.org/3.3/library/compileall.html 
                python -m compileall <path>
     10.1 loading will be faster, but not running
     10.2 -O and -OO: optimization
     10.3 it is ok to have only *.pyc or *.pyo but no *.py. This is good to avoid reverse engineer.
11. The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings
     11.1 dir() does not list the names of built-in functions and variables. If you want a list of those, they are defined in the standard module   builtins
12. Package
     12.1 The   __init__.py  files are required to make Python treat the directories as containing packages
     12.2 __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable
     12.3 "from <package name> import <item>" 
     12.4 Define the list "__all__" in "__init__.py". It is the list of submodules should be imported by "from *". Then "from <package_name> import *" will only import the name defined in "__all__".
     12.5 If "__all__" is not defined, "from <package_name> import *" only import <package name> without sub-modules. 
     12.6 "from Package import *" is bad practice. "from Package import specific_submodule" is recommended.
     12.7 Relative import: leading dots to indicate the current and parent packages involved in the relative import: from . import echo
     12.8 "__path__": a list containing the name of the directory holding the package’s __init__.py. Not often needed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值