Python3 - OpenStack (original) (raw)

This page tracks the progress of Python 3 effort porting for OpenStack.

Contents

IRC: #openstack-dev

Join the #openstack-dev IRC channel on the Freenode network to discuss Python 3.

Current Status

Since Ussuri, OpenStack is Python3 only except Swift and Storlets. Python2.7 support and testing has been removed from all other projects. For more details, please refer to Drop py2.7 community goal

Release Python Versions Notes
V python3.6 python3.7 python3.8 [1]
Ussuri (Dropped Python 2.7) python3.6 python3.7 [2] In Ussuri, OpenStack is python3-only except Swift & Storlets. Refer to the drop Python 2.7 goal
Train python2.7 python3.6 python3.7 [3] Train is the last release where python2.7 testing was required.

Python 3

Why should OpenStack move to Python 3 right now?

Python 3 is usually seen as the new Python version which breaks compatibility and raises new Unicode issues. Python 3 is much more than that. It’s a new clean language which has a more consistent syntax. It has many new features, not less than 15 new modules. Python 3 is already well supported by major Linux distributions, whereas Python 2.7 reached its end-of-life. Slowly, some bugs cannot be fixed in Python 2.7 anymore and are only fixed in the latest Python 3 release. Python 3 is now 5 years old and considered as a mature programming language.

Python 3 Status of OpenStack projects

Common Libraries (Oslo Projects)

See programs.yaml for the list of Common Libraries.

Project Python 3 Only (Ussuri onwards) Python 3 compatibility Comment
automaton Yes Yes
castellan Yes Yes
cliff Yes Yes
debtcollector Yes Yes
futurist Yes Yes
oslo.cache Yes Yes
oslo.concurrency Yes Yes
oslo.config Yes Yes
oslo.context Yes Yes
oslo.db Yes Yes
oslo.i18n Yes Yes
oslo.log Yes Yes
oslo.messaging Yes Yes
oslo.middleware Yes Yes
oslo.policy Yes Yes
oslo.privsep Yes Yes
oslo.reports Yes Yes
oslo.rootwrap Yes Yes
oslo.serialization Yes Yes
oslo.service Yes Yes
oslotest Yes Yes
oslo.versionedobjects Yes Yes
oslo.vmware Yes Yes
oslo.utils Yes Yes
pylockfile NA Yes Project is deprecated.
stevedore Yes Yes
taskflow Yes Yes
tooz Yes Yes

Development tools

Project Python 3 Only (Ussuri onwards) Python 3 compatibility Comment
cookiecutter Yes yes
hacking Yes yes
pbr No yes pbr is not capped so we need to keep support for py2.7 code on safer side.
stackforge/python-jenkins No yes
openstack-infra/jenkins-job-builder No partial https://review.openstack.org/172238

OpenStack clients

Project Python 3 Only (Ussuri onwards) Python 3 compatibility CI tests running? Python 3 classifiers ? Blocked by Comment
keystonemiddleware Yes Yes Voting On PyPI
python-barbicanclient No Yes Voting On PyPI https://review.opendev.org/#/c/699096/
python-ceilometerclient Yes Yes Voting On PyPI
python-cinderclient Yes Yes Voting On PyPI
python-fuelclient N/A Yes voting On PyPI This project is no longer maintained.
python-glanceclient Yes Yes Voting On PyPI
python-heatclient Yes Yes Voting On PyPI
python-ironicclient Yes Yes Voting On PyPI
python-keystoneclient Yes Yes Voting On PyPI
python-manilaclient Yes Yes Voting On PyPI
python-zaqarclient Yes Yes Voting On PyPI
python-masakariiclient Yes Yes Voting On PyPI
python-monascaclient Yes Yes Voting On PyPI
python-novaclient Yes Yes Voting On PyPI
python-neutronclient Yes Yes Voting On PyPI
python-octaviaclient Yes Yes Voting On PyPI
python-openstackclient Yes Yes Voting On PyPI As of 0.9
python-saharaclient Yes Yes Voting On PyPI
python-searchlightclient Yes Yes Voting On PyPI
python-senlinclient Yes Yes Voting On PyPI
python-swiftclient No Yes Voting On PyPI Swift is keeping python2 support
python-tuskarclient N/A Yes Voting On PyPI This project is no longer maintained.
python-troveclient Yes Yes Voting On PyPI
python-watcherclient Yes Yes Voting On PyPI
python-designateclient Yes Yes Voting On PyPI

