Importing python oauth2client from third_party

967 views
Skip to first unread message

sandeep aggarwal

unread,
Jun 1, 2015, 11:40:25 AM6/1/15
to bazel-...@googlegroups.com
Hi,

I am trying to import oauth2client module in admin module. This is how my current directory structure look like :-
.
├── admin
   ├── admin.py
   ├── admin.yaml
   ├── BUILD
   ├── BUILD.oauth2client
    |── third_party
   │   └── py
   │       ├── oauth2client
   │       └── six
   └── WORKSPACE

As suggested in "importing third_party" example in bazel docs, i have written following build files.

BUILD:
py_binary(
    name="admin",
    srcs=[
        "admin.py",
    ],
    deps=[
        "//external:oauth2",
    ],
    data=[
        "admin.yaml",
    ],
)

BUILD.oauth2client
py_library(
    name="oauth2client",
    srcs=glob([
        "*.py",
    ]),
    deps=[
        "//third_party/py/six",
    ],
    visibility = ["//visibility:public"],
)

WORKSPACE
new_local_repository(
    name = "oauth2client",
    path = "/home/asandeep/workspace/bhojo/apps/ganesh/gae/admin/third_party/py/oauth2client/oauth2client",
    build_file = "BUILD.oauth2client",
)

bind(
    name = "oauth2",
    actual = "@oauth2client//:oauth2client",
)


bazel build :admin command executes successfully and maps oauth2client directly correctly to bazel-bin/external/oauth2client.

However, in admin.py when i try to import oauth2client as: 

from oauth2client.oauth2client import client

i get below error:

ImportError: No module named oauth2client

Is there anything that i am missing. Any pointers will be really appreciated.

Thanks

Kristina Chodorow

unread,
Jun 1, 2015, 1:36:56 PM6/1/15
to sandeep aggarwal, bazel-...@googlegroups.com
Note that you don't need to make this an external dependency.  You can simply refer to it in your py_binary:

py_binary(
    name="admin",
    srcs=["admin.py"],
    deps=["//third_party/py/oauth2client"],
    data=["admin.yaml"],
)

Either way, you need to import it as third_party.py.oauth2client (or external.oauth2).  The PYTHONPATH Bazel generates only includes the top-level directory.




--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/f8be6708-00e2-4a6e-a30e-a59104aadb7d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sandeep aggarwal

unread,
Jun 2, 2015, 12:11:33 PM6/2/15
to bazel-...@googlegroups.com, sand...@gmail.com
Thanks for your response Kristina.

Like you said, i can import as third_party.py.oauth2client (or external.oauth2client), and that works inside admin.py, However, this fails when client.py tries to import other modules from same package.


Exact trace:
Traceback (most recent call last):
  File "/home/asandeep/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/asandeep/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/asandeep/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/home/asandeep/.cache/bazel/_bazel_asandeep/081a636f92339453ceab5324de4a2600/admin/bazel-out/local_linux-fastbuild/bin/admin.runfiles/admin.py", line 6, in <module>
    from external.oauth2client import client
  File "/home/asandeep/.cache/bazel/_bazel_asandeep/081a636f92339453ceab5324de4a2600/admin/bazel-out/local_linux-fastbuild/bin/admin.runfiles/external/oauth2client/client.py", line 36, in <module>
    from oauth2client import clientsecrets
ImportError: No module named oauth2client


I think this can be resolved if bazel maps external libraries to root instead of external/, though i am not sure how to do it.

Kristina Chodorow

unread,
Jun 2, 2015, 1:23:23 PM6/2/15
to sandeep aggarwal, bazel-...@googlegroups.com
I think you'll need to append the paths that you want to the PYTHONPATH, e.g., 

import sys
sys.path.append("third_party/py/oauth2client")

sandeep aggarwal

unread,
Jun 3, 2015, 11:19:09 AM6/3/15
to bazel-...@googlegroups.com, sand...@gmail.com
That worked..

Thanks a lot Kristina.. :)
Reply all
Reply to author
Forward
0 new messages