Using two Python libraries with conflicting names

I want to use two Python libraries (Google Cloud Library and their Cloud SDK ) in one application, but they have conflicting names (they both use google in their base import names and do not use relative imports within themselves). How can I use them in one application?

Changing the library code to use proper relative imports is impractical. Also, I know that I can use virtualenv to access these libraries from individual python applications, but how do I access them from the same python application?

Naming Conflict Details

Here are some of the import details. When I import a module from the Cloud Library (I run import google.cloud.datastore ), there is an exception due to another import inside this library:

 >>> import libs.google.cloud.datastore Traceback (most recent call last): File "<console>", line 1, in <module> File "C:\[ProjectDIR]\libs\google\cloud\datastore\__init__.py", line 52, in <module> from google.cloud.datastore.batch import Batch ImportError: No module named cloud.datastore.batch 

The library is trying to do absolute imports, not relative ones. The reason the Google cloud library cannot import google.cloud.datastore.batch is because google already defined in the SDK, there is a name conflict:

 >>> print google.__path__ ['C:\\Program Files (x86)\\Google\\Cloud SDK\\google-cloud-sdk\\platform\\google_appengine\\google'] 

Since the cloud library uses absolute imports, and the google name is already defined in the SDK, import is not possible.

+7
python google-app-engine virtualenv python-import google-cloud-platform
source share
1 answer

google packages must register as a namespace package. If sys.path configured sys.path there is no conflict here.

You need to properly configure the library environment. Add the appengine_config.py file to the root of your project:

 from google.appengine.ext import vendor # Add any libraries installed in the "lib" folder. vendor.add('lib') 

This adds the lib subdirectory to the right place sys.path . See Installing a Third-Party Library Section in Python Application Development Applications on How-To App Engine.

From here, google.cloud imports just work:

 $ ls -1d lib *.py *.yaml app.yaml appengine_config.py lib main.py $ pip install -t lib google-cloud # installing into the lib subdirectory $ cat main.py import google from google.cloud import datastore from google.appengine.api import memcache import os.path here = os.path.dirname(os.path.abspath(__file__)) def app(*args, **kwargs): return ''' google: {}<br /> google.cloud.datastore: {}<br /> google.appengine.api.memcache: {}'''.format( os.path.relpath(google.__file__, here), os.path.relpath(datastore.__file__, here), os.path.relpath(memcache.__file__, here)) 

and in the browser I am served:

 google: ../google-cloud-sdk/platform/google_appengine/google/__init__.py google.cloud.datastore: lib/google/cloud/datastore/__init__.pyc google.appengine.api.memcache: ../google-cloud-sdk/platform/google_appengine/google/appengine/api/memcache/__init__.pyc 
+5
source share

All Articles