OpenStack Libraries

Project Python 3 Only (Ussuri onwards) Python 3 compatibility CI tests running? Python 3 classifiers ? Comment
blazar-nova Yes Yes voting Yes
ceilometermiddleware Yes Yes voting Yes
django_openstack_auth N/A Yes voting Yes The code has been merged into openstack/horizon.
glance_store Yes Yes voting Yes
heat-translator Yes Yes voting Yes
ironic-lib Yes Yes voting Yes
keystoneauth Yes Yes voting Yes
keystonemiddleware Yes Yes voting Yes
ldappool Yes Yes voting Yes
monasca-statsd Yes Yes voting Yes
mistral-lib Yes Yes voting Yes
neutron-lib Yes Yes voting Yes
octavia-lib Yes Yes voting Yes
os-brick Yes Yes voting Yes
os-client-config Yes Yes voting Yes
os-traits Yes Yes voting Yes
os-vif Yes Yes voting Yes
os-win Yes Yes voting Yes
osc-lib Yes Yes voting Yes
osc-placement Yes Yes voting Yes
pycadf Yes Yes voting Yes
shade No Yes voting Yes shade is on extended maintainance.
sqlalchemy-migrate N/A Yes voting Yes This project is not maintained under OpenStack
sushy Yes Yes voting Yes
taskflow Yes Yes voting Yes
tosca-parser Yes Yes voting Yes

Dependencies: Environment markers

Example to not install the "futures" dependency on Python 3.3 and newer:

futures; python_version < '3.2'

OpenStack applications (tc:approved-release)

OpenStack applications approved by the OpenStack Technical Committee.

