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.

Exception while calling 'pip.main(...)' from a Flask-Restful application

See original GitHub issue
  • Pip version: 9.0.1
  • Python version: 2.7.10
  • Operating system: Macos (Darwin Kernel Version 16.5.0)

Description:

I’m running an application using Flask-Restful (0.3.5), which exposes several APIs. I’m trying to uninstall a module using pip.main(['uninstall', '-y', '--log', 'pip.log','foobar']). This function returns 2, and the following exception is logged

Uninstalling foobar:
Exception:
Traceback (most recent call last):
  File "/Users/jmdacruz/workspace/App/build/virtualenv/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Users/jmdacruz/workspace/App/build/virtualenv/lib/python2.7/site-packages/pip/commands/uninstall.py", line 76, in run
    requirement_set.uninstall(auto_confirm=options.yes)
  File "/Users/jmdacruz/workspace/App/build/virtualenv/lib/python2.7/site-packages/pip/req/req_set.py", line 346, in uninstall
    req.uninstall(auto_confirm=auto_confirm)
  File "/Users/jmdacruz/workspace/App/build/virtualenv/lib/python2.7/site-packages/pip/req/req_install.py", line 754, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Users/jmdacruz/workspace/App/build/virtualenv/lib/python2.7/site-packages/pip/req/req_uninstall.py", line 95, in remove
    with indent_log():
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/Users/jmdacruz/workspace/App/build/virtualenv/lib/python2.7/site-packages/pip/utils/logging.py", line 34, in indent_log
    _log_state.indentation += num
AttributeError: 'thread._local' object has no attribute 'indentation'

What I’ve run:

pip.main(['uninstall', '-y', 'foobar'])
pip.main(['uninstall', '-y', '--log', 'pip.log','foobar'])
...

The command above returns 2 (which I think is UNKNOWN). I also tried replacing pip’s logger in memory instead of using the --log parameter to pip.main(...), but the result was the same. It seems that this could be solved with a failsafe here and here when the attribute indentation is not set on _log_state

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
pfmoorecommented, May 16, 2017

Using pip’s internal functions from within another application is not supported, sorry. I suggest trying to run pip in a subprocess.

But please note that the behaviour of a running Python program if a module it’s using is uninstalled is not well defined. It’s quite possible that even if you run pip in a subprocess, your application may not behave as expected after the uninstall.

0reactions
xavfernandezcommented, May 31, 2017

Should we close this issue if using the internal functions is not supported?

Yup 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Handle Errors in a Flask Application - DigitalOcean
You will see the following response: Not Found The requested URL was not found on the server.
Read more >
Why is my Flask error handler not being called? - Stack Overflow
It appears that MyGenericException is being raised properly, but Flask seems to ignore it. The @app.
Read more >
Handling Application Errors — Flask Documentation (2.2.x)
When Flask catches an exception while handling a request, it is first looked up by code. If no handler is registered for the...
Read more >
API Docs — Flask-RESTful 0.3.8 documentation
A decorator that apply marshalling to the return values of your methods. A decorator that formats the return values of your methods with...
Read more >
Why and how to handle exceptions in Python Flask
What do you want to do if the app fails to communicate with Threat Stack? You're going to raise a new exception. This...
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