Python 没有名为 StringIO 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48938978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:55:19  来源:igfitidea点击:

no module named StringIO

pythonpython-3.xstringiocstringio

提问by user9399101

I have python 3.6. I want to execute python file named 'operation.py' from another python file named 'run.py'.

我有 python 3.6。我想从另一个名为“run.py”的python文件中执行名为“operation.py”的python文件。

In operation.pyI do from cStringIO import StringIO. PyCharm shows me a warning that there is no module named StringIO. I know that since python3 I have to import StringIO module from io. However, when I use this importation, the functions of this module are no longer work.

operation.py我做from cStringIO import StringIO。PyCharm 向我显示了一个警告,指出没有名为 StringIO 的模块。我知道从 python3 开始,我必须从 io 导入 StringIO 模块。但是,当我使用此导入时,该模块的功能不再起作用。

Although there is a warning in from cStringIO import StringIO, the code still works (I know this import really works because I tried to make it a comment and it couldn't run). The problem is that when I try to run this file by the 'run.py' file, it can't run and prints the following message: ModuleNotFoundError: No module named 'cStringIO'.

尽管 中有警告from cStringIO import StringIO,但代码仍然有效(我知道这个导入确实有效,因为我试图将其设为注释但它无法运行)。问题是,当我尝试通过“run.py”文件运行此文件时,它无法运行并打印以下消息:ModuleNotFoundError: No module named 'cStringIO'.

I tried to use this Unresolved reference issue in PyCharmbut it didn't help.

我尝试在 PyCharm 中使用这个未解决的参考问题,但没有帮助。

Why does 'operation.py' run though the warning, but 'run.py' does not? How can I solve this?

为什么 'operation.py' 虽然有警告但运行,但 'run.py' 没有?我该如何解决这个问题?

operation.py:

操作.py:

    from cStringIO import StringIO


    str_io = StringIO()
    g = Generator(str_io, False)
    # There is a full code here...

run.py:

运行.py:

    import operation


    def main():
        operation

The operation.pyhas a warning but runs well, run.py has a fail.

operation.py有一个警告,但运行良好,run.py有失败。

回答by Niayesh Isky

I think you're looking for the iomodule in Python 3.x. cStringIO(which is a Python 2 module that is a faster version of StringIO, see here) was replaced with io, along with a host of other changes. See herefor more info about that.

我认为您正在寻找Python 3.x 中的io模块。cStringIO(这是一个 Python 2 模块,是 的更快版本StringIO,请参见此处)被替换为io,以及许多其他更改。有关更多信息,请参见此处

Historical note: Here is the reason why we no longer have both cStringIOand StringIO:

历史记录:以下是我们不再同时拥有cStringIOand的原因StringIO

A common pattern in Python 2.x is to have one version of a module implemented in pure Python, with an optional accelerated version implemented as a C extension; for example, pickle and cPickle. This places the burden of importing the accelerated version and falling back on the pure Python version on each user of these modules. In Python 3.0, the accelerated versions are considered implementation details of the pure Python versions. Users should always import the standard version, which attempts to import the accelerated version and falls back to the pure Python version. The pickle / cPickle pair received this treatment. The profile module is on the list for 3.1. The StringIO module has been turned into a class in the io module. (Source)

Python 2.x 中的一个常见模式是用纯 Python 实现模块的一个版本,以及作为 C 扩展实现的可选加速版本;例如,pickle 和 cPickle。这给这些模块的每个用户带来了导入加速版本和使用纯 Python 版本的负担。在 Python 3.0 中,加速版本被视为纯 Python 版本的实现细节。用户应该始终导入标准版本,它会尝试导入加速版本并回退到纯 Python 版本。pickle / cPickle 对接受了这种处理。配置文件模块在 3.1 的列表中。StringIO 模块已经变成了 io 模块中的一个类。(来源