I am trying to get up and running with AWS Lambda Python (beginner in Python btw) but having some problems with including MySQL dependency. I am trying to follow the instructions here on my Mac.
For step number 3, I am getting some problems with doing the command at the root of my project
sudo pip install MySQL-python -t /
Error:
Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 311, in run os.path.join(options.target_dir, item) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 292, in move raise Error, "Destination path '%s' already exists" % real_dst Error: Destination path '/MySQL_python-1.2.5-py2.7.egg-info/MySQL_python-1.2.5-py2.7.egg-info' already exists
I end up writing my following lambda function (works fine on my Mac), which is:
import MySQLdb
def lambda_handler(event, context):
# Open database connection
db = MySQLdb.connect(...)
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM Users"
try:
# Execute the SQL command
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
for row in results:
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# Now print fetched result
print ("lname=%s" %(lname))
except:
print "Error: unable to fecth data"
# disconnect from server
db.close()
What I went on to do is go to /Library/Python/2.7/site-packages and copying over the the MySQLdb folders/files that were downloaded when I did sudo pip install MySQL-python (without -t /) (I'm sure I'm doing something wrong here), to my lambda project, and then zipped the content along with the lambda_function.py and uploaded to AWS Lambda.
Then I get:
Unable to import module 'lambda_function': No module named MySQLdb
Grateful for any help and suggestions!
EDIT
Was able to do make sudo pip install MySQL-python -t /pathToProject work (thanks for the help in the comments) but now I get this when runing the lambda function:
Unable to import module 'lambda_function': /var/task/_mysql.so: invalid ELF header
I know that if I work on a Linux box, then it should work fine (as suggested by some people), but I am wondering if I can make it work from an OS X box.
python -c "import sys; print(sys.path)"
? Why are you using the system python? Haven't you installed one in your/usr/local
path with homebrew or macports? If not, you should. You will not need to usesudo
for pip because it will be owned by your own user and won't be changing any system owned files. Also, it will be completely seperate from the python that your system depends on. It will also be a newer version, since it's from a repository. – Currencurl
instruction fromhttp://brew.sh
and then runbrew install python
pip install MySQLdb
. Verify that your python is in /usr/local/bin by runningwhich python
-- homebrew will have set your $PATH variable to look in/usr/local/
to find the correct python. – Curren