- Python – no module named StringIO
- Best Solution
- Related Solutions
- Modulenotfounderror no module named stringio
- How to solve “no module named stringio” in Python
- Conclusion
- Leave a Comment Cancel reply
- Saved searches
- Use saved searches to filter your results more quickly
- Python 3.x support (ImportError: No module named ‘StringIO’) #18
- Python 3.x support (ImportError: No module named ‘StringIO’) #18
- Comments
Python – 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?
from cStringIO import StringIO str_io = StringIO() g = Generator(str_io, False) # There is a full code here.
import operation def main(): operation
The operation.py has a warning but runs well, run.py has a fail.
Best Solution
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)
Related Solutions
Python – Calling a function of a module by using its name (a string)
Assuming module foo with method bar :
import foo method_to_call = getattr(foo, 'bar') result = method_to_call()
You could shorten lines 2 and 3 to:
if that makes more sense for your use case.
You can use getattr in this fashion on class instance bound methods, module-level methods, class methods. the list goes on.
Python – How to import a module given its name as string
With Python older than 2.7/3.1, that’s pretty much how you do it.
For newer versions, see importlib.import_module for Python 2 and Python 3.
You can use exec if you want to as well.
Or using __import__ you can import a list of modules by doing this:
>>> moduleNames = ['sys', 'os', 're', 'unittest'] >>> moduleNames ['sys', 'os', 're', 'unittest'] >>> modules = map(__import__, moduleNames)
Modulenotfounderror no module named stringio
In this article, we will show you how to solve the modulenotfounderror no module named stringio in Python.
This error indicates that the stringIO module is not installed in your system or Python environment.
What is Python?
Python is one of the most popular programming languages.
It is used for developing a wide range of applications.
In addition, Python is a high-level programming language that is used by most developers due to its flexibility.
Returning to our issue, we must take a few actions to fix this error.
So, without further ado, let’s move on to our “how to fix this error” tutorial.
How to solve “no module named stringio” in Python
- Verify the module name you are using.
Resolving the modulenotfounderror no module named stringio is an easy task.
All you have to do is verify that the module name you are using is correct.
If you’re using Python 3.x, use from io import StringIO instead, since the io module has taken over the stringio module.
If you’re using Python 2.x, install the StringIO module using pip.
To install this module, open your cmd or command prompt, then input the command pip install -U StringIO.
Conclusion
In conclusion, the error modulenotfounderror no module named stringio can be easily solved by:
“Verifying that the module name we are using is correct or by installing the ‘stringio’ module in your system or Python environment.”
By following the guide above, there’s no doubt that you’ll be able to resolve this error quickly.
We hope you’ve learned a lot from this.
Leave a Comment Cancel reply
You must be logged in to post a comment.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 3.x support (ImportError: No module named ‘StringIO’) #18
Python 3.x support (ImportError: No module named ‘StringIO’) #18
Comments
When running python handler.py I get the following error:
ImportError: No module named 'StringIO'
I believe this is related to python 3.x
⇒ python --version Python 3.5.2
The text was updated successfully, but these errors were encountered:
try: from StringIO import StringIO except ImportError: from io import StringIO
Thanks.After above fix got below error
urlparse was renamed to urllib.parse in Python 3.
When running python3 handler.py I get the following error:
ModuleNotFoundError: No module named ‘StringIO’
I already added this following code in my handler.py and also i changed the urlparse to urllib.parse!
Have you tried just using from io import StringIO , @Sechunt3r?
@EdOverflow
python3 handler.py works fine but when I parse urls in the localhostm I get this error
File "/usr/lib/python3/dist-packages/tornado/web.py", line 1590, in _execute result = method(*self.path_args, **self.path_kwargs) File "handler.py", line 198, in post data = self.parseLinks(url, headers) File "handler.py", line 184, in parseLinks file = self.fetchURL(url, headers) File "handler.py", line 179, in fetchURL res = sc.execute(url) File "/opt/JSParser/safeurl.py", line 710, in execute response = StringIO.StringIO() AttributeError: type object '_io.StringIO' has no attribute 'StringIO' ERROR:tornado.access:500 POST /parse/ajax (::1) 73.78ms
hey anyone can help me in these errors.
ss attached
@kunal812eee replace import StringIO to from io import StringIO
root@local:~/JSParser$ python3 handler.py
Traceback (most recent call last):
File «handler.py», line 5, in
import safeurl, types, sys, re, mimetypes, glob, jsbeautifier, urlparse, pycurl
ModuleNotFoundError: No module named ‘urlparse’
try: from StringIO import StringIO except ImportError: from io import StringIO
@EdOverflow
python3 handler.py works fine but when I parse urls in the localhostm I get this error
File "/usr/lib/python3/dist-packages/tornado/web.py", line 1590, in _execute result = method(*self.path_args, **self.path_kwargs) File "handler.py", line 198, in post data = self.parseLinks(url, headers) File "handler.py", line 184, in parseLinks file = self.fetchURL(url, headers) File "handler.py", line 179, in fetchURL res = sc.execute(url) File "/opt/JSParser/safeurl.py", line 710, in execute response = StringIO.StringIO() AttributeError: type object '_io.StringIO' has no attribute 'StringIO' ERROR:tornado.access:500 POST /parse/ajax (::1) 73.78ms
Did you find any solution for this one ?
@EdOverflow
python3 handler.py works fine but when I parse urls in the localhostm I get this error
File "/usr/lib/python3/dist-packages/tornado/web.py", line 1590, in _execute result = method(*self.path_args, **self.path_kwargs) File "handler.py", line 198, in post data = self.parseLinks(url, headers) File "handler.py", line 184, in parseLinks file = self.fetchURL(url, headers) File "handler.py", line 179, in fetchURL res = sc.execute(url) File "/opt/JSParser/safeurl.py", line 710, in execute response = StringIO.StringIO() AttributeError: type object '_io.StringIO' has no attribute 'StringIO' ERROR:tornado.access:500 POST /parse/ajax (::1) 73.78ms
Did you find any solution for this one ?
Edit both files ( handler.py and safeurl.py)
from io import StringIO
@EdOverflow
python3 handler.py works fine but when I parse urls in the localhostm I get this error
File "/usr/lib/python3/dist-packages/tornado/web.py", line 1590, in _execute result = method(*self.path_args, **self.path_kwargs) File "handler.py", line 198, in post data = self.parseLinks(url, headers) File "handler.py", line 184, in parseLinks file = self.fetchURL(url, headers) File "handler.py", line 179, in fetchURL res = sc.execute(url) File "/opt/JSParser/safeurl.py", line 710, in execute response = StringIO.StringIO() AttributeError: type object '_io.StringIO' has no attribute 'StringIO' ERROR:tornado.access:500 POST /parse/ajax (::1) 73.78ms
Did you find any solution for this one ?
In python 3.7, I edit safeurl.py as following and it work
- import io
- edit line No.710 response = StringIO.StringIO() to response = io.BytesIO()
- edit line No.732 return response.getvalue() to return response.getvalue().decode(‘utf-8’)