First time poster, so let me know if I need to provide more info! I have a Django project of the following structure:
a/b/c/d/
├── e
│ ├── BUILD.pants
│ ├── myapp
│ │ ├── BUILD.pants
│ │ ├── migrations
│ │ ├── models.py
│ │ ├── ...
│ │ └── tests
│ ├── BUILD.pants
│ ├── settings.py
│ ├── ...
│ └── wsgi.py
├── BUILD.pants
├── ...
└── manage.py
The Django app can be run using manage.py runserver
perfectly fine, but it uses relative and app-level imports which causes Pants and Pyright issues within my repo. I want to set it up on Pants anyways, so I have switched all my relative imports to absolute imports (which breaks it running via Django directly).
This is my BUILD.pants at a/b/c/d:
python_sources()
pex_binary(
name="manage",
entry_point="manage.py",
dependencies=["a/b/c/d/e"],
restartable=True,
)
Running manage.py through Pants, instead of starting the app, produces the following:
./pants run a/b/c/d:manage -- runserver --noreload
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/__main__.py", line 103, in <module>
bootstrap_pex(__entry_point__, execute=__execute__, venv_dir=__venv_dir__)
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex_bootstrapper.py", line 599, in bootstrap_pex
pex.PEX(entry_point).execute()
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex.py", line 551, in execute
sys.exit(self._wrap_coverage(self._wrap_profiling, self._execute))
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex.py", line 458, in _wrap_coverage
return runner(*args)
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex.py", line 489, in _wrap_profiling
return runner(*args)
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex.py", line 594, in _execute
return self.execute_entry(
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex.py", line 775, in execute_entry
return self.execute_module(entry_point.module)
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/.bootstrap/pex/pex.py", line 783, in execute_module
runpy.run_module(module_name, run_name="__main__", alter_sys=True)
File "/usr/lib/python3.8/runpy.py", line 207, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/a/b/c/d/manage.py", line 22, in <module>
main()
File "/home/artificialask/.pex/unzipped_pexes/900cb0387e270fc2eadde5a3883453a34b9a796e/a/b/c/d/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/artificialask/.pex/installed_wheels/ba7b3b60845048a44d2e41a4e4a52139aed31176b2358657f84b4e506efcde25/Django-4.1.7-py3-none-any.whl/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/artificialask/.pex/installed_wheels/ba7b3b60845048a44d2e41a4e4a52139aed31176b2358657f84b4e506efcde25/Django-4.1.7-py3-none-any.whl/django/core/management/__init__.py", line 420, in execute
django.setup()
File "/home/artificialask/.pex/installed_wheels/ba7b3b60845048a44d2e41a4e4a52139aed31176b2358657f84b4e506efcde25/Django-4.1.7-py3-none-any.whl/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/artificialask/.pex/installed_wheels/ba7b3b60845048a44d2e41a4e4a52139aed31176b2358657f84b4e506efcde25/Django-4.1.7-py3-none-any.whl/django/apps/registry.py", line 91, in populate
app_config = AppConfig.create(entry)
File "/home/artificialask/.pex/installed_wheels/ba7b3b60845048a44d2e41a4e4a52139aed31176b2358657f84b4e506efcde25/Django-4.1.7-py3-none-any.whl/django/apps/config.py", line 193, in create
import_module(entry)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'd'
In manage.py main()
it looks like the offending line is:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'b.c.d.e.settings')
.
(In my repo all absolute imports are relative to b
, even though the file is located in directory a/b
). Trying to change this line to some other import path will unsurprisingly give the same error, just with a different module not being found.
I've tried:
This example repo structure is a bit different because it uses a service architecture, but I tried to mimic the patterns I could pick out. --noreload
also doesn't do anything for me.
This post I've used to try and structure my build files, but I don't think this is the same core issue, since running ./pants dependencies
shows the correct dependencies for my directories.
I think the issue is something silly, where I'm not using the correct directory structure/import structure in one of the build/settings files, but I've tried all the combinations I can think of and nothing seems to work. I'm a total noob with Pants so I'm not sure how to setup the pex_binary() and python_sources() in the BUILD.pants files.