- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
具有相对模块名称的 importlib.import_module
有时会失败。
例如,我有这样一个包结构:
├── test0
│ ├── __init__.py
│ ├── test1.py
│ └── test2.py
├── test_impl.py
测试代码如下:
import importlib
import sys
print(sys.version_info)
def test_imp_module(module_name, pkgname):
try:
_m = importlib.import_module(module_name, pkgname)
print("import OK: module={}, pkg={}".format(module_name, pkgname))
except Exception as e:
print(e)
test_imp_module(".test1", "test0")
test_imp_module("test0.test1", "")
if __name__ == '__main__':
test_imp_module(".test1", "test0")
test_imp_module("test0.test1", "")
在 Python3.5 中它会在第一个测试中失败:
$ python3 test_impl.py
sys.version_info(major=3, minor=5, micro=2, releaselevel='final', serial=0)
Parent module 'test0' not loaded, cannot perform relative import
import OK: module=test0.test1, pkg=
import OK: module=.test1, pkg=test0
import OK: module=test0.test1, pkg=
在 python3.6 中有效:
$ python3 test_impl.py
sys.version_info(major=3, minor=6, micro=4, releaselevel='final', serial=0)
import OK: module=.test1, pkg=test0
import OK: module=test0.test1, pkg=
import OK: module=.test1, pkg=test0
import OK: module=test0.test1, pkg=
这是预期的还是错误?更新: Python2.7.12 与 3.6 具有相同的行为。
最佳答案
docs说:
importlib.import_module(name, package=None)
...
Changed in version 3.3: Parent packages are automatically imported.
因此,3.6.4 行为是正确的,而 3.5.2 行为是错误的。你好像在打#30876 ,并且该问题表示已在版本 5 micro 中修复,即 v3.5.5。
感谢 6 号房间的 vaultah finding the ticket .
关于python - importlib.import_module 在 python3.5 中有奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49787648/
我正在尝试基于 imp 重新实现一些代码要使用的模块importlib反而。 imp.find_module 函数之间存在细微差别和 importlib.find_loader : # Using i
我是 Django 新手。我有一个 Django 休息项目。当我运行它时,错误显示为 from django.utils.importlib import import_module ImportEr
我收到了一份。在Spyder中第二次从子文件夹导入库时出错,但第一次(重新启动Spyder后)或在Spyder外导入时工作正常。。代码是:。其中,test_lib.py只是。输出结果为:。当库不在子文
from importlib.machinery import EXTENSION_SUFFIXES 导入错误:没有名为machinery的模块 清理中...命令 python setup.py Eg
pytest-6.0介绍a new 'import-mode' option dubbed 'importlib'连同声明: "We intend to make importlib the defa
我正在尝试导入 importlib 模块,但收到此消息: >>> importlib Traceback (most recent call last): File "", line 1, in N
鉴于此代码,我如何告诉 python 不使用缓存源或重新加载? import importlib.util spec = importlib.util.spec_from_file_location(
我正在尝试从不同的 python 文件 (*.py) 导入名为“tasks”的字典。 路径存储在我的数组directories['code']中。并非所有文件都包含这些任务指令之一。 问题是,一旦找到
我正在使用 flask 并具有以下结构 manage_server.py cas --- __init__.py --- routes.py --- models.py --- templates
这个问题在这里已经有了答案: from django.utils.importlib import import_module ImportError: No module named import
一旦你导入了 foo 模块,其中你有 bar 类 import importlib foo = importlib.import_module('path/to/foo') 如何简单地调用 bar.r
这个问题在这里已经有了答案: TypeError: Missing 1 required positional argument: 'self' (8 个答案) 关闭 2 年前。 我知道类似的问题已
我正在寻找一种使用 importlib 的方法在 Python 2.x 中即时重写导入模块的字节码。换句话说,我需要在导入期间在编译和执行步骤之间 Hook 我自己的函数。除此之外,我希望导入功能能够
使用 import 语句导入具有本地名称的模块很容易: import numpy as np 我相信 np 在这里被称为“本地名称”,但我可能会混淆。 我不知道如何使用 importlib 模块来做同
我想根据传递给 Python 脚本的参数在我的主函数中选择要导入的模块。所以,我正在使用其中之一 blah = importlib.import_module("blah1") blah = impo
根据 this answer , 你可以使用 importlib 到 import_module 使用像这样的相对导入: importlib.import_module('.c', 'a.b') 为什
第二个断言失败,表明importlib.reload静静地重新加载修改后的模块失败,谁能解释为什么? import os import sys import tempfile import impor
我希望在 Python (3.7) 中动态导入一个模块,模块的代码是在一个字符串中定义的。 下面是一个使用 imp 模块的工作示例,该模块已被弃用,取而代之的是 importlib(从版本 3.4 开
第二个断言失败,表明importlib.reload静静地重新加载修改后的模块失败,谁能解释为什么? import os import sys import tempfile import impor
在我的模块中,我有几个函数依赖于启动时间较长的外部模块。如何使用 LazyLoader ?如果我有 import veggies 或 import veggies.brussels.sprouts 或
我是一名优秀的程序员,十分优秀!