Hi all - I'm new to pants, and not an engineer by ...
# general
c
Hi all - I'm new to pants, and not an engineer by profession in general with no experience in build systems so sorry for struggling with some possibly very simple pants concepts. The build system was setup by an someone who no longer works at the company so I'm trying to reverse engineer some pants ideas from reading the docs, but would appreciate your help :) Currently Pants is used for a Django project and I've been able to successfully create the models, build the tables, do migrations, runserver etc. Now I'm trying to use a
populate_regions.py
file to fill one of the tables from a csv file. Normally the Django command for this would be:
Copy code
python manage.py populate_regions country_codes_and_regions.csv
Since with the other commands I've been using lines like:
Copy code
NAME_SQLITE_DB=db.sqlite3 pants run src/sillion/service/modelling:manage -- migrate
I tried something similar (without a lot of hope of this working)
Copy code
NAME_SQLITE_DB=db.sqlite3 pants run src/sillion/service/modelling:manage -- populate_regions country_codes_and_regions.csv
This presents an unknown commad error. So trying to understand how the manage.py entry point works in general and how I could run commands such at that one above? The BUILD file at project level
Copy code
python_sources(
    overrides = {
        "settings.py": {
            "dependencies": [
                "3rdparty/python#django-simple-history",
                "3rdparty/python#djangorestframework",
                "3rdparty/python#requests-cache",
                "//src/Companies",
                "//src/User",
                "//src/Sources",
                "//src/Scenarios",
                "//src/Vehicles",
            ]
        }
    }
)

pex_binary(
    name="manage",
    entry_point="manage.py",
    layout="packed",
    env={
        "NAME_DB_SQLITE": "",
    }
)
Also what is the significance of having those BUILD files at the app level and the top project level? Thanks!
h
Hi, no worries, this can be non-trivial even for experienced engineers.
Getting this right requires understanding something of Pants and a lot of Django...
So if
python manage.py populate_regions
works but
pants run src/sillion/service/modelling:manage -- populate_regions
doesn't then it means that when run under Pants, Django is not "seeing" the Django app that provides the custom "populate_regions" management command.
And indeed it looks like your
manage
binary doesn't have an explicit dependency on the
python_sources
target that provides the settings.py
1
So let's start by adding that dependency explicitly
"dependencies": [":modelling"]
I think
c
Thanks for the guidance here - you were right it was about providing the correct path. Modified the above a bit to reflect my directory structure but the final binary looked like:
Copy code
pex_binary(
    name="manage",
    entry_point="manage.py",
    dependencies=["src/Vehicles/management/commands/populate_regions.py"],
    layout="packed",
    env={
        "NAME_DB_SQLITE": "",
    }
)
So kinda had to direct right up until the .py file otherwise the command not found error persists. after this change could run
Copy code
NAME_SQLITE_DB=db.sqlite3 pants run src/sillion/service/modelling:manage -- populate_regions country_codes_and_regions.csv
The table is updated exactly as expected 🙂
h
Great! I would expect settings.py to also be in the dependencies somewhere for all that to work, but presumably it's getting pulled in transitively