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

AttributeError: 'thread._local' object has no attribute 'indentation' #2553

Closed
drnextgis opened this issue Mar 17, 2015 · 25 comments
Closed
Labels
auto-locked Outdated issues that have been locked by automation

Comments

@drnextgis
Copy link

Traceback points here https://github.com/pypa/pip/blob/develop/pip/utils/logging.py#L39

@dstufft
Copy link
Member

dstufft commented Mar 17, 2015

Can you give the entire error?

@dstufft
Copy link
Member

dstufft commented Mar 17, 2015

Also what you were doing to trigger it.

@drnextgis
Copy link
Author

I cannot reproduce it here, the following stacktrace sent me my colleague:

Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/__init__.py", line 846, in emit
    msg = self.format(record)
  File "/home/zadmin/ngw/env/local/lib/python2.7/site-packages/pip/utils/logging.py", line 103, in format
    msg = logging.StreamHandler.format(self, record)
  File "/usr/lib/python2.7/logging/__init__.py", line 723, in format
    return fmt.format(record)
  File "/home/zadmin/ngw/env/local/lib/python2.7/site-packages/pip/utils/logging.py", line 52, in format
    for line in formatted.splitlines(True)
  File "/home/zadmin/ngw/env/local/lib/python2.7/site-packages/pip/utils/logging.py", line 39, in get_indentation
    return _log_state.indentation
AttributeError: 'thread._local' object has no attribute 'indentation'
Logged from file channel.py, line 340
Traceback (most recent call last):
  File "/usr/lib/python2.7/logging/handlers.py", line 77, in emit
    if self.shouldRollover(record):
  File "/usr/lib/python2.7/logging/handlers.py", line 156, in shouldRollover
    msg = "%s\n" % self.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 723, in format
    return fmt.format(record)
  File "/home/zadmin/ngw/env/local/lib/python2.7/site-packages/pip/utils/logging.py", line 52, in format
    for line in formatted.splitlines(True)
  File "/home/zadmin/ngw/env/local/lib/python2.7/site-packages/pip/utils/logging.py", line 39, in get_indentation
    return _log_state.indentation
AttributeError: 'thread._local' object has no attribute 'indentation'
Logged from file channel.py, line 340

This error occurs in Pyramid application installed within virtual environmen with the following command:

env/bin/pip install -e appname

When we downgraded pip to 1.5.4 this error was gone.

@airhorns
Copy link

Also seeing this issue after upgrading to pip 6. I think it has to do with logging from something other than the main thread if pip has been imported in a python process.

@balloob
Copy link
Contributor

balloob commented Jul 15, 2015

Would it be an idea to have get_indentation return 0 if indentation does not exist in current thread local?

This would do the job:

def get_indentation():
    return getattr(_log_state, 'indentation', 0)

balloob added a commit to balloob/pip that referenced this issue Jul 15, 2015
Fixes pypa#2553. When a Python script embeds pip and tries to log something from a different thread, an AttributeError is raised.
@matthewhampton
Copy link

@balloob @dstufft I think this just masks the problem... The issue is that _log_state is a thread.local() object - I'm not sure why - and the indentation attribute is set in the global scope - i.e. it is only visible to the thread that first imports pip. Any other thread has a problem...

@dstufft Is there a reason that it is a thread local object?

@zjffdu
Copy link

zjffdu commented Mar 6, 2017

I hit this issue in pip '9.0.1'.

Here's the code snippet I used

        def _run_pip(packages, iterator):
            import pip
            pip.main(["install"] + packages)

        if install_driver:
            import threading
            driver_thread = threading.Thread(target=_run_pip, args=(packages, None),
                                             name='driver_install_thread')
            driver_thread.start()

@jmdacruz
Copy link

Same here (pip 9.0.1). The issue seems to be that this line is not using get_indentation(...) and it's accessing the attribute directly: https://github.com/pypa/pip/blob/0da18e9/pip/utils/logging.py#L34

@monty6400
Copy link

So how can I fix this on the pip module?

@pradyunsg
Copy link
Member

@zjffdu pip does not support in-process usage. If you want to invoke pip from a script, the only supported manner is to run it as a subprocess.

So how can I fix this on the pip module?

@monty6400 You just have to modify 2 lines in pip.utils.logging. But IIUC one won't hit this error unless they're using pip incorrectly.

@monty6400
Copy link

@pradyunsg I understood that, but the lines I need to modify are zipped, so I can't modify them, is there any way I'm not aware of to modify these lines?
(and to be sure I should replace _log_state.indentation to getattr(_log_state, indentation, 0)?)

Thanks anyway!

@pradyunsg
Copy link
Member

@monty6400 As I said, it's likely that your use of pip is incorrect if you're running into this error.

is there any way I'm not aware of to modify these lines?

Nope. I don't think so.

(and to be sure I should replace _log_state.indentation to getattr(_log_state, indentation, 0)?)

Yes, that's the modification.

@monty6400
Copy link

monty6400 commented May 21, 2017

@pradyunsg
this is the code I try to run:
wiucht
can you see what is the problem?

@pradyunsg
Copy link
Member

You're importing pip and calling pip.main. This is in-process invocation of pip and not supported. You should use the subprocess module (see subprocess.run) and invoke pip using that...

That said, I'm in the middle of something so, I'll let myself out of this discussion now. :)

@monty6400
Copy link

I saw no subprocess.run, only call (I'm using python 2.7 maybe that's the reason), anyway I saw multiprocessing and made the code run the pip installer in another window (the problem was not with the call of main, but with the call to the pip installer), and I see no problem for now.

thanks for the help

@mitar
Copy link

mitar commented Aug 8, 2017

I made #4654 to address this.

@pfmoore
Copy link
Member

pfmoore commented Aug 8, 2017

Summary (for clarity) pip does not support use in-process via its internal API, and in particular the pip code makes no guarantees that it can be used safely in the presence of threads.

@dmvieira
Copy link

Do you know when this fix will be released?

@RonnyPfannschmidt
Copy link
Contributor

@dmvieira never, as the pr got closed

@pradyunsg
Copy link
Member

@dmvieira This won't be fixed since fixing this is essentially moving a bit towards supporting people's use of an unsupported API.

FWIW, we know that there'll be some sort of other bug that'll bite you even if this fix gets merged simply because [what @pfmoore stated above].

That said, if you're feeling fancy, you can patch pip's functions but then you're on you own. :)

@dmvieira
Copy link

Ok! Better answer 😊 thx

@pradyunsg
Copy link
Member

@pypa/pip-committers Do any of you think now reverting #2977 is a good idea?

@pfmoore
Copy link
Member

pfmoore commented Sep 27, 2017

I think we probably shouldn't have committed it, and we should have been stricter over our "no supported API" position, but hindsight is easy, and I don't think it's worth reverting now. But it shouldn't be taken as a precedent that we'll accept further changes in support of using the internal API.

@dstufft
Copy link
Member

dstufft commented Sep 27, 2017

I agree with @pfmoore

@pradyunsg
Copy link
Member

Cool. :)

@lock lock bot added the auto-locked Outdated issues that have been locked by automation label Jun 3, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 3, 2019
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
auto-locked Outdated issues that have been locked by automation
Projects
None yet
Development

No branches or pull requests