Search code examples
pythonpython-3.xbotords

TypeError: the JSON object must be str, not 'bytes' when using the bash shell to execute. When using python interpreter the code works fine


#!/usr/bin/env python3    
import boto.rds2    
rds2_conn=boto.rds2.connect_to_region(region_name="us-east")
cs= rds2_conn.describe_db_instances(db_instance_identifier=None)
print(cs)

I want to get all the running instances in a AWS environment. I am using python 3.3. i had used boto.rds earlier which has a method called rds2.get_all_dbinstances(). But i am migrating now to rds2. when i try to run the above code using python interpreter usage :python filename.py : i get no error. the program works fine. But using Bash shell usage :./filename.py : i get the error :

TypeError: File "/Users/heninkarkada/Documents/repositories/jaws/b.py", line 5, in <module>
    raw=con.describe_db_instances(db_instance_identifier=None)
  File "/usr/local/lib/python3.4/site-packages/boto/rds2/layer1.py", line 1512, in describe_db_instances
    path='/', params=params)
  File "/usr/local/lib/python3.4/site-packages/boto/rds2/layer1.py", line 3764, in _make_request
    return json.loads(body)
  File "/usr/local/Cellar/python3/3.4.1_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'the JSON object must be str, not 'bytes' at line no:3

Can anyone tell me why this thing is happening? Advance thanks


Solution

  • The shebang #!/usr/bin/env python3 might be incorrect if the script is actually Python 2 script, not Python 3 script. json accepts bytestrings in Python 2. In Python 3, the input must be Unicode string.

    The json code that fails is inside boto package. boto's setup.py claims Python 3 support but It is possible that boto scripts are incompatible with Python 3 yet.