Project Python 3 Only (Ussuri onwards) Unit tests pass on Python 3 py35 gate functional tests Comments
ceilometer Yes Yes voting
cinder Yes Yes voting integrated-gate-py35 cinder-python3 blueprint (written by Victor Stinner) has been accepted for Liberty. Patches: bp/cinder-python3 topic. Note: Commit message must use "Partial-Implements: blueprint cinder-python3" to be linked to the blueprint. Kendall Nelson, Ivan Kolodyazhny (core) and Eric Harney (core) prefer this syntax: see COMMIT_MSG.
designate Yes Yes voting designate-bind9-py36 designate-bind9-py35 Relies on oslo.service WSGI code that does not work under Python 3 with SSL. Functional tempest tests are ran on both of these jobs. [4]
glance Yes Yes voting integrated-gate-py35 Relies on oslo.service WSGI code that does not work under Python 3. [5] Victor Stinner is working on porting Glance to Python3. Patches: Patches for glance (topic: py3).
heat Yes Yes voting heat-functional-convg-mysql-lbaasv2-py35 Sirushti Murugesan wrote and implemented the spec Python34 Support (which was accepted for Liberty). Mitaka version will fully support Python 3. Great job Sirushti Murugesan who did almost all the work!
horizon Yes Yes voting horizon-openstack-tox-python3-django111,horizon-integration-tests, horizon-dsvm-tempest-plugin George Peristerakis wrote Make Horizon python3.4 compatible in May 2015, the blueprint was approved. Patches: bp/porting-python3 topic.
ironic Yes Yes voting ironic-tempest-dsvm-ipa-partition-pxe_ipmitool-tinyipa-python3 Python 3.4 unit tests are now being run for openstack/ironic. The unit tests are a voting job.Thanks to Victor Sergeyev for all of his work to update the Ironic code to make it pass the unit tests using Python 3.4: Run tests in py34 environment
keystone Yes Yes voting integrated-gate-py35 The move to pyldap and new ldappool were major hurdles solved in Newton. Great work by the Keystone team to get the last things through the gate in Newton.
neutron Yes Yes voting integrated-gate-py35 Porting to Python 3 spec (by Cyril Roelandt) accepted for Liberty, port in progress. Patches of the blueprint neutron-python3
neutron-lbaas N/A Yes voting neutron-lbaasv2-dsvm-py3x-api and neutron-lbaasv2-dsvm-py3x-api-namespace This project is no longer maintained.
neutron-fwaas Yes Yes voting neutron-fwaas-networking-midonet-cross-py35 (non-voting)
neutron-vpnaas Yes Yes voting
nova Yes Yes voting integrated-gate-py35 Status at 2019-01-24: Unit tests all running and voting under Python 3 via the use of the mox3 package. There are only 3 unit test files left that are using mox3 (nova/tests/unit/cells/test_cells_messaging.py, nova/|tests/unit/network/test_neutronv2.py, nova/tests/unit/network/test_manager.py).We advised contributors not to spend time replacing mox in files testing cells v1 and nova-network (test_cells_messaging.py and test_manager.py), as both are slated for removal as soon as we're able. Progress is being tracked at: https://blueprints.launchpad.net/nova/+spec/mox-removal-stein
sahara Yes Yes voting sahara-tests-scenario-py3 (experimental) Status at 2018-02-23: the job fails during devstack deployment, because swift does not start. The list of its dependencies is minimal and components like keystone are optional. This was partially addressed with an extra dependency: https://review.openstack.org/#/c/544121/ but this requires a) support in devstack (tentative support in https://review.openstack.org/#/c/544383/) b) at least another extra, or a more generic way of doing this; even with an additional extra explicitly required in devstack, the extras would be installed from pip breaking the test of depending patches.We are facing an issue with pickle library and python 3, once that gets fixed we can try to make the python 3 gate voting.
swift No Yes voting Swift will keep python2 support.Update (2016-06-27): master branch is currently in a soft-freeze in order to get encryption middleware merged Python 3 bugs: https://bugs.launchpad.net/swift/+bug/1614289 Plan to add Python 3 support to Swift. Patches: Patches with the topic py3 Python 3 unit tests require liberasurecode >= 1.0.9 and PyEClib >= 1.0.9. To install liberasurecode (in /usr): git clone https://bitbucket.org/tsg-/liberasurecode/ cd liberasurecode /autogen.sh && /configure --prefix=/usr && make && sudo make install liberasurecode >= 1.0.9 is available in Ubuntu Precise, Ubuntu Trusty, Fedora 22, Fedora 23 and CentoOS 7.Update 2018-02-22: A list of current blockers: https://gist.github.com/tipabu/833b03a865dba96e9fa2230b82f5d075 Update 2018-03-06: Current list of blockers with progress notes: https://etherpad.openstack.org/p/Swift_py3
trove Yes Yes voting trove-devstack-base Status at 2019-01-06: Unit tests - 1708 tests, 1707 passed, 1 skipped.Integration tests are based on trove-devstack-base job.Status at 2016-07-25: 1,155 / 2,014 (run_tests.py: 398; testr: 1,521; generate_examples: 95) unit tests (57%), TODO: 859 testsStatus at 2016-06-22: 839 (testtools) / 1,992 (run_tests.py: 398; testr: 1,499; generate_examples: 95) unit tests (42%), TODO: 1,153 testsStatus at 2018-02-22: It appears that only one test is in the blacklist now: guestagent.test_operating_systemrun_tests.py (proboscis) failures: https://review.openstack.org/#/c/346905/1 Victor Stinner proposed the trove-python3 blueprint for Mitaka.From Victor (3/17/2016): "I propose to discuss Python 3 before the summit. For example, prepare a concrete plan to port Trove to Python 3, list technical issues like MySQL-Python, etc."Patches: search for the bp/trove-python3 topic.

Other OpenStack Applications and Projects

List of all OpenStack projects: projects.yaml.

