I've inherited a project in Python 2.7 that needs to be updated to be compatible with Python 3.9 because AWS Lambda has deprecated support for Py 2.7
The code that needs to be updated to 3.9:
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
In AWS Cloudwatch, I usually get an "Unable to Import Module" error, and I think it might be because of this line of code.
OPTION 1: Should I modify the code so it reads:
try:
from io import StringIO
except:
from io import StringIO
OPTION 2: Should I modify the code so it reads:
try:
from io import StringIO
except ImportError:
from cStringIO import StringIO
CodePudding user response:
The only thing you need in Python 3 is
from io import StringIO
The io
module replaces both the StringIO
and cStringIO
modules available in Python 2.
CodePudding user response:
From user @chepner: "In Python 2, one would try to import the faster cStringIO module, falling back to the slower pure-Python module if necessary. In Python 3, there is only a single user-facing io module. If importing it the first time failed, it would fail the second time as well. (If it does fail, that's a problem with your Python installation that is beyond the scope of your script to deal with.)"
My final code will read
from io import StringIO