Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reloader is breaking the execution if using Poetry scripts #2588

Closed
delucca opened this issue Feb 16, 2023 · 3 comments · Fixed by #2591
Closed

Reloader is breaking the execution if using Poetry scripts #2588

delucca opened this issue Feb 16, 2023 · 3 comments · Fixed by #2591

Comments

@delucca
Copy link

delucca commented Feb 16, 2023

The Flask reloader is apparently using the executed command instead of the actual Python package. When I try running with Poetry scripts, it throws an error (only if the reloader is enabled)

To replicate the behavior you can create a new Poetry Python package, install Flask and create the following Poetry script:

[tool.poetry.scripts]
dev = 'my_package.main:run'

(as you can imagine, you need to have a run function inside a file called main.py within a folder called my_package that will create the app and run it)

Now, you can set FLASK_DEBUG=0 and run it with poetry run dev. It will work (as expected).

After this, you can set FLASK_DEBUG=1 and run the same command, it will fail saying that it wasn't able to find the dev file. I've explored it a little bit, and the problem isn't the debugger, but the reloader (if you launch with the debugger, but without the reloader it works)

It seems the reloader somehow uses the executed command (dev) as the expected Python file to be used upon reloading and, because of that, it can't find the file and throws an error.

Environment:

  • Python version: 3.10
  • Flask version: 2.2.2
@delucca delucca changed the title Reloader is breaking the executing if using Poetry Reloader is breaking the execution if using Poetry scripts Feb 16, 2023
@davidism davidism transferred this issue from pallets/flask Feb 16, 2023
@davidism
Copy link
Member

davidism commented Feb 17, 2023

If I modify Werkzeug's reloader to print out sys.argv and sys.orig_argv, it shows this:

sys.argv = ['dev']
sys.orig_argv = ['/home/david/.cache/pypoetry/virtualenvs/hello-y1FJtfO2-py3.10/bin/python', '-c', "import sys; from importlib import import_module; sys.argv = ['dev']; sys.exit(import_module('hello').run())"]

This points to an issue with Poetry, it is breaking the documented behavior of sys.argv by setting sys.argv = ["dev"]; it should either set it to ["-c"] or to the filename of the module it imported. Please report that to Poetry.

That said, Werkzeug still wouldn't support this, as the python -c 'code' form of invoking Python does not put enough information in sys.argv to recreate the command. I was already looking at sys.orig_argv, but that was only added in Python 3.10.

Since you're using Flask, you should use the flask command to run the development server, rather than creating your own entry point.

@davidism
Copy link
Member

davidism commented Feb 17, 2023

I opened #2589 to better describe what I was working on already. It will fix this issue as well. The sys.argv issue I described above should still be reported to Poetry.

@delucca
Copy link
Author

delucca commented Feb 17, 2023

thanks @davidism for the detailed explanation. Tomorrow I'll open an issue on the Poetry repository 😁

Regarding using Flask CLI, I'm already doing that. I found that issue because I wanted to experiment with Poetry Scripts and accidentally found it 😁

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants