juju / charm-helpers

Apache License 2.0
18 stars 127 forks source link

cannot use charmhelpers.core.hookenv on python2 due to addition of enum import #500

Open afreiberger opened 4 years ago

afreiberger commented 4 years ago

After refreshing charmhelpers on a python2 charm, I ran into issues during import of hookenv and host due to bare import of enum (available in python3.4+)

...
install Setting up python-six (1.14.0-2) ...
install Traceback (most recent call last):
install   File "hooks/hooks.py", line 14, in <module>
install     from charmhelpers.core.host import (
install   File "/var/lib/juju/agents/unit-rsyslog-forwarder-ha-0/charm/hooks/charmhelpers/core/host.py", line 37, in <module>
install     from .hookenv import log, INFO, DEBUG, local_unit, charm_name
install   File "/var/lib/juju/agents/unit-rsyslog-forwarder-ha-0/charm/hooks/charmhelpers/core/hookenv.py", line 24, in <module>
install     from enum import Enum
install ImportError: No module named enum

I've updated my code to run on python3 to workaround this. Is there a requirement for python3 for v0.20.16?

Is there a stable charm-helpers tagged release that supports py2?

afreiberger commented 4 years ago

introduced by PR #456

ajkavanagh commented 4 years ago

This is a bit odd. I just did this in py2 on a bionic host:

Python 2.7.18rc1 (default, Apr  7 2020, 12:05:55)
Type "copyright", "credits" or "license" for more information.

IPython 5.10.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import charmhelpers.core.hookenv

In [2]: ls
bin/           debian/  HACKING.md  Makefile     py2venv/    requirements.txt  setup.cfg  tarmac_tests.sh*       tests/  tox.ini
charmhelpers/  docs/    LICENSE     MANIFEST.in  README.rst  scripts/          setup.py   test-requirements.txt  tools/

In [3]: import enum

In [4]:

Is it an old py2 version?

afreiberger commented 4 years ago

I don't still have my artifacts, but it may have been xenial and/or trusty. I ran into it during pretty standard deployment during zaza testing of lp:charm-rsyslog-forwarder-ha.
https://git.launchpad.net/~afreiberger/charm-rsyslog-forwarder-ha/commit/?id=ad9186b5c9fcce2d386d2459da6d03684330f8a7

There's not an automated test, but this commit above updated charmhelpers and broke trusty and possibly xenial support for the charm due to the enum.

Sorry for lack of more sufficient info.

afreiberger commented 4 years ago

Okay, my notes show that the way my program was written was using /usr/bin/env python and breaking in focal on the charmhelpers import. But I also had found that my install hook was installing py2 packages python-six, python-sqlalchemy, etc instead of detecting python version. So, it may be that I was running into complicated python versioning issues crossing over into supporting focal with assumption that /usr/bin/env python was py2.

Eventually, I just rewrote as py3 and dropped trusty support.

aieri commented 4 years ago

This is a bit odd. I just did this in py2 on a bionic host:

Python 2.7.18rc1 (default, Apr  7 2020, 12:05:55)
Type "copyright", "credits" or "license" for more information.

IPython 5.10.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import charmhelpers.core.hookenv

In [2]: ls
bin/           debian/  HACKING.md  Makefile     py2venv/    requirements.txt  setup.cfg  tarmac_tests.sh*       tests/  tox.ini
charmhelpers/  docs/    LICENSE     MANIFEST.in  README.rst  scripts/          setup.py   test-requirements.txt  tools/

In [3]: import enum

In [4]:

Is it an old py2 version?

@ajkavanagh you might have package python-enum34 installed (which backports enum for py2). I've run into this as well when developing on my workstation: everything looked good until I deployed the charm on a fresh bionic unit ;)

ajkavanagh commented 4 years ago

@ajkavanagh you might have package python-enum34 installed (which backports enum for py2). I've run into this as well when developing on my workstation: everything looked good until I deployed the charm on a fresh bionic unit ;)

That's an interesting point. I tested it on a fresh lxc bionic and it's not installed. Good catch!