VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • python模块

python模块是一个py文件,一个模块只会被导入一次

 

python在编译或安装的时候会确定搜索路径,使用import语句的时候,python解释器就从搜索路径(即一系列目录名)中查找模块

import sys
print(sys.argv)  # 命令行参数
print(sys.path)  # 路径

结果为:

['D:/Pycharm/pythonProject/main.py']
['D:\\Pycharm\\pythonProject', 'D:\\Pycharm\\pythonProject', 'C:\\Program Files\\PerkinElmerInformatics\\ChemOffice2020\\ChemScript\\Lib', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\DLLs', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39', 'D:\\Pycharm\\pythonProject\\venv', 'D:\\Pycharm\\pythonProject\\venv\\lib\\site-packages', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib', 'C:\\Users\\86158\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']

 

import

from … import *

 

可以使用模块名称.函数名来访问函数

例如我的myFunction.py中定义了max()函数,那么可以通过myFunction.max()来使用max()函数

 

__name__属性

每个模块都有一个__name__属性,当其值是'__main__'时,表明该模块自身在运行:

if __name__ == '__main__':
   print('程序自身在运行')
else:
   print('我来自另一模块')

如果程序自身运行,会输出'程序自身在运行',如果被导入其他模块,会输出'我来自另一模块'

 

dir()函数可以找到模块内定义的所有名称,以一个字符串列表的形式返回:

复制代码
print(dir())  # 不加参数,罗列出当前定义的所有名称
a = 1
print(dir())
import sys
print(dir(sys))
print(dir())
复制代码

结果为:

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a']
['__breakpointhook__', '__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '__unraisablehook__', '_base_executable', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_framework', '_getframe', '_git', '_home', '_xoptions', 'addaudithook', 'api_version', 'argv', 'audit', 'base_exec_prefix', 'base_prefix', 'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth', 'getallocatedblocks', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'platlibdir', 'prefix', 'pycache_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'unraisablehook', 'version', 'version_info', 'warnoptions', 'winver']
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'sys']

 

python本身带有一些标准的模块库,有些模块直接被构建在解析器里。模块 sys ,它内置在每一个 Python 解析器中

 

包是一种管理 Python 模块命名空间的形式,本质上就是一个文件夹,目录只有包含一个叫做 __init__.py 的文件才会被认作是一个包

导入包中的模块的方式和上面一样

注意如果使用from package import *时,会把__init__.py中的一个叫做__all__的列表变量中的所有名字导入(例如:__all__ = ['echo', 'surround', 'reverse']),如果__all__没有定义,那么就不会导入任何子模块,而是导入package中定义的所有内容


出处:https://www.cnblogs.com/daxiangcai/p/16168027.html


相关教程