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

'ModuleNotFoundError' No module named 'tomli' after installing using pip and venv #3447

Closed
mateuszkojro opened this issue Dec 17, 2022 · 5 comments · Fixed by #3448
Closed
Labels
T: bug Something isn't working

Comments

@mateuszkojro
Copy link

Describe the bug

After installing black using pip inside venv running it results in 'ModuleNotFoundError' No module named 'tomli' exception beeing raised:

To Reproduce

  1. Create new virtual environment and activate it
python -m venv venv

and

.\venv\Scripts\activate.ps1
  1. pip install black
  2. black --version

output:

Traceback (most recent call last):
  File "C:\Users\PC1080Ti\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\PC1080Ti\AppData\Local\Programs\Python\Python38\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\PC1080Ti\Downloads\black-repro-bug\venv\Scripts\black.exe\__main__.py", line 4, in <module>
  File "src\black\__init__.py", line 45, in <module>
  File "c:\users\pc1080ti\downloads\black-repro-bug\venv\lib\site-packages\black\files.py", line 32, in <module>
    import tomli as tomllib
ModuleNotFoundError: No module named 'tomli'

Expected behavior
output:

black, 22.12.0 (compiled: yes)
Python (CPython) 3.8.8rc1

Environment

  • Black's version: 22.12.0
  • OS and Python version: Windows (21H2 19044.2364) running Python 3.8.8rc1

Additional context
The same behavior is observed when installing black from the main branch. 

@mateuszkojro mateuszkojro added the T: bug Something isn't working label Dec 17, 2022
@JelleZijlstra
Copy link
Collaborator

tomli is a dependency for 3.8: https://github.com/psf/black/blob/main/pyproject.toml#L70

I can't reproduce this on MacOS with 3.8.10. The one thing that stands out in your report is that you're using 3.8.8rc1: maybe something is mishandling deps for RC versions.

@mateuszkojro
Copy link
Author

Hi,
This might indeed be the issue with Python versions using the postifx notation since Tomli is automatically installed for Python 3.10.9 and not for 3.10.0b1 (I cannot test it for 3.8 because they no longer provide those on the official website).

@JelleZijlstra
Copy link
Collaborator

@ichard26 looked into this with me on Discord and found:

  • The root cause is a bug in packaging, fixed in allow pre-release versions in marker evaluation pypa/packaging#523
  • pip hasn't yet updated packaging to pick up this fix
  • We can prevent getting hit by this bug by changing our version requirement to python_version < '3.11' instead of listing a specific alpha. I'm about to submit a PR doing that.

@mateuszkojro
Copy link
Author

Tested in my environment and it solves it. Thanks a lot!

@NavyaAJ
Copy link

NavyaAJ commented Aug 24, 2023

I've reproduced this issue on mac today after the Python update to 3.11. Later realised I hadn't changed the version of Python in ci.yaml.
So the conclusion is that this issue can occur when the version of black installed for 3.10 is not compatible with 3.11

Error details:
`Traceback (most recent call last):

File "/opt/hostedtoolcache/Python/3.10.12/x64/bin/black", line 5, in

from black import patched_main

File "src/black/init.py", line 52, in

File "/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/black/files.py", line 27, in

import tomli as tomllib

ModuleNotFoundError: No module named 'tomli'

Error: Process completed with exit code 1.`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants