当前位置: 首页 > wzjs >正文

淘客网站推广免备案福州到泉州

淘客网站推广免备案,福州到泉州,新手用什么框架做网站比较好,路由器带u盘接口的做网站文章目录 1. importlib 库 概述2. 导入模块(import_module())2.1. 导入已安装的模块2.2. 导入子模块2.3 通过字符串变量导入模块 3. 重新加载模块(reload())4. 检查模块是否存在(find_spec)5. 获取模块路径…

文章目录

  • 1. importlib 库 概述
  • 2. 导入模块(import_module())
    • 2.1. 导入已安装的模块
    • 2.2. 导入子模块
    • 2.3 通过字符串变量导入模块
  • 3. 重新加载模块(reload())
  • 4. 检查模块是否存在(find_spec)
  • 5. 获取模块路径(find_spec().origin)
  • 6. 加载 .py 文件为模块(spec_from_file_location)
  • 7. 读取模块资源(importlib.resources)
  • 8. 获取模块元数据(importlib.metadata)

1. importlib 库 概述

importlib 是 Python 的动态导入模块,用于运行时加载模块、重新加载模块、自定义模块导入,适用于 插件系统、动态代码执行等场景。
importlib 主要功能
动态导入模块(import_module)
重新加载已导入的模块(reload)
加载 .py 文件为模块(spec_from_file_location)
检查模块是否存在 util.find_spec()
获取模块路径 util.find_spec().origin

importlib.util.spec_from_loader(name, loader, *, origin=None):
这个函数用于创建一个模块规范对象,基于给定的模块名称、加载器对象和可选的模块来源。它可以用于动态加载由自定义加载器提供的模块。
官方:
https://docs.python.org/zh-cn/3/library/importlib.html
参考:
https://blog.csdn.net/u013172930/article/details/146214799
typing.cast:
https://blog.csdn.net/engchina/article/details/144367018

2. 导入模块(import_module())

2.1. 导入已安装的模块

import importlib# 运行时导入模块
math_module = importlib.import_module("math")# 调用模块中的函数
print(math_module.sqrt(16)) # 4.0

等价于

import math
print(math.sqrt(16))        # 4.0

import_module(“math”) 在运行时导入 math。

2.2. 导入子模块

import importlibjson_encoder = importlib.import_module("json.encoder")
print(json_encoder.JSONEncoder)  # <class 'json.encoder.JSONEncoder'>

导入 json.encoder,无需 import json。
等价于

import json
print(json.encoder.JSONEncoder)  # <class 'json.encoder.JSONEncoder'>

2.3 通过字符串变量导入模块

import importlibmodule_name = "random"
random_module = importlib.import_module(module_name)
print(random_module.randint(1, 10)) # 生成随机数

等价于

import random
print(random.randint(0, 10))        # 生成随机数

可用于插件系统,动态加载模块。

3. 重新加载模块(reload())

import importlib
import mymodule  # 假设 `mymodule.py` 已导入importlib.reload(mymodule)  # 重新加载模块

reload() 用于修改代码后立即生效(适用于开发环境)。

4. 检查模块是否存在(find_spec)

importlib.util.find_spec(name, package=None):
这个函数用于查找指定名称的模块规范对象。它会搜索 sys.path 中的目录和 zip 文件,返回一个 ModuleSpec 对象,如果找不到则返回 None。

import importlib.utilspec = importlib.util.find_spec("numpy")
if spec:module = importlib.util.module_from_spec(spec)spec.loader.exec_module(module)  # 加载模块print(module.array([1, 2, 3]))  # 使用 numpy.array

5. 获取模块路径(find_spec().origin)

from importlib.util import find_specspec = find_spec("torch")
if spec:print(f"torch 的位置: {spec.origin}")

6. 加载 .py 文件为模块(spec_from_file_location)

如果 .py 文件不在 Python sys.path 中,不能直接 import,但可以用 importlib 加载:
importlib.util.module_from_spec(spec):
这个函数用于创建一个新的模块对象,基于给定的模块规范对象 spec。返回的模块对象可以通过 sys.modules 进行访问。
importlib.util.spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None):
这个函数用于创建一个模块规范对象,基于给定的模块名称、模块文件路径和可选的加载器对象。它可以用于动态加载位于特定位置的模块。
importlib.abc.loader.exec_module(module)
运行模块代码。
首先、创建文件 “utils/testmodel.py”:

