Linuxbrew-core: python 3.9 of pre-commit 2.7.1 is broken

Created on 16 Oct 2020  路  11Comments  路  Source: Homebrew/linuxbrew-core

Bug report

Please note we will close your issue without comment if you delete, do not read or do not fill out the issue checklist below and provide ALL the requested information. If you repeatedly fail to use the issue template, we will block you from ever submitting issues to Homebrew again.

  • [x] ran brew update and can still reproduce the problem?
  • [x] ran brew doctor, fixed all issues and can still reproduce the problem?
  • [x] ran brew gist-logs <formula> (where <formula> is the name of the formula that failed) and included the output link?
  • [x] if brew gist-logs didn't work: ran brew config and brew doctor and included their output with your issue?

brew gist-logs pre-commit result

https://gist.github.com/zrma/9773e54e28124fd9e35ef8d453fe2a40

What you were trying to do (and why)

some git repo$ pre-commit install
some git repo$ git add .
some git repo$ git commit -m

or

$ /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3.9

What happened (include command output)

python runtime doesn't work


Command output

Error processing line 1 of /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/lib/python3.9/site-packages/_virtualenv.pth:

Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 169, in addpackage
exec(line)
File "", line 1, in
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/lib/python3.9/site-packages/_virtualenv.py", line 40, in
from functools import partial
ModuleNotFoundError: No module named 'functools'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 589, in
main()
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 572, in main
known_paths = venv(known_paths)
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 504, in venv
addsitepackages(known_paths, [sys.prefix])
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 359, in addsitepackages
addsitedir(sitedir, known_paths)
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 208, in addsitedir
addpackage(sitedir, name, known_paths)
File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 179, in addpackage
import traceback
ModuleNotFoundError: No module named 'traceback'


What you expected to happen

python 3.9 of pre-commit 2.7.1 works normally

Step-by-step reproduction instructions (by running brew install commands)

$ brew install pre-commit
$ move `some git repo`
some git repo$ vi .pre-commit-config.yaml

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
    -   id: check-yaml
    -   id: end-of-file-fixer
    -   id: trailing-whitespace

:wq

some git repo$ git add .
some git repo$ git commit -m "msg"

or

$ brew install pre-commit
$ /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3.9

thank you 馃樃

Most helpful comment

I also have the same issue with Ubuntu 20.04 LTS on WSL2 and Fedora 32 on physical. I tried to do some research and found there's something strange (to my eyes at least) how python3 works with pre-commit:

$ pwd
/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin

$ ls -l python3
python3 -> ../../../../../opt/[email protected]/bin/python3.9

$ ./python3
Python 3.9.0 etc just as expected

$ ../../../../../opt/[email protected]/bin/python3.9
Python 3.9.0 etc just as expected

$ /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3
Error processing line 1 of /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/lib/python3.9/site-packages/_virtualenv.pth:

Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 169, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/lib/python3.9/site-packages/_virtualenv.py", line 40, in <module>
    from functools import partial
ModuleNotFoundError: No module named 'functools'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 589, in <module>
    main()
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 572, in main
    known_paths = venv(known_paths)
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 504, in venv
    addsitepackages(known_paths, [sys.prefix])
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 359, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 208, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 179, in addpackage
    import traceback
ModuleNotFoundError: No module named 'traceback'

For me this error occurs on pre-commit 2.7.1 and 2.8.2

All 11 comments

I have the exact same problem on a Ubuntu WSL2-instance

Same issue in an official homebrew docker-image.
Content of used Dockerfile:

FROM homebrew/ubuntu16.04

# Install Brew Packages
## Install formulae
RUN brew install awscli aws-vault azure-cli pre-commit terraform-docs tfenv tfsec tflint infracost

## Install taps
RUN brew tap bridgecrewio/checkov https://github.com/bridgecrewio/checkov && brew update && brew install checkov

Next, cd to the git repo, stage a file and git commit -m "<message>" to see it fail with the Module 'functools' not found message

I also have the same issue with Ubuntu 20.04 LTS on WSL2 and Fedora 32 on physical. I tried to do some research and found there's something strange (to my eyes at least) how python3 works with pre-commit:

$ pwd
/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin

$ ls -l python3
python3 -> ../../../../../opt/[email protected]/bin/python3.9

$ ./python3
Python 3.9.0 etc just as expected

$ ../../../../../opt/[email protected]/bin/python3.9
Python 3.9.0 etc just as expected

$ /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3
Error processing line 1 of /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/lib/python3.9/site-packages/_virtualenv.pth:

Fatal Python error: init_import_site: Failed to import the site module
Python runtime state: initialized
Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 169, in addpackage
    exec(line)
  File "<string>", line 1, in <module>
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/lib/python3.9/site-packages/_virtualenv.py", line 40, in <module>
    from functools import partial
