Install black with pipx not install dependencies aiohttp

1.4k views Asked by At

On MacOs I've installed with brew the pipx:

brew install pipx

then pipx install black

$ pipx list                                           
venvs are in /Users/mc/.local/pipx/venvs
apps are exposed on your $PATH at /Users/mc/.local/bin
   package black 22.12.0, installed using Python 3.11.1
    - black
    - blackd

However, I keep getting missing dependency:

$ /Users/mc/.local/bin/blackd                         
Traceback (most recent call last):
  File "/Users/mc/.local/bin/blackd", line 5, in <module>
    from blackd import patched_main
  File "/Users/mc/.local/pipx/venvs/black/lib/python3.11/site-packages/blackd/__init__.py", line 14, in <module>
    raise ImportError(
ImportError: aiohttp dependency is not installed: No module named 'aiohttp'. Please re-install black with the '[d]' extra install to obtain aiohttp_cors: `pip install black[d]`
  1. How to fix it?

  2. Why pipx is not solving this dependency while installing black ?

  3. Why it uses some (no idea where is this installed) python 3.11.1 when my system python is 3.9.6

    $ python3 --version
    Python 3.9.6

EDIT

I've did as advised by below answer from @KarlKnechtel :

$ brew install [email protected]                                
==> Auto-updated Homebrew!
Updated 1 tap (romkatv/powerlevel10k).

You have 2 outdated formulae installed.
You can upgrade them with brew upgrade
or list them with brew outdated.

==> Fetching [email protected]
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.10/manifests/3.10.9
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/python/3.10/blobs/sha256:a9b28161cec6e1a027f1eab7576af7
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:a9b28161cec6e1a027f
######################################################################## 100.0%
==> Pouring [email protected]_monterey.bottle.tar.gz
==> /opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3.10 -m ensurepip
==> /opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3.10 -m pip install -v --no-deps --no-index --upgr
==> Caveats
Python has been installed as
  /opt/homebrew/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /opt/homebrew/opt/[email protected]/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /opt/homebrew/lib/python3.10/site-packages

tkinter is no longer included with this formula, but it is available separately:
  brew install [email protected]

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
  /opt/homebrew/Cellar/[email protected]/3.10.9: 3,110 files, 57.1MB
==> Running `brew cleanup [email protected]`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

so I got:

$ python3 --version                                      
Python 3.10.9

$brew list python python3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/2to3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/2to3-3.10
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/idle3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/idle3.10
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/pip3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/pip3.10
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/pydoc3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/pydoc3.10
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3-config
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3.10
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3.10-config
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/wheel3
/opt/homebrew/Cellar/[email protected]/3.10.9/bin/wheel3.10
/opt/homebrew/Cellar/[email protected]/3.10.9/Frameworks/Python.framework/ (3055 files)
/opt/homebrew/Cellar/[email protected]/3.10.9/IDLE 3.app/Contents/ (8 files)
/opt/homebrew/Cellar/[email protected]/3.10.9/lib/pkgconfig/ (4 files)
/opt/homebrew/Cellar/[email protected]/3.10.9/libexec/bin/ (6 files)
/opt/homebrew/Cellar/[email protected]/3.10.9/libexec/wheel-0.38.4-py3-none-any.whl
/opt/homebrew/Cellar/[email protected]/3.10.9/Python Launcher 3.app/Contents/ (16 files)
/opt/homebrew/Cellar/[email protected]/3.10.9/share/man/ (2 files)

but still when I install black it installs python 3.11:

$pipx install black[d]                                   
zsh: no matches found: black[d]

$pipx install black                                    
  installed package black 22.12.0, installed using Python 3.11.1
  These apps are now globally available
    - black
    - blackd
done! ✨  ✨
2

There are 2 answers

0
mCs On BEST ANSWER

I solved it as in edit 1 but later you have to do according to this:

pipx install "black[d]" --force

but after that I got error:

dyld[56881]: Library not loaded: '/opt/homebrew/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/Python'
  Referenced from: '/Users/mc/Library/Application Support/pypoetry/venv/bin/python'
  Reason: tried: '/opt/homebrew/Cellar/[email protected]/3.9.13_1/Frameworks/Python.framework/Versions/3.9/Python' (no such file), '/Library/Frameworks/Python.framework/Versions/3.9/Python' (no such file), '/System/Library/Frameworks/Python.framework/Versions/3.9/Python' (no such file)

so I had to:

curl -sSL https://install.python-poetry.org | python3 - --uninstall
curl -sSL https://install.python-poetry.org | python3 -

And now:

poetry env use /opt/homebrew/Cellar/[email protected]/3.10.9/bin/python3.10 

as 3.10 is required for aiohttp as mentioned by @KarlKnechel

1
Karl Knechtel On

When you use pipx rather than pip to install a package, it will:

  • create a new virtual environment for the package
  • use pip to install the package into that virtual environment
  • Do some additional setup work, so that "entry points" for the package (commands created by the package to use directly at the command line, such as blackd) will work, without having to activate the specific virtual environment first.

That is its explicit purpose (i.e., the reason not to just use pip), and therefore why there is a different Python being used rather than the system Python.

To use the blackd entry point defined by Black (which starts up a server), the aiohttp optional dependency is required. This must be specified at installation. Per the error message, with pip, this looks like pip install black[d].

However, from the Black release notes (since version 22.8.0; current version 22.12.0):

Python 3.11 is now supported, except for blackd as aiohttp does not support 3.11 as of publishing (#3234)

So, pipx will need to use an older version to create the virtual environment.

From the pipx documentation:

The default python executable used to install a package is typically the python used to execute pipx and can be overridden by setting the environment variable PIPX_DEFAULT_PYTHON.

Presumably, brew install pipx installed pipx into some 3.11 install that is not the system Python. This sort of thing happens because you are not intended to install anything into the system Python. As the name suggests, your (operating) system depends on it, and a rogue package could seriously harm your computer in any number of ways. (Hence why pip is not available in it by default.)

You can:

  • use brew to install 3.10 or earlier, then install pipx to that, then proceed with pipx install black[d].

  • use venv to create a virtual environment based off your system Python (3.9); this venv should include pip automatically by default (downloaded and installed by venv). Activate this virtual environment, then use pip to install pipx in it, then proceed with pipx install black[d].