def say_hello():print("hello world")

然后、加载 “utils/testmodel.py” 为模块:

import importlib.util
import sysdef test_model(module_name, file_path):existed_spec = importlib.util.find_spec(module_name)if existed_spec:print(f"module {module_name} existed_spec")spec = existed_specif not spec.loader:raise Exception(f"Failed to load module {module_name} from {file_path!r}")else:print(f"module {module_name} not_existed_spec")# 创建模块 specspec = importlib.util.spec_from_file_location("testmodule", file_path)if not spec or not spec.loader:raise Exception(f"Failed to load module {module_name} from {file_path!r}")module = importlib.util.module_from_spec(spec)# 添加到系统模块if not existed_spec:sys.modules[module_name] = module# 执行模块代码spec.loader.exec_module(module)return moduleif __name__ == "__main__":module_name = "testmodule"# 指定文件路径# file_path = "/utils/testmodel.py"file_path = "utils/testmodel.py"module = test_model(module_name, file_path)module.say_hello()# 查看系统模块是否已经更新existed_spec = importlib.util.find_spec(module_name)if existed_spec:print(f"module {module_name} existed_spec {existed_spec.origin}")

运行结果:

> python.exe .\test.py
module testmodule not_existed_spec
hello world
module testmodule existed_spec D:\works\demo\importlb\utils/testmodel.py

7. 读取模块资源(importlib.resources)

import importlib.resources# # 读取 mypackage 包内的 data.txt 文件
# with importlib.resources.open_text("mypackage", "data.txt") as f:
with importlib.resources.open_text("requests", "__version__.py") as f:content = f.read()print(content)

运行结果:

> python.exe .\test.py
module testmodule not_existed_spec
hello world
module testmodule existed_spec D:\works\demo\importlb\utils/testmodel.py
# .-. .-. .-. . . .-. .-. .-. .-.
# |(  |-  |.| | | |-  `-.  |  `-.
# ' ' `-' `-`.`-' `-' `-'  '  `-'__title__ = "requests"
__description__ = "Python HTTP for Humans."
__url__ = "https://requests.readthedocs.io"
__version__ = "2.32.3"
__build__ = 0x023203
__author__ = "Kenneth Reitz"
__author_email__ = "me@kennethreitz.org"
__license__ = "Apache-2.0"
__copyright__ = "Copyright Kenneth Reitz"
__cake__ = "\u2728 \U0001f370 \u2728"

8. 获取模块元数据(importlib.metadata)

import importlib.metadataprint(importlib.metadata.version("requests"))  # 2.32.3 (获取 `requests` 版本)
http://www.dtcms.com/wzjs/688642.html

相关文章:

  • 网站做三层结构群晖 nas做网站 推荐
  • 学校网站建设策划福州 网站建设价格
  • vs做的网站源代码凡科网做的网站
  • 给别人做软件的网站电商企业网站建设方案
  • 特色的武进网站建设wordpress 跳转适配
  • 创建网站免费wordpress阿里百秀4.1
  • 服务公司网站建设建设网站需要哪个语言编译器
  • 柳州企业 商家应该如何做网站怎样做网站的快捷方式
  • 做网站多久互联网项目推广平台有哪些
  • 连云制作企业网站漳州市网站建设
  • 从百万到千万 网站怎么优化舟山建设企业网站
  • 网站后台编辑器下载浅笑云虚拟主机
  • 数据库网站 建设百度怎么推广产品
  • 数码家电商城网站源码wordpress 文章页 模板
  • 珠海手机网站开发建设网站实训报告
  • 图库下载网站源码东莞网站建设百度地图
  • 做公司网站多钱南昌建设医院官方网站
  • 中企视窗做网站怎么样网站服务器与虚拟主机
  • 网站仿做莲花直播
  • 专题网站建设工作香水网站模板
  • 灵台教育局网站师资队伍建设海外推广代理商
  • 深圳龙岗建网站网站cms系统下载
  • 金华网站制作系统网站域名设置
  • 深圳网站制作建设服务公司娄底市住房和城乡建设局网站
  • 深圳网站制作哪里找找人做的网站 没登录口
  • python做电商网站网上做娱乐广告的网站
  • 建瓯网站建设微网站模板前后台
  • 网站降权如何恢复手机宣传网站
  • 网页设计制作网站素材c# 网站开发教程
  • 东莞网站建设求职简历中级网页设计师