bobby
bobby

Reputation: 189

boto3 import fails python3.4

I am trying to come up with python script, to get instance's Tag key AutoScalingGroupName value of the instance. I already installed boto3 using pip3.

from python3.4 shell I was able to import boto3 successfully.

#python3.4
Python 3.4.3 (default, Jul  8 2016, 11:37:17)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> exit()

but the script fails with this error.

#python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import boto3
ImportError: No module named boto3
#

here is the content of the test.py

# cat test.py
#!/bin/python3.4
import requests
import boto3
import botocore.session
import urllib.request

instid = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read().decode()
autossession = botocore.session.Session(profile='auto1')
ec2 = boto3.resource('ec2', region_name='us-west-2')
autosval = ec2.describe_instances(InstanceIds={instid}, Filters={"tag:AutoScalingGroupName"})
print(autosval)
#

Upvotes: 2

Views: 4437

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269091

It appears that your problem is caused by running different versions of Python from the command line vs scripts.

This is quite common when there are multiple versions of Python (and its libraries) installed on a system.

I, too, have experienced problems when installing python libraries and utilities via the sudo command (eg pip), but then having different paths between users so different versions would run.

Fortunately, the Python community has created Virtualenv, which allows multiple versions of Python to be installed, each within its own environment. This also allows the installation of libraries without requiring sudo access.

If you're still experiencing version mismatches, give virtualenv a try!

Upvotes: 3

Related Questions