Python equivalent to "include" paths

961 views
Skip to first unread message

glin...@gmail.com

unread,
Dec 2, 2015, 4:43:51 PM12/2/15
to bazel-discuss
Since bazel is already setting up a wrapper around python binaries when launching, it would be great to have an equivalent to the C++ include paths. This would make it nicer to work with dependent libraries imported into the source tree.

ie.
/third_party/bar/BUILD - an imported library, wrapped in py_library
/third_party/foo/BUILD - an imported library, wrapped in py_library, depends on bar

I now have to fix up all references to bar inside of foo. ie. "import bar" becomes "import third_party.bar". If there aren't good unit tests in the library, this becomes a fairly error prone process.

Is this possible in the current system?

Thanks!

Damien Martin-guillerez

unread,
Dec 2, 2015, 4:48:21 PM12/2/15
to glin...@gmail.com, bazel-discuss
Do you mean to add third_party to the PYTHONPATH?

--
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/dcf39bcd-77c3-4072-973e-e55fff629f26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

glin...@gmail.com

unread,
Dec 2, 2015, 4:58:05 PM12/2/15
to bazel-discuss, glin...@gmail.com
On Wednesday, December 2, 2015 at 1:48:21 PM UTC-8, Damien Martin-guillerez wrote:
> Do you mean to add third_party to the PYTHONPATH?
>
Yes, that's possible. Having it happen from the rule (similar to the includes in cc_library) would be nicer, as then I don't have to have some outside setup step. It looks like there is already a fair bit of infrastructure in python binaries around the runfiles -- that's what led me to think there might be some way to do this.

Austin Schuh

unread,
Dec 2, 2015, 6:56:10 PM12/2/15
to glin...@gmail.com, bazel-discuss
On Wed, Dec 2, 2015 at 1:58 PM <glin...@gmail.com> wrote:
On Wednesday, December 2, 2015 at 1:48:21 PM UTC-8, Damien Martin-guillerez wrote:
> Do you mean to add third_party to the PYTHONPATH?
>
Yes, that's possible.  Having it happen from the rule (similar to the includes in cc_library) would be nicer, as then I don't have to have some outside setup step.  It looks like there is already a fair bit of infrastructure in python binaries around the runfiles -- that's what led me to think there might be some way to do this.

I've been looking for this as well!  Whenever we use the python protobuf library checked into third_party in our repo, we have to modify PYTHONPATH in each python file that includes it to add it so that things will load.  Having a way to get it added automatically to PYTHONPATH would be amazing.

Austin

Benjamin Staffin

unread,
Dec 8, 2015, 5:28:05 PM12/8/15
to bazel-discuss, glin...@gmail.com
I've had some luck with creating a third_party/__init__.py file with the following contents:

import os
import sys
sys
.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'py')
del os
del sys

Then in my code I just "import third_party" before importing any libraries from third_party/py/ and I'm good to go:

#!/usr/bin/env python
import third_party
import thing.inside.third.party

If there's a better way of doing this, I'd love to hear about it.  I really want to have better python support in Bazel in general, and am willing to put some effort into making it happen.

Brian Silverman

unread,
Dec 10, 2015, 12:26:48 AM12/10/15
to Benjamin Staffin, bazel-discuss, Gary Linscott
I agree that having better Bazel support for this would be nice. Maybe something like pkg_tar's strip_prefix?

For now, I was having issues with something similar to that sys.path manipulation preferring .py files from /usr/, plus having to "import third_party" first is annoying. To fix this, I created an external repository with a thing/__init__.py file that looks like this:
import sys
import third_party.thing.inside
sys.modules['thing.inside'] = third_party.thing.inside

Including a py_library with thing/__init__.py in a py_binary works nicely AFAIK ("import thing.inside.third.party" just works and the library importing parts of itself works too).

I went with a new_http_archive using an empty archive and a genrule in the .BUILD file to generate thing/__init__.py to avoid managing versions of something external, but any of the external repository types should work.

--
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.
Reply all
Reply to author
Forward
0 new messages