Reputation: 13750
I have a Python 2.x program with this line of code:
from types import SliceType
When running the file with python3
, the following error is printed:
ImportError: cannot import name 'SliceType'
How can I fix this so both Python 2.x and Python 3.x can execute the file?
This is not a duplicate of any random other ImportError
question like this one. It is intended to be found via Google/SO search when you got the same error message. Before writing this question/answer, I wasn't able to find any solution for the issue described here.
Upvotes: 1
Views: 448
Reputation: 13750
You can use a try
-based construct to get full 2.x/3.x compatibility:
try:
from types import SliceType
except ImportError:
SliceType = slice
See this reference for a table on the Python3 names for objects in the Python2 types
module.
Upvotes: 2