Console scripts in virtual environment do not output to terminal in git bash
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?
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
boudewijn21 | (no comment) | Apr 21, 2022 at 09:34 |
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.
1 comment thread