black library error

Any one come across this error when running pip install django.

FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/bin/black'

I tried running an older django version (4.1), but got the same error, if I run
pip install black, before pip install Django, I get no errors, but I’ve never had to do this before?

Black is not a Django dependency. Django only requires asgiref and sqlparse for installation. This means you have something in your environment (ide? custom pip commands?) trying to run black.

You can verify this by creating a new virtual environment from the command line, activating it, and doing a pip install Django.

I have a similar issue, i have tried on a new project:

mkdir test_django
cd test_django
python3 -m venv .venv
source .venv/bin/activate
pip3 install Django
pip install --upgrade pip
django-admin startproject testdjango .

and this is the stack trace with the same error:

Traceback (most recent call last):
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/bin/django-admin", line 8, in <module>
    sys.exit(execute_from_command_line())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/commands/startproject.py", line 21, in handle
    super().handle("project", project_name, target, **options)
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/templates.py", line 232, in handle
    run_formatters([top_dir], **formatter_paths)
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/site-packages/django/core/management/utils.py", line 172, in run_formatters
    subprocess.run(
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/subprocess.py", line 1024, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/Users/lucianboboc/.pyenv/versions/3.11.3/lib/python3.11/subprocess.py", line 1917, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/bin/black'

I was getting the same error and it turned out that Django is checking if black is available here and if it is, subprocess calls it here .

In my case I had a copy of black on my system (/usr/local/bin/black), but not installed in my virtual environment. The subprocess call was expecting it in the virtual environment, and so threw the FileNotFoundError.

To fix the issue you could install black in your virtual environment as @SoundBoySelecta did, or you could find out how it got installed in /usr/local/bin and uninstall it if it’s no longer needed.

2 Likes

Just realized I made an error in my post and I get the black package import error when running dlango-admin startproject after django pip installation.

I also found this issue when calling

django-admin startapp temp

Just as @chiatt says, it calls the shutil.which("black") command referenced by line 232 in core.management.templates.py

The solutions posted above work, but we shouldn’t have to do that.

Hey guys, looks like django is checking for formatters available on your local system and finds “black” formatter but it fails when it’s trying to invoke it from command line. This is a piece of code from django/core/management/utils.py:

def run_formatters(written_files, black_path=(sentinel := object())):
    """
    Run the black formatter on the specified files.
    """
    # Use a sentinel rather than None, as which() returns None when not found.
    if black_path is sentinel:
        black_path = shutil.which("black")
    if black_path:
        subprocess.run(
            [black_path, "--fast", "--", *written_files],
            capture_output=True,
        )

So you either fix this black formatter or remove it completely from the system which was what I did.