Is there a way to deal with cases where there are ...
# general
c
Is there a way to deal with cases where there are two third-party libraries being used, but both have the same name? In my project, I use both the
google-api-python-client
and
google-auth
libraries. In Python code, they're both referred to as
google
, for example:
Copy code
from google import auth
from google import api_core
I noticed that I can't specify two separate
module_mappings
where the values are the same, otherwise the build system gets confused...
f
In this case, you will have to provide explicit dependencies on the
python_sources
targets for the code that is importing those modules.
(For context, the
module_mappings
feature is part of the Pants “dependency inference” feature which tries to automatically infer dependencies. In cases where it cannot, you can still use the
dependencies
field on a target to explicitly set the dependency.)
Are you using a
python_requirements()
target?
If so, then the address for each dependency will be the address of the
python_requirements
plus
#
then the name of the Python module.
e.g.
3rdparty/py#google-api-python-client
if the file
3rdparty/BUILD
had a
python_requirements(name="py")
target
c
Ahh, I realized my issue was related to something else altogether. It ended up being due to
path/to/source.py
having a dependency on
path/__init__.py
, but the dependency not being inferred. So I went ahead and added the following explicitly.
Copy code
python_sources(
    dependencies=["path/__init__.py"]
)
b
Actually this is supported by
module_mappings
!
module_mappings
allow for any valid package name, so in your case
google-api-python-client
should map to
google.api_core
and likewise fgor
google-auth
Ahh, I realized my issue was related to something else altogether. It ended up being due to
path/to/source.py
having a dependency on
path/__init__.py
, but the dependency not being inferred.
This shouldn't be necessary. Do your inits have contents? If so you might want to set https://www.pantsbuild.org/docs/reference-python-infer#section-inits
c
Ah awesome, I didn't know https://www.pantsbuild.org/docs/reference-python-infer#section-inits was an option! That should hopefully prevent me from having to manually specify it. Before I do so, are there any downsides of enabling this feature? We have some instances where there is logic in
__init__.py
files, but we're trying to migrate off of it...
b
Not really other than possible noise. (As far as I know). In Pants 2.13 you'll have https://github.com/pantsbuild/pants/pull/15397 which should pretty much always "do the right thing"
Thanks @hundreds-father-404 🏆
❤️ 1