The root of the problem is that spaces in shebangs are interpreted as supplying additional arguments to an executable, so C:\Program Files\Python\python.exe
gets seen as C:\Program
given Files\Python\python.exe
as an argument.
The best solution for this, because Windows LOVES spaces in the $HOME
directory and Program Files
and other places even though it can really break things in cmd.exe and Powershell and other tools, is:
Install Python to C:\Python
and add the C:\Python
folder where python.exe
lives and the Scripts
directory that lives inside it to your PATH
environment variable at the system or user level.
If you need Python 2.7.x and 3.x to co-exist, install them into C:\Python27
and C:\Python36
and C:\Python37
and rename the python.exe
to python2.exe
, python36.exe
, python37.exe
, etc and add each of those folders and their Scripts
folders into the PATH
. You may want to determine which of the Python 3 versions you want to be the "default" and also make a copy in that folder as python3.exe
to handle any scripts that use !#/usr/bin/env python3
.
If your user home directory has a space in it, you may also experience issues if you use the pip install --user somepackage
syntax. The --user
defaults to your home directory, and the space will trip up things in this case as well. The workaround is described here but boils down to exporting PYTHONUSERBASE to your environment.
export PYTHONUSERBASE=/myappenv
pip install --user SomePackage
or in Windows (Powershell):
$env:PYTHONUSERBASE='C:\PythonPkgs'
pip install --user SomePackage