| Project | Python 3 Only (Ussuri onwards) | Unit tests pass on Python 3 | py35 gate | functional tests | Comments | | | ---------------------------------------------------------------------------------- | ------------------------------ | --------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | adjutant | Yes | Yes | voting | Partial* | (*) Adjutant's unit tests include a suite of API tests against Adjutant APIs using DRF's internal test classes. While not true functional tests, they do stand up an internal web server and process real workloads from Adjutant's perspective, with mocking for external APIs and services (Keystone, Nova, Neutron, etc) in the places we touch their clients. This tests 92% of the code, so in all likelihood covers most py3 vs py2 issues. Tempest tests are also planned for later. | | | aodh | Yes | Yes | voting | | | | | barbican | Yes | Yes | voting | unspecified | Blueprint barbican-py3. | | | blazar | Yes | Yes | voting | blazar-devstack-dsvm-py35 | | | | cloudkitty | Yes | Yes | voting | cloudkitty-tempest-full-python3 | | | | congress | Yes | Yes | voting* | congress-devstack-py35-api-mysql | (*)py35 support required tiny modification to antlr3 package, creating debian packaging difficultyRandom Stacker wrote a support-python3 blueprint. Patches: search for the topic bp/support-python3. | | | cyborg | Yes | No job | No job | | | | | designate | Yes | Yes | voting | designate-devstack-pdns4-py35 (check queue only) | Pradeep Kumar Singh proposed the blueprint designate-py3 which was accepted for Liberty. Patches: topic:bp/designate-py3. | | | ec2-api | Yes | Yes | voting | openstack-tox-py35 | | | | freezer | Yes | Yes | voting | | | | | karbor | Yes | Yes | voting | | | | | kuryr | Yes | Yes | voting | openstack-tox-py35 | | | | magnum | Yes | Yes | voting | (voting: magnum-functional-api) AND (non-voting: magnum-functional-k8s AND magnum-functional-swarm-mode) | | | | manila | Yes | Yes | voting | manila-tempest-minimal-dsvm-dummy | | | | manila-ui | Yes | Yes | voting | horizon-openstack-tox-python3-django111 | | | | masakari | Yes | Yes | voting | openstack-tox-py35 | | | | mistral | Yes | Yes | voting | | | | | monasca-agent | Yes | Yes | voting | monasca-tempest-python3-influxdb | https://storyboard.openstack.org/#!/story/2000975 | | | monasca-api | Yes | Yes | voting | monasca-tempest-python3-influxdb | https://storyboard.openstack.org/#!/story/2000975 | | | monasca-log-api | Yes | Yes | voting | monascalog-python3-tempest | | | | monasca-ui | Yes | Yes | voting | | https://storyboard.openstack.org/#!/story/2000975 | | | murano | Yes | Yes | voting | | Blueprint: murano-python-3-support. | | | murano-agent | Yes | Yes | voting | | Blueprint: murano-python-3-support. | | | octavia | Yes | Yes | voting | octavia-tox-functional-py35 | | | | panko | Yes | Yes | voting | panko-tox-py35-mysql and panko-tox-py35-postgresql | | | | qinling | Yes | Yes | voting | | | | | rally | Yes | Yes | voting | rally-tox-functional-py3 and rally-tox-self | A big thank to Andrey Kurilin for a lot of work in this direction | | | searchlight | Yes | Yes | voting | openstack-tox-py36 | | | | senlin | Yes | Yes | voting | senlin-dsvm-tempest-py35-api, senlin-dsvm-tempest-py35-functional, senlin-dsvm-tempest-py35-integration (check only, non-voting) | | | | solum | Yes | Yes | voting | solum-devstack-py35 (check only) | Victor Stinner proposed the blueprint solum-python3 which was approved. Patches: search for bp/solum-python3 topic (open reviews). | | | storlets | No | Yes | voting | | Storlets will keep python2 support. | | | tacker | Yes | Yes | voting | tacker-functional-devstack-python3 (check only, non-voting) | | | | tricircle | Yes | Yes | voting | | | | | vitrage | Yes | Yes | voting | vitrage-dsvm-api-py35 and vitrage-dsvm-datasources-py35 | | | | watcher | Yes | Yes | voting | | | | | zaqar | Yes | Yes | voting | | | | | zun | Yes | Yes | voting | zun-tempest-py35-docker-sql | | | | networking-l2gw | Yes | Yes | voting | | | | | vmware-nsx | Yes | Yes | voting | All 3rd part CI jobs are running python 3 | | | | vmware-nsxlib | Yes | Yes | voting | tempest-api-vmware-nsxv3 is running python 3.5 and is voting | | |

