An update on our migration from Python 2.7 to Python 3.5. Since our [initial announcement] we have been feverishly converting things over to Python 3. Everything has progressed smoothly.
As an interim step, we had added a feature, in both the IDE and notebooks, to run either Python 2.7 or 3.5. Hopefully, this helped in troubleshooting and keeping any existing code running. Our next step however, is to phase out this feature and remove the Python 2.7 environment entirely from Quantopian. The plan is to remove Python 2.7 on March 10, 2020.
What does this mean for you? For most users the transition will be a non-event. If you have any code currently written in 2.7, simply go into the IDE or notebook and select Python 3.5. This will verify it runs in 3.5. Also, ensure any new algos and notebooks are coded using Python 3.5. In the future, most old algorithms and notebooks should run. However, the auto-conversion tool will still be available so you can easily convert any of those old algorithms to Python 3.5. Also note that new contest entries must be coded in Python 3.5. However, existing contest entries coded in Python 2.5 will continue to run 'as is' and won't require changes.
If you do encounter errors when updating Python 2.7 code , you may want to check these common issues.
# Python 2 could print like this print 'hello world' # Python 3 requires parenthesis like this print('hello world')
.iterkeys()are no longer supported for iterating dictionaries. Replace with
.keys(). Check out [this explanation] for specific details.
# In Python 2 iterating over a dictionary, such as `context.portfolio`, could be done for stock, position in context.portfolio.iteritems(): # In Python 3 iterating over a dictionary should be done for stock, position in context.portfolio.items():
Division is always 'true' division in Python 3 and returns a real number. This actually often created problems in Python 2 so it may be a welcome relief for most. However, this can cause some inadvertent errors. We have seen this show up as "ValueError: At based indexing on an integer index can only have integer indexers". This happens because ones code calculated an index through division of two integers. In Python 2 the result would always be an integer. In Python 3 it will be a real number. Using a real number as an index can often create issues. If you get some sort of "non-integer index" error then look for possible issues with division.
# In Python 2 dividing two integers would result in an integer. 'result1' will equal 0 and 'result2' will equal .5. # result1' will not equal 'result2' # In Python dividing two integers would result in an real number. 'result1' and result2' will both equal .5 result1 = 1 / 2 result2 = 1.0 / 2.0
If you are having issues converting any of your algorithms, please feel free to reach out to our support team.