Hi there, I'm working on adding a django project t...
# general
r
Hi there, I'm working on adding a django project to a monorepo and I'm running into an issue that I can't seem to sort out. Currently running this works fine:
Copy code
pants run src/python/bin/internal_tools/manage.py -- runserver
On the other hand this does not:
Copy code
pants run src/python/bin/internal_tools:manage -- runserver
When ran it gives this error:
Copy code
pex_warnings.warn(
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/me/code/org/repo/src/python/bin/internal_tools/manage.py", line 23, in <module>
    main()
  File "/home/me/code/org/repo/src/python/bin/internal_tools/manage.py", line 10, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
I haven't been able to sort out why its not finding django. My
BUILD
file for this project is set up like this:
Copy code
python_sources(
    name="site",
    dependencies=[
        "//:root#django-browser-reload",
        "//:root#django-tailwind",
        "//:root#django-widget-tweaks",
        "//:root#django-compressor",
        "//:root#psycopg2",
        "//:root#Django",
        "src/python/bin/internal_tools/project:project",
        "src/python/bin/internal_tools/apps:apps",
    ],
    overrides={
        "manage.py": {
            "restartable": True,
        }
    },
)

pex_binary(
    name="manage",
    entry_point="manage.py",
    dependencies=[
        ":site",
    ],
)
For context I've been following this repo as a bit of a guide: https://github.com/pantsbuild/example-django/tree/main Any insight here would be appreciated.
h
Do other
manage.py
commands work? If so then this is a well-known issue with
runserver
specifically
Or even more specifically, with the autoreloader, and how it tinkers with sys.path
r
To clarify,
runserver
works as expected when I target the
manage.py
directly. The problem is when I use the pex binary to invoke it.
Ahh, I see what you mean now. Taking a look.
h
Yes, the difference is that the semantics of running a .py file are "run directly in the workspace", whereas running a pex_binary target has the semantics of "build a .pex and run it"
And the pex mechanisms don't play well with the django reloader's sys.path munging magic