Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Notifications
Mark all as read
Q&A

Console scripts in virtual environment do not output to terminal in git bash

+7
−0

I have console scripts in my virtual environment in \env\Scripts, installed with packages (e.g. black, pytest, coverage). If I try to execute these in Git Bash, the output is not returned to the terminal.

If I use the command line tool for e.g. black with a virtual enviroment activated in Git Bash, I get a blank line returned, but no response:

$ black version.py

If I invoke black through python, I get the expected response:

$ python -m black version.py
All done! ✨ � ✨
1 file left unchanged.

If I invoke the command line tool using black from the system interpreter (i.e. not using the virtual environment), I get the expected response:

$ black version.py
All done! ✨ � ✨
1 file left unchanged.

If I pipe the output to a file, I get the output in the terminal, but nothing in the file:

$ black version.py > test.txt
All done! ✨ � ✨
1 file left unchanged.

If I invoke the command line tool with Microsoft cmd, I get the expected response:

λ black version.py
All done! ✨ � ✨
1 file left unchanged.

If I invoke the command line tool with a blank virtual environment, I get the expected response.

$ python -m venv env
$ . ./env/Scripts/activate
$ pip install black
$ black version.py
All done! ✨ � ✨
1 file left unchanged.

If I try to pip uninstall black, sys.stderr seems to be missing:

$ pip uninstall black
Found existing installation: black 22.1.0
Uninstalling black-22.1.0:
  Would remove:
    c:\users\XXX\env\lib\site-packages\_black_version.py
    c:\users\XXX\env\lib\site-packages\black-22.1.0.dist-info\*
    c:\users\XXX\env\lib\site-packages\black\*
    c:\users\XXX\env\lib\site-packages\black_primer\*
    c:\users\XXX\env\lib\site-packages\blackd\*
    c:\users\XXX\env\lib\site-packages\blib2to3\*
    c:\users\XXX\env\lib\site-packages\c249b57082ff69932638__mypyc.cp39-win_amd64.pyd
    c:\users\XXX\env\scripts\black-primer.exe
    c:\users\XXX\env\scripts\black.exe
    c:\users\XXX\env\scripts\blackd.exe
ERROR: Exception:
Traceback (most recent call last):
  File "C:\Users\XXX\env\lib\site-packages\pip\_internal\cli\base_command.py", line 167, in exc_logging_wrapper
    status = run_func(*args)
  File "C:\Users\XXX\env\lib\site-packages\pip\_internal\commands\uninstall.py", line 97, in run
    uninstall_pathset = req.uninstall(
  File "C:\Users\XXX\env\lib\site-packages\pip\_internal\req\req_install.py", line 638, in uninstall
    uninstalled_pathset.remove(auto_confirm, verbose)
  File "C:\Users\XXX\env\lib\site-packages\pip\_internal\req\req_uninstall.py", line 363, in remove
    if auto_confirm or self._allowed_to_proceed(verbose):
  File "C:\Users\XXX\env\lib\site-packages\pip\_internal\req\req_uninstall.py", line 403, in _allowed_to_proceed
    return ask("Proceed (Y/n)? ", ("y", "n", "")) != "n"
  File "C:\Users\XXX\env\lib\site-packages\pip\_internal\utils\misc.py", line 186, in ask
    response = input(message)
RuntimeError: input(): lost sys.stderr

The script is where I would expect it:

$ which black
/c/Users/XXX/env/Scripts/black

I get similar results with coverage and pytest. I re-installed Git for Windows. I am using Python 3.9.9. It feels like an environment variable or something related to the PATH that messes up stderr/stdout.

I understand this is a very specific situation, probably related to the repository that I am currently working on (which I installed in editable mode with pip install -e .).

Could somebody point me in the right direction to fix this?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Maybe try `winpty black version.py` when in git-bash? A complete shot in the dark but its come in han... (5 comments)

1 answer

+0
−0

A summary of the comment thread with jmathew:

The simplest workaround is to use winpty, i.e. write:

$ winpty black version.py

The reason this works is that the Python build that I am using and the Python console scripts are expecting a Windows terminal, but Git Bash is not one. Winpty fixes this. See also: https://stackoverflow.com/questions/32597209/python-not-working-in-the-command-line-of-git-bash/36530750#36530750

This can be simplified by adding aliases to your .bashrc file:

alias black='winpty black'

which will allow you to again type:

$ black version.py

Something I did not investigate is building Python without ncurses support: https://stackoverflow.com/a/32599341/2505165

Moving away from Git Bash, solutions are to use WSL or Cygwin.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate