Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no module named StringIO

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

In operation.py I 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.

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'.

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

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

operation.py:

    from cStringIO import StringIO


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

run.py:

    import operation


    def main():
        operation

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

like image 465
user9399101 Avatar asked Jun 15 '26 17:06

user9399101


1 Answers

I think you're looking for the io module 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 here for more info about that.

Historical note: Here is the reason why we no longer have both cStringIO and 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)

like image 190
Niayesh Isky Avatar answered Jun 17 '26 07:06

Niayesh Isky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!