1

I've struggle with using MySQLdb. In my project (Python 2.7) I had:

import MySQLdb

In responce: ImportError: No module named MySQLdb.

app.yaml:

libraries: 
- name: MySQLdb
  version: latest

I found that MySQLdb is a part of predefined standard modules. Yet still I couldn't solve it. When

pip install -t /lib MySQLdb

the responce was:

Collecting MySQLdb

Could not find a version that satisfies the requirement MySQLdb (from versions: ) No matching distribution found for MySQLdb

So I followed some tuts and did like that:

sudo apt-get install python-mysqldb

while installing I've got:

You are running apt-get inside of Cloud Shell. Note that your Cloud Shell machine is ephemeral and no system-wide change will persist beyond session end.

Indeed these changes persisted for session span only.

Any comments, suggestions? Do I do smth. wrong?

Igor Savinkin
  • 5,669
  • 8
  • 37
  • 69

1 Answers1

1

The app.yaml file is only used for your application when deployed on App Engine. It specifies some configuration for your deployed app only. So by defining:

libraries:
- name: MySQLdb
  version: "latest"

it basically instructs the App Engine platform to use that built-in library on the instance to run your code.

Now, as explained here, on your local machine for development, you need to install the library yourself. You can do that by running:

pip install MySQL-python

Note that you don't have to install this library also in the lib folder. This is needed only if the library isn't listed in the Built-in Third-party Libraries so that the library is uploaded along with your app when you deploy it. If it is in the list then referencing it in the app.yaml file is enough.

One last thing about the Cloud Shell. It is a small VM instance provided to you to ease resource management or testing. It isn't meant for development. Every time it starts up a default image is installed (thus any library or OS level tool you installed will be lost). Only your home folder is copied over. Some useful tools are installed by default though on the image like gcloud.

LundinCast
  • 9,412
  • 4
  • 36
  • 48
  • MySQLdb is a [built-in library](https://cloud.google.com/appengine/docs/standard/python/tools/built-in-libraries-27), it will not be installed via pip. – Dustin Ingram Sep 12 '18 at 15:56
  • 1
    I wanted to make a comparison with how it'd work locally, although it doesn't actually use pip. I've updated my answer. – LundinCast Sep 12 '18 at 16:04
  • @LundinCast, thanks for clarifying. I develop locally and it works. Yet since I need to use in the project both Cloud SQL and Cloud Storage I need to attune the project with those settings (of GCP). I used that [tut](https://cloud.google.com/appengine/docs/flexible/python/using-cloud-sql?authuser=2&hl=ru#setting_connection_strings_and_adding_a_library) but it fails... – Igor Savinkin Sep 13 '18 at 07:30