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
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

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

Post

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?

History
Why does this post require attention from curators or moderators?
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)
Maybe try `winpty black version.py` when in git-bash? A complete shot in the dark but its come in han...
jmathew‭ wrote over 2 years ago

Maybe try winpty black version.py when in git-bash? A complete shot in the dark but its come in handy for certain interactive yarn commands. Relevant link

boudewijn21‭ wrote over 2 years ago · edited over 2 years ago

Hi jmathew‭, the command with winpty returns output! Another piece of the puzzle pointing to redirection of output. I've now been using the python -m black version.py variation, but the variation with winpty is closer to what I would like, plain black version.py.

jmathew‭ wrote over 2 years ago · edited over 2 years ago

If winpty does work, it's likely the problem you're facing is what's described in that link. Basically, your installation of python is expecting a windows console and git-bash isn't one.

If the goal is to just be able to do run the same command in both terminals setting up aliases in git-bash is a workaround. Either for just the one program with alias black = 'winpty black version.py', or for python as a whole with alias python = 'winpty python.exe' as described here: https://stackoverflow.com/a/36530750/730326. Though I'm not sure if the second would work. It depends on what exactly is being called when the activated terminal runs black/pytest/etc. Also iirc, there are issues redirecting, eg: winpty black version.py | less. So it's not something you'd always want on.

Alternatively WSL or Cygwin with their own versions of python installed should work without issue.

boudewijn21‭ wrote over 2 years ago · edited over 2 years ago

Oh I missed the link in your earlier message. Yes I think that explains it pretty well: the python console scripts expect a windows terminal, but they are not getting that with git-bash, but with winpty they do.

Good idea about the aliases. I've made them work for black/mypy/pylint/flake8, as in alias black='winpty black' etc. However, coverage run -m pytest tests does not work with alias coverage='winpty coverage' and/or alias pytest='winpty pytest', so I just left it at python -m coverage run -m pytest tests.

If you want to, you can write your comment as an answer and I'll accept it. I can also summarize our comment thread into an answer and accept that. Anyway, thank you very much for your help.

jmathew‭ wrote over 2 years ago

No problem glad to help! Feel free to summarize and answer.