16

I use Bash on Windows, provided by MSYS.

I tried to run a Python file with a shebang of #!/usr/bin/env python, but I get this error message:

/usr/bin/env: python: No such file or directory

What can I do to make this work?

I know I can launch the Python file by suffixing it with python, but I want it to work without the suffix too.

9

Use the proper path where Python is installed instead, for example:

#!c:/Python/python.exe

If you have Unix-like path support on your system (I'm not familiar with MSYS), you can always enter the path to your python executable, i.e. the output of which python.

3
  • 2
    Or you could use env, as the asker is doing. – Ignacio Vazquez-Abrams Jan 15 '12 at 0:07
  • I can't change the shebang, it's part of a project which involves other programmers that work on Linux and Mac. – Ram Rachum Jan 16 '12 at 11:25
  • @RamRachum What's the output of which env and which python and type python? – Daniel Beck Jan 16 '12 at 11:33
3

You might be interested in a Python Launcher For Windows

1

I turned out to be a mismatch between Virtualenv's activate.sh file and MSYSGIT. It was never intended to work on Windows.

0

What happens when you run (from the shell):

$ /usr/bin/env python

..?

If it starts an interactive python session, then python can be found. (You can also simply run: type python). If it prints "no such file or directory", then python isn't being found in your PATH. See if the following can be tweaked to fix the problem (spaces in the path/to/python will probably cause issues):

$ export PATH=$PATH:/path/to/python
$ /usr/bin/env python
0

Latest versions of Windows 10 supports installing Linux subsystems. If you run from there paths should be correct.

It should be a working solution if you are okay with running it that way.

0

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
0

Although it doesn't answer the question...

For people that installed python2 then python3 and just now want their python3 programs to run... right-click => open with => Choose another app now navigate to your python3 python.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.