Docker Community Forums

Share and learn in the Docker community.

Python-dockercloud environment variables unusable in Python 3+

There is an encoding issue with environment variables in Python 3+. Here’s the traceback I get from just importing dockercloud with variables set:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-6deea90486e3> in <module>()
----> 1 import dockercloud
/home/xxx/.virtualenvs/cron/lib/python3.5/site-packages/dockercloud/__init__.py in <module>()
     34     basic_auth = base64.b64encode("%s:%s" % (os.environ.get('DOCKERCLOUD_USER'), 
os.environ.get('DOCKERCLOUD_PASS')))
     35 if os.environ.get('DOCKERCLOUD_USER') and os.environ.get('DOCKERCLOUD_APIKEY'):
---> 36     basic_auth = base64.b64encode("%s:%s" % (os.environ.get('DOCKERCLOUD_USER'), 
os.environ.get('DOCKERCLOUD_APIKEY')))
     37 
     38 rest_host = os.environ.get("DOCKERCLOUD_REST_HOST") or 'https://cloud.docker.com/'
/home/xxx/.virtualenvs/cron/lib/python3.5/base64.py in b64encode(s, altchars)
     57     """
     58     # Strip off the trailing newline
---> 59     encoded = binascii.b2a_base64(s)[:-1]
     60     if altchars is not None:
     61         assert len(altchars) == 2, repr(altchars)

TypeError: a bytes-like object is required, not 'str'

I guess the problem is that in Python 2 str and bytes are the same object type but not in Python 3.

Actually Python 3 seems completely unusable in this regard. Also with just setting the values on dockercloud.user and dockercloud.apikey. Very frustrating seeing how long Python 3 has been around.