In a non-pants environment, if you are teaching so...
# general
c
In a non-pants environment, if you are teaching someone Django you would tell them to run the following:
Copy code
pip install Django==3.2
django-admin startproject mysite
Trying to map this over to a fresh pants environment. If I add
Django==3.2
to my
requirements.txt
is there some light weight way I could run the
django-admin
script contained in that package through the pants CLI?
h
For most uses,
django-admin
is the same as
manage.py
(except that
manage.py
also sets the DJANGO_SETTINGS_MODULE), so normally I'd say create a pex_binary around the manage.py and
./pants run
it. But in this case there is no
manage.py
yet, so I guess we have to run
django-admin
itself?
Just trying to get to the underlying issue
c
I figured out a django specific solution but just wondering if there is a generic solution for running a binary script (?) in a python package throuhg pants
FWIW I created
src/django-admin.py
Copy code
"""
Invokes django-admin when the django module is run as a script.

Example: python -m django check
"""
from django.core import management

if __name__ == "__main__":
    management.execute_from_command_line()
Then I can run
./pants run src/django-admin.py -- startproject mysite
h
@clean-city-64472 have you seen the new console-script support? https://www.pantsbuild.org/v2.8/docs/reference-pex_binary#codescriptcode
c
Is this suggesting if you had a python_binary target that included Django in it, you could set this script property to
django-admin
and it would find that script inside of the Django package and run it?
h
Yep! Whereas
entry_point
is for your own code,
script
can be set to third-party entry points. Like to build a PEX that runs Black, you could set
script="black"
New feature in Pants 2.8
c
interesting -i'll try that out
h
Running it will be a little weird though, as there's no associated file to run..So file args don't work, and you have to use the target's address. You'd have to do
./pants run //:django-admin
(or whatever the path to the
pex_binary
target is, like
utils:django_admin_pex
) One possible solution to that: the new
[cli.alias]
feature added by @curved-television-6568 this week! https://github.com/pantsbuild/pants/pull/13228 You can set up an alias so that
./pants django-admin
expands out to
./pants run //:django-admin
, and your users don't have to think about what a target is etc
👍 1
👀 1
c
I just had to try this out, and it turned out really well…
BUILD
Copy code
python_requirement(
    name="django",
    requirements=["Django"]
)

pex_binary(
    name="django-admin",
    script="django-admin",
    dependencies=[":django"],
)
Then:
Copy code
$ ./pants run :django-admin

Type 'django-admin.pex help <subcommand>' for help on a specific subcommand.

Available subcommands:

[django]
    check
    compilemessages
    createcachetable
    dbshell
    diffsettings
    dumpdata
...
❤️ 2
Copy code
$ ./pants run :django-admin -- help check
usage: django-admin.pex check [-h] [--tag TAGS] [--list-tags] [--deploy] [--fail-level {CRITICAL,ERROR,WARNING,INFO,DEBUG}] [--database DATABASES] [--version] [-v {0,1,2,3}]
                              [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
                              [app_label [app_label ...]]

Checks the entire Django project for potential problems.

positional arguments:
  app_label
h
Sweet! @curved-television-6568 I'm being lazy not trying, but I'm wondering if the
[cli].alias
can include
--
in it:
run :django-admin --
, so all you run is
./pants django-admin help check
?
c
Yep, doing a demo of that right now…
💯 1
So, with this in your pants config:
Copy code
[cli.alias]
django-admin = "run :django-admin --"
You can call django admin like this:
Copy code
$ ./pants django-admin --version
3.2.8

$ ./pants django-admin help check
usage: django-admin.pex check [-h] [--tag TAGS] [--list-tags] [--deploy] [--fail-level {CRITICAL,ERROR,WARNING,INFO,DEBUG}] [--database DATABASES] [--version] [-v {0,1,2,3}]
                              [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color]
                              [app_label [app_label ...]]

Checks the entire Django project for potential problems.
This is on bleeding edge, using
export PANTS_SHA=c711f53fc4beaf2a544920514c4082b6527f75ba
Copy code
$ ./pants --version
2.8.0.dev4+gitc711f53f
💯 1
❤️ 1
@proud-dentist-22844 ^^ 😉 could be similar for invoke …
👏 1