ModuleNotFoundError: No module named 'functools'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 589, in <module>
    main()
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 572, in main
    known_paths = venv(known_paths)
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 504, in venv
    addsitepackages(known_paths, [sys.prefix])
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 359, in addsitepackages
    addsitedir(sitedir, known_paths)
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 208, in addsitedir
    addpackage(sitedir, name, known_paths)
  File "/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/../../../../../opt/[email protected]/lib/python3.9/site.py", line 179, in addpackage
    import traceback
ModuleNotFoundError: No module named 'traceback'

For me this error occurs on pre-commit 2.7.1 and 2.8.2

I found another broken case about python 3.9 in docker-compose.

bash1$ docker-compose up // and pending...(wait to kill signal)

bash2$ docker-compose down
// it succeed to clean up docker resource gracefully.

// but in bash1's, error has occurred like this.

// bash1 result
------------------------------------------------------------------------------------------------------------------------
Exception in thread Thread-16:
Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 696, in _update_chunk_length
    self.chunk_left = int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 436, in _error_catcher
    yield
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 763, in read_chunked
    self._update_chunk_length()
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 700, in _update_chunk_length
    raise httplib.IncompleteRead(line)
http.client.IncompleteRead: IncompleteRead(0 bytes read)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/requests/models.py", line 751, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 571, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 792, in read_chunked
    self._original_response.close()
  File "/home/linuxbrew/.linuxbrew/opt/[email protected]/lib/python3.9/contextlib.py", line 135, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/urllib3/response.py", line 454, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/opt/[email protected]/lib/python3.9/threading.py", line 950, in _bootstrap_inner
    self.run()
  File "/home/linuxbrew/.linuxbrew/opt/[email protected]/lib/python3.9/threading.py", line 888, in run
    self._target(*self._args, **self._kwargs)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/compose/cli/log_printer.py", line 214, in watch_events
    for event in event_stream:
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/compose/project.py", line 575, in yield_loop
    yield build_container_event(event)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/compose/project.py", line 543, in build_container_event
    container = Container.from_id(self.client, event['id'])
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/compose/container.py", line 44, in from_id
    return cls(client, client.inspect_container(id), has_been_inspected=True)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/docker/api/container.py", line 771, in inspect_container
    self._get(self._url("/containers/{0}/json", container)), True
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/docker/api/client.py", line 228, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/requests/sessions.py", line 543, in get
    return self.request('GET', url, **kwargs)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/requests/sessions.py", line 685, in send
    r.content
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/requests/models.py", line 829, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "/home/linuxbrew/.linuxbrew/Cellar/docker-compose/1.27.4_1/libexec/lib/python3.9/site-packages/requests/models.py", line 754, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

thank you.

Same here with Ubuntu 18.04 on WSL.

I found that

/home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3

which is in the shebang of .git/hooks/pre-commit, gives the Fatal Python error, while

/home/linuxbrew/.linuxbrew/Cellar/pre-commit/2.8.2/libexec/bin/python

which is in the shebang of /home/linuxbrew/.linuxbrew/bin/pre-commit, runs fine without any problem (as pre-commit run --all-files does).

Changing the shebang and INSTALL_PYTHON in .git/hooks/pre-commit to point to /home/linuxbrew/.linuxbrew/Cellar/pre-commit/2.8.2/libexec/bin/python seems to fix (or rather work around) this issue.

I can also confirm that even without the change above, pre-commit run --all-files works just fine but git commit triggers the Python error.

Another workaround is to change the /home/linuxbrew/.linuxbrew/opt/pre-commit/libexec/bin/python3 link to point to /home/linuxbrew/.linuxbrew/Cellar/pre-commit/2.8.2/libexec/bin/python instead of the default ../../../../../opt/[email protected]/bin/python3.9

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Ubuntu 18.04 on WSL;
pre-commit version: 2.9.2

I found that if you specify the default language version for python in the .pre-commit-config.yaml to python3 this works without having to change the shebang or the INSTALL_PYTHON of the .git/hooks/pre-commit file.

default_language_version:
    # force all unspecified python hooks to run python3
    python: python3

Ubuntu 18.04 on WSL;
pre-commit version: 2.9.2

I found that if you specify the default language version for python in the .pre-commit-config.yaml to python3 this works without having to change the shebang or the INSTALL_PYTHON of the .git/hooks/pre-commit file.

default_language_version:
    # force all unspecified python hooks to run python3
    python: python3

Looks like a great workaround, I will test that, thanks

Was this page helpful?
0 / 5 - 0 ratings