Unsorted projects:

Python 2: Python 2.6 support dropped, Python 2.7 only

OpenStack Liberty targets Python 2.7 and 3.4.

Python 2.6 support is being dropped in OpenStack since OpenStack Juno for servers. Python 2.6 support is currently kept in Oslo libraries and clients. See juno-cross-project-future-of-python etherpad.

Python 3.3 support is being dropped since OpenStack Liberty.

Python 2.6 is slowly being dropped in the whole OpenStack project, ex: Remove p26 job from DIB.

Python 2.6 support will be removed in Oslo and Clients clients for OpenStack Mitaka: Oslo libraries dropping python 2.6 compatability.

Port Python 2 code to Python 3

OpenStack project chose to use the same code base for Python 2 and Python 3. The Six: Python 2 and 3 Compatibility Library helps to write code working on both versions. OpenStack supported Python 2.6 for RHEL up to Juno, but not Python 2.5 and older. As we are targeting Python 3.4 and up, there is no need to avoid u'unicode' syntax. Do not use six.u('unicode').

Before you begin

If you're doing development with Ubuntu/Debian (and not using devstack with the USE_PYTHON flag set), then you'll need the following packages installed to run the py34 tox unit test targets in the projects:

sudo apt-get install python3.4 python3.4-dev

sixer tool

The sixer tool helps to replace most basic patterns to add Python 3 compatibility and it respects OpenStack coding style.

Common patterns

Note: Replacing dict.iteritems()/.itervalues() with six.iteritems(dict)/six.itervalues(dict) was preferred in the past, but there was a discussion suggesting to avoid six for this. The overhead of creating a temporary list on Python 2 is negligible.

Serialization: base64, JSON, etc.

Hexadecimal:

JSON:

contextlib.nested

To replaced contextlib.nested is to use contextlib.ExitStack. It's available on Python 2 using contextlib2.ExitStack. For contextlib.nested, nova/test.py defines:

if six.PY3: @contextlib.contextmanager def nested(*contexts): with contextlib.ExitStack() as stack: yield [stack.enter_context(c) for c in contexts] else: nested = contextlib.nested

Another option to replaced contextlib.nested is to use the @mock.patch decorator. Example with nested function:

def test_thing(self): @mock.patch(...) @mock.patch(...) @mock.patch(...) def do_test(...): ...

do_test()

More options:

oslo_utils.encodeutils.to_utf8

oslo.utils 3.5 has an oslo_utils.encodeutils.to_utf8() function to encode Unicode to UTF-8 and return bytes unchanged.

bytes.decode and unicode.encode

Python has a notion of "default encoding": sys.getdefaultencoding(). On Python 2, the default encoding is ASCII, whereas it is UTF-8 on Python 3.

Don't write data.decode() or text.encode() without parameter, because you will use a different encoding on Python 2 and Python 3.

Use an explicit encoding instead. Example: data.decode('utf-8') or text.encode('utf-8'). The right encoding depends on the use case, but UTF-8 is usually a good candidate (it is a superset of ASCII).

safe_decode

Olso Incubator has a function safe_decode() which can be used to decode a bytes string and pass text strings unchanged.

The default encoding is sys.stdin.encoding or sys.getdefaultencoding():

It's safer to explicit the encoding to not rely on the locale encoding and have the same behaviour even if sys.stdin is "mocked".

Safe usage:

Unsafe usage:

By default, the decoder is strict. You can specify a different error handler using the optional errors parameter. Example: safe_decode(b'[\xff]', 'ascii', 'ignore') returns '[]'.

