agreeable-oyster-28981
05/20/2022, 7:46 PMflask
pex
works with gunicorn
, having tried with entry_points
and PEX_SCRIPT
./pants run xxx
works perfectly fine
Once I try to have it running with gunicorn
(from local or from a docker image, which is the ultimate goal), I can’t seem to find the right syntax for the module/app
COPY packages.hello_world/main.pex packages/hello_world/main.pex
CMD ./packages/hello_world/main.pex packages.hello_world.main:app
Will give me the following error:
/pants run packages/hello_world
/Users/quentin/.cache/pants/setup/bootstrap-Darwin-arm64/2.11.0_py39/bin/pants
15:34:54.78 [INFO] Canceled: Building docker image hello_world:latest
15:34:56.01 [INFO] Completed: Building docker image hello_world:latest
[2022-05-20 19:34:57 +0000] [8] [INFO] Starting gunicorn 20.1.0
[2022-05-20 19:34:57 +0000] [8] [INFO] Listening at: <http://127.0.0.1:8000> (8)
[2022-05-20 19:34:57 +0000] [8] [INFO] Using worker: sync
[2022-05-20 19:34:57 +0000] [11] [INFO] Booting worker with pid: 11
[2022-05-20 19:34:57 +0000] [11] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
worker.init_process()
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/root/.pex/venvs/s/4323ba33/venv/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
mod = importlib.import_module(module)
File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'packages.hello_world.main'
[2022-05-20 19:34:57 +0000] [11] [INFO] Worker exiting (pid: 11)
[2022-05-20 19:34:57 +0000] [8] [INFO] Shutting down: Master
[2022-05-20 19:34:57 +0000] [8] [INFO] Reason: Worker failed to boot.
I’ve been trying multiple syntax, and I either get: Failed to find attribute 'app' in 'packages.hello_world'.
or ModuleNotFoundError: No module named 'packages.hello_world.main'
which tells me that
• the package packages.hello_world
exists
• the package `packages.hellow_world.main`doesn’t exist
• there is no app
attribute in the package
• there is no main
attribute in the package
folder structure is:
/packages/hello_word
|- Build
|- Dockerfile
|- main.py
You can find a repo to reproduce the issue here: https://github.com/qventura/pants-flask/tree/main/packages/hello_world
Thanks in advance, if anyone can help me finding the right way to start my app, I’ll be grateful!agreeable-oyster-28981
05/20/2022, 8:25 PMpex
, with:
pex -vv -D . -o ./main.pex -r requirements.txt
PEX_SCRIPT=gunicorn ./main.pex main:app
I looks like I’m just struggling with the naming…wide-midnight-78598
05/20/2022, 8:50 PMwide-midnight-78598
05/20/2022, 9:00 PM:src
sources as a dependency into your pex_binary?agreeable-oyster-28981
05/20/2022, 9:01 PM./pants package packages/hello_world:main-non-docker
./dist/packages.hello_world/main-non-docker.pex main:app
or
./pants run package packages/hello_world:docker
They both don’t run, with error ModuleNotFoundError: No module named 'main'
wide-midnight-78598
05/20/2022, 9:05 PMwide-midnight-78598
05/20/2022, 9:06 PMwide-midnight-78598
05/20/2022, 9:07 PMpex_binary(
name="main",
execution_mode="venv",
entry_point="hello_world.main",
dependencies=[":src"]
agreeable-oyster-28981
05/20/2022, 9:08 PMAlso, are you adding yourHum, no. So, when I usesources as a dependency into your pex_binary?:src
entry_point="main.py",
the file is included automatically, but if my entry point is gunicorn
I need to add the files?agreeable-oyster-28981
05/20/2022, 9:08 PMwide-midnight-78598
05/20/2022, 9:09 PM__init__.py
files where neededwide-midnight-78598
05/20/2022, 9:10 PM./pants dependencies
or ./pants peek
to see what will be included, but personally, I'm always explicit over implicitwide-midnight-78598
05/20/2022, 9:15 PMhappy-kitchen-89482
05/20/2022, 9:40 PM./pants package packages/hello_world:main
and run the resulting pex?wide-midnight-78598
05/21/2022, 12:44 AM# BUILD
python_sources(
name="src"
)
pex_binary(
name="main",
script="gunicorn",
dependencies=[":src", "//:root#gunicorn"]
)
And then ./pants package ::
And then from the correct dir:
> ./main.pex hello_world.main:app
[2022-05-20 20:43:01 -0400] [78898] [INFO] Starting gunicorn 20.1.0
[2022-05-20 20:43:01 -0400] [78898] [INFO] Listening at: <http://127.0.0.1:8000> (78898)
[2022-05-20 20:43:01 -0400] [78898] [INFO] Using worker: sync
[2022-05-20 20:43:01 -0400] [78899] [INFO] Booting worker with pid: 78899
wide-midnight-78598
05/21/2022, 12:46 AMPEX_TOOLS=1
exported to a venv and then called that from nginx unit
@enough-analyst-54434 would know better thoughwide-midnight-78598
05/21/2022, 12:48 AMmain.pex
and check out PEX_INFO
that should give you a better idea of what's going onhappy-kitchen-89482
05/22/2022, 10:31 PMpackages/helloworld:main
has gunicorn
as an entry point, and gunicorn expects an argumenthappy-kitchen-89482
05/22/2022, 10:31 PM./dist/packages.hello_world/main.pex hello_world.main
would be the right way to invoke thathappy-kitchen-89482
05/22/2022, 10:32 PMmain.pex
is really a pex that contains gunicorn and your main code, but the entry point is generic gunicorn doesn't know about the main)happy-kitchen-89482
05/22/2022, 10:33 PMModuleNotFoundError: No module named 'hello_world'
happy-kitchen-89482
05/22/2022, 11:07 PMmain.py
has no imports from hello_world
so pants has nothing to infer a dependency fromhappy-kitchen-89482
05/22/2022, 11:08 PMdependencies=[":src"]
to the pex_binary targethappy-kitchen-89482
05/22/2022, 11:10 PMFailed to find attribute 'application' in 'hello_world.main'.
happy-kitchen-89482
05/22/2022, 11:13 PMhappy-kitchen-89482
05/22/2022, 11:13 PMapp
to application
in main.py
solved that and the server works for mehappy-kitchen-89482
05/22/2022, 11:29 PMhappy-kitchen-89482
05/22/2022, 11:29 PMwsgi_app =
in gunicorn.conf.pyhappy-kitchen-89482
05/22/2022, 11:30 PMagreeable-oyster-28981
05/24/2022, 12:13 PMagreeable-oyster-28981
05/24/2022, 12:15 PMapplication
to app
is not 100% necessary, as the attribute name can be passed directly when calling unicorn with hello_world.main:app
agreeable-oyster-28981
05/24/2022, 12:17 PM