question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to wire injections, Flask out of context error

See original GitHub issue

I’m hoping this is just because I’m new at this.

I’m developing an application, and currently working on converting to use dependency-injector. I’ve written a container and have been able to get configuration and a data store factory working. However, I’m running into an issue when wiring up injections with a Flask blueprint.

When I follow the documentation and call the function to wire the container, I get a RuntimeError (actually from Flask):

Traceback (most recent call last):
  File "c:\source\personal\pydas\src\pydas\__init__.py", line 61, in create_app
    app.container.wire(
  File "src/dependency_injector/containers.pyx", line 250, in dependency_injector.containers.DynamicContainer.wire
  File "C:\Source\Personal\pyDAS\.env\lib\site-packages\dependency_injector\wiring.py", line 238, in wire
    if inspect.isfunction(member):
  File "C:\Python38\lib\inspect.py", line 169, in isfunction
    return isinstance(object, types.FunctionType)
  File "C:\Source\Personal\pyDAS\.env\lib\site-packages\werkzeug-2.0.0rc1-py3.8.egg\werkzeug\local.py", line 379, in __get__
    obj = instance._get_current_object()
  File "C:\Source\Personal\pyDAS\.env\lib\site-packages\werkzeug-2.0.0rc1-py3.8.egg\werkzeug\local.py", line 499, in _get_current_object
    return self.__local()  # type: ignore
  File "C:\Source\Personal\pyDAS\.env\lib\site-packages\flask-1.1.2-py3.8.egg\flask\globals.py", line 38, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.

Although it’s from Flask, it is only raised when calling the wire function, passing in the modules to my blueprints. I’ve refactored my code to no longer use current_app, thinking that might have made an impact. My primary app factory can be found here: https://github.com/bvanfleet/pydas/blob/u/bvanfleet/di.support/src/pydas/__init__.py

Has this ever been encountered before? I tried searching through the other issues, but couldn’t find an obvious answer.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
bvanfleetcommented, Feb 28, 2021

That’s great to hear, I was just about to report that I had a reproducable project that you could have worked with (sorry I was AFK for too long). Thanks again for catching and fixing this for me!

0reactions
rmk135commented, Feb 28, 2021

Thanks! If any other problems - feel free to open an issue. PyDAS is a great project btw!

Read more comments on GitHub >

github_iconTop Results From Across the Web

python 3.x - FlaskInjector : Working outside of request context
So I am trying to using dependency injection for the httpClient object. Working outside of request context.
Read more >
The Application Context — Flask Documentation (1.1.x)
RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current ...
Read more >
Developing Python Web Applications with Flask
Read HERE on how to install and use Eclipse PyDev for Python program development. This section highlights how to use Eclipse PyDev to...
Read more >
Python and Flask Tutorial in Visual Studio Code
If you see an error that the Flask module cannot be found, make sure you've run python -m pip install flask in your...
Read more >
Exploring SSTI in Flask/Jinja2 - nVisium Blog
If you've never heard of Server-Side Template Injection (SSTI) or aren't exactly sure ... If you want a little more context before diving...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found