- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在同一目录中有 2 个文件,一个编译后的库文件和源文件:
.
├── a.py
└── a.pyd
它看起来像import a
,实际上导入了a.pyd
模块。但我找不到任何官方文件来保证这一点。
有人知道不同文件类型的导入顺序吗?
同样的问题也适用于 Unix Python 扩展 (.so)
最佳答案
在典型的 Python 安装中,ExtensionFileLoader
类优先于 SourceFileLoader
用于 .py
文件。它是处理 .pyd
文件导入的 ExtensionFileLoader
,在 Windows 计算机上,您会发现 .pyd
在 importlib.machinery.EXTENSION_SUFFIXES
中注册。 (注意:在 Linux/macOS 上,它将包含 .so
)。
因此,在同一目录内发生名称冲突的情况下(这意味着按顺序查看 sys.path 时出现“平局”),a.pyd
文件将采用优先于 a.py
文件。您可以验证在创建空的 a.pyd
和 a.py
文件时,语句 import a
尝试加载 DLL(并且失败了,当然)。
要查看 CPython 源代码中的优先级,请查看 here in importlib._bootstrap_external. _get_supported_file_loaders
:
def _get_supported_file_loaders():
"""Returns a list of file-based module loaders.
Each item is a tuple (loader, suffixes).
"""
extensions = ExtensionFileLoader, _imp.extension_suffixes()
source = SourceFileLoader, SOURCE_SUFFIXES
bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES
return [extensions, source, bytecode] # <-- extensions before source!
有关文档引用,请参阅 http://www.python.org/doc/essays/packages/
What If I Have a Module and a Package With The Same Name?
You may have a directory (on sys.path) which has both a module spam.py and a subdirectory spam that contains an _init_.py (without the _init_.py, a directory is not recognized as a package). In this case, the subdirectory has precedence, and importing spam will ignore the spam.py file, loading the package spam instead. If you want the module spam.py to have precedence, it must be placed in a directory that comes earlier in sys.path.
(Tip: the search order is determined by the list of suffixes returned by the function imp.get_suffixes(). Usually the suffixes are searched in the following order: ".so", "module.so", ".py", ".pyc". Directories don't explicitly occur in this list, but precede all entries in it.)
本文档没有明确提及“.pyd”,但它在 Windows 中相当于“.so”。我刚刚在 Windows 机器上进行了测试,确实在后缀列表中 '.pyd'
出现在 '.py'
之前。
请注意,上面给出的引用文献非常古老!自从写这篇文章以来,导入系统已经被彻底改造,底层机制已经暴露给用户访问:你可以改变 sys.meta_path
例如,注册您自己的加载程序或更改优先级。因此,现在可以自定义“.py”而不是“.pyd”,并且 imp.get_suffixes()
所说的任何内容并不重要(实际上,该功能现已弃用)。当然,默认的 Python 安装不会这样做,并且默认优先级与上面提到的引用保持相同。
关于python - "import"更喜欢什么 - .pyd (.so) 还是 .py?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58686448/
我正在使用 Cython 构建一个链接到 DLL 文件的 python 模块。为了成功导入我的模块,我需要在 Windows 搜索路径中包含 DLL。否则,典型的错误消息是: ImportError:
在 this post ,答案是.c文件的输出路径。 我想知道如何将所有.pyd文件输出到特定目录(默认情况下,它与源代码混合在一起),例如目录pyd/ 在源代码中。 最佳答案 您可以将命令行参数--
我的 python 工作区中有两个需要 sip.pyd 的模块 Module1.pyd 需要 sip.pyd(实现 v 8.0-8.1) Module2.pyd 需要 sip.pyd(另一个实现 v6
这个问题在这里已经有了答案: How hard to reverse engineer .pyd files? (2 个答案) 关闭 9 年前。 我想知道,如果我将我的游戏导出为 .exe 并且所有
我的开发环境:赢7 64 python 2.7.6 64扭曲13.2 我已经安装了所需的所有模块。但总是抛出错误: from twisted.internet import reactor c
我正在尝试为用 pyd.wrap_class 包装的 D 类提供 python 迭代器支持。根据文档(https://github.com/ariovistus/pyd/wiki/ClassWrap#
我正在创建一个使用 Python OpenCV 的项目。我的图像处理有点慢,所以我想我可以通过创建一个 .pyd 文件(我在某处读到过)来加快代码速度。 我可以使用 Cython 创建一个 .c 文件
我的 python 项目有一个 C++ 组件,它被编译并作为 Python egg 中的 .pyd 文件分发。我注意到它似乎只与我们全新的一些 64 位 Windows 服务器不兼容。我们有 4 台(
看完How do I protect Python code? ,我决定在 Windows 上尝试一个非常简单的扩展模块。之前在linux上编译过自己的扩展模块,在windows上编译还是第一次。我期
Python import .pyd文件时会搜索sys.path列表中的路径 运行 import xxx.pyd 1. "ImportError: No module name
我试图找出反编译 python .pyd 文件的最佳方法。我所到的每一个地方都遇到了死胡同。似乎没有任何程序可以做到这一点,除了 Aaron Portnoy 和 Ali Rizvi-Santiago
这个问题已经有答案了: Collapse multiple submodules to one Cython extension (5 个回答) 已关闭 3 年前。 我在一个包中有多个 .py 文件
我开始慢慢涉足 Python/C API,经过大量摆弄和欺骗,我能够构建一个 spam.pyd 文件。 但是,我一定在这个过程中遗漏了一些东西,并希望有人能给我指出正确的方向。我认为一旦创建了 spa
我正在用 c++ 制作一个 .dll 插件并在其中嵌入 python 2.7。 在我导入大型程序之前,简单的 .py 程序一切正常。最奇怪的是程序第一次运行没问题,第二次却抛出异常: Unhandle
我正在开发一个主要使用 NumPy 和一个 SciPy 模块 (scipy.optimize.fsolve) 的小工具。我与他人分享它的想法是它与 Portable Python 打包在一起,这样理论
我使用 Cython 将 .pyx 文件转换为 .c。现在,我尝试使用 tcc 编译器将其编译为 .pyd: C:\Users\USER>"C:\Program Files\tcc\tcc.exe"
存在qml文件无法调用pyd文件函数的问题。 有几个文件: 我。 main.py -> python.exe 运行入口ii. mainWidget.py -> 显示 MyItem.qml ui 并将
我必须将模块从 .py 转换为 .pyd。我用 Cython 做到了。项目结束后我进行了测试,一切正常。当我使用 cx_Freeze 创建构建时,.pyd 模块产生了问题。我不明白为什么构建后会出现此
我想知道是否有一种解决方案可以两次导入从 C++ 源 pyd 模块编译的一个。那么要让一个模块的两个独立实例具有在 C 中定义的不同变量值?这是例子。我有一个简单的 cpp 模块: #include
我有一些 C++ 代码使用 boost 作为 python 模块工作。它实际上是另一个c++ python模块的插件。 在 Windows 上,我必须链接到这个库的 libavg.pyd 文件。 在
我是一名优秀的程序员,十分优秀!