safe_encode

Olso Incubator has a function safe_encode() which can be used to encode a string. Its usage is tricky and you should understand how it works and which encodings are used.

The default input encoding (incomding parameter) is sys.stdin.encoding or sys.getdefaultencoding():

The default output encoding (encoding parameter) is UTF-8.

It's safer to explicit the input encoding to not rely on the locale encoding and have the same behaviour even if sys.stdin is "mocked".

Safe usage:

Unsafe usage:

Example:

By default, the encoder and the decoder are strict. You can specify a different error handler using the optional errors parameter. Example: safe_encode(b'[\xff]', incoming='ascii', errors='ignore') returns b'[]'.

logging module and format exceptions

The exception_to_unicode(exc) function of oslo_utils.encodeutils is the recommanded way to format an exception to Unicode. This function works on Python 2 and Python 3 and it should avoid mojibake is most cases.

On Python 2, the logging module accepts bytes and text strings. On Python 3, it only accepts text strings. For example, logging.error(b'hello') logs b'hello' instead of 'hello'.

There is no clear rule for format exceptions yet. There are different choices depending on the project:

Since logging functions expect text strings on Python 3, logged exceptions should be formatted using str(exc). Example: LOG.debug(str(exc)).

HTTP

The HTTP protocol is based on bytes:

References to port Python 2 code to Python 3

Common pitfalls

What is a string ?

You should definitely not talk about "strings" in your commit logs/reviews. In Python 2, a 'string' is bytes; in Python 3, it's a Unicode text string. The following code snippet may help in understanding the difference:

Python 2:

type('foo') <type 'str'> type(u'foo') <type 'unicode'> type(b'foo') <type 'str'> isinstance('foo', six.text_type) False isinstance(u'foo', six.text_type) True bytes is str True b'foo'[0] 'f'

Python 3:

type('foo') <class 'str'> type(u'foo') <class 'str'> type(b'foo') <class 'bytes'> isinstance('foo', six.text_type) True isinstance(b'foo', six.text_type) False bytes is str False b'foo'[0] 102

tox/testr error: db type could not be determined

The "db type could not be determined" error comes from .testrepository/times.dbm used by testr.

Workaround: "rm -rf .testrepository/" and then run "tox -e py34" before running "tox -e py27". You only have to do this once. The problem only occurs with "tox -e py34" when .testrepository/ was created by Python 2.

Enable Python 3 in DevStack

sudo dnf install python3-devel sudo python3 -m pip install python-memcached # needed by keystonemiddleware

USE_PYTHON3=True

See Switch to python 3.5.

Functional and Integration Tests

Etherpad: https://etherpad.openstack.org/p/support-python3.5-functional-tests

Doug Hellmann wrote a cross-project specification to run integration tests on Python 3: Enabling Python 3 for Application Integration Tests (spec approved for Liberty).

Patch for DevStack: Enable optional Python 3 support.

Projects wanting to run their existing functional test suite against their services on Python 3 should:-

Then run tox -e. Example:- tox -efunctional to run the functional the tests of a certain project.

Once the functional test suite is reliably passing against the service in a python 3.5 env, the trove classifiers must be updated along with a new gate job being added to the project.

Sirushti Murugesan's attempt on Heat: https://review.openstack.org/#/c/188033/

Python 3.5

Python 3.6 status

Python 3.6 is scheduled to be the default/only version of Python available in Ubuntu 18.04 LTS (Bionic Beaver) and Debian 10 (Buster).

Known issues:

Status of Python 3 in Linux distributions

Check the Project Testing Interface for updates about the platforms each release is tested on.

Status of Python 2 Support Being Dropped in Dependencies

OpenStack plans to start dropping Python 2 support at the start of the U cycle

Progress reports, meetings and sprints

Articles

Pycon Montreal 2014: Sprint Port OpenStack to Python 3

Enovance organized a sprint to Port OpenStack to Python 3 during 4 days: between April, 14 (Monday) and April, 17 (Thursday) 2014. See the page Python3/SprintPycon2014.