diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -58,6 +58,12 @@ # See http://bugs.python.org/issue20053 for details os.environ['PIP_CONFIG_FILE'] = os.devnull +def _ensure_streams(): + # pip requires sys.__stdout__ to be set, which normally prevents + # installing it with pythonw.exe on Windows. + if not sys.__stdout__: + import io + sys.stdout = sys.__stdout__ = io.TextIOWrapper(io.BytesIO()) def bootstrap(*, root=None, upgrade=False, user=False, altinstall=False, default_pip=False, @@ -73,6 +79,7 @@ _require_ssl_for_pip() _disable_pip_configuration_settings() + _ensure_streams() # By default, installing pip and setuptools installs all of the # following scripts (X.Y == running Python version): diff --git a/Lib/ensurepip/_uninstall.py b/Lib/ensurepip/_uninstall.py --- a/Lib/ensurepip/_uninstall.py +++ b/Lib/ensurepip/_uninstall.py @@ -23,6 +23,7 @@ args = parser.parse_args(argv) + ensurepip._ensure_streams() ensurepip._uninstall_helper(verbosity=args.verbosity)