Pants TIP of the day: When you’re working with “m...
# general
c
Pants TIP of the day: When you’re working with “magic” libraries, such as Django, there are times where you have 3rd party dependencies that are not inferred as they don’t materialize in your 1st party code. At times there are hints to use, though, as shown in this little example. Say we have a module, that uses the postgresql feature:
Copy code
import django.contrib.postgres
and that module in turn uses
psycopg2
but it isn’t inferred nor provided as a dependency of Django itself as it’s optional. You can use the module mapping feature in Pants to your advantage in this case, with a snippet like this:
Copy code
python_requirements(
    module_mapping={
        "psycopg2": [
            "psycopg2",
            # This is for dependency inference only, when we see an import for
            # `django.contrib.postgres` we should infer a dependency on `psycopg2`.
            "django.contrib.postgres",
        ],
    },
...
Pants will properly infer the dependency for you correctly. 🎉
🎉 1
b
Oh funny
h
Nice hack!
b
Nice, and I like this idea of sharing tips here to help others. I hope we see more tips here in the future.
Re this tip itself, should we work this into the docs?
b
Not a bad idea. Dedicated tips and tricks page
b
Or in the section on troubleshooting?
b
These seem different than troubleshooting to me. But thats me
b
c
👍
b
@curved-television-6568 do you have time to integrate it into the docs?
c
sure, I can look at that later this week.
b
Thanks! If you find you don't have time, gimme a ping and I'll put it on my todos instead.
🙏 1
r
Nice I was just about to ask for this feature because I didn't see it in the docs 🙌
b
@rough-vase-83553 where would have been a good place for you to spot it as a newcomer, and what search terms failed to turn up this solution for you? Let's take this as an opportunity to improve the docs!
r
This topic is definitely right since the build warnings direct you there when a dep can't be resolved: https://www.pantsbuild.org/docs/troubleshooting#import-errors-and-missing-dependencies