在 Python 中导入模块的问题

Posted

技术标签:

【中文标题】在 Python 中导入模块的问题【英文标题】:Issues in importing module in Python 【发布时间】:2021-05-28 17:02:45 【问题描述】:

我对 python 和一般的编码都很陌生,所以对于可能的愚蠢问题表示歉意。

我的问题是我用 python 编写了一个代码,因为它非常慢,我尝试使用 cython 来加速它。所以目前我想在 .py 文件中导入一个名为 test_cython.C 的模块,但是使用 import 我收到了这个错误(ModuleNotFoundError: No module named 'test_cython')......你对如何解决这个问题?或者更笼统地说,如何在 python 中导入 C 模块?

【问题讨论】:

这能回答你的问题吗? Can we use C code in Python? 对于 ModuleNotFoundError,您必须提供显示问题的代码的 minimal reproducible example。 见cython.readthedocs.io/en/latest/src/quickstart/build.html,你好像没有在构建你的扩展。 【参考方案1】:

要使用 c 文件,您需要先编译 C 代码,然后在代码中使用编译后的文件。 看看这个链接:http://book.pythontips.com/en/latest/python_c_extension.html

【讨论】:

以上是关于在 Python 中导入模块的问题的主要内容,如果未能解决你的问题,请参考以下文章

python中导入模块/包的几种方式

python 中导入redis模块

在 python 类中导入模块

在pycharm中导入自定义模块

安装的 PILLOW 模块未识别为在 Python 3 中导入

我无法使用 PythonKit 在 Xcode 11 中导入 Python 模块