-1

Python is installed at:

C:/Python/Python35

At the top of my program I put:

#!/usr/bin/env python3

I opened windows command prompt and entered:

./words.py

The message I got was:

"." is not recognized

I was told this should work great on Windows so I'm confused?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Jasmine
  • 1
  • 1
  • 2

4 Answers4

5

./words.py will not work on the Windows command prompt, this way of executing scripts is meant for Linux/UNIX shells.

If you're using Python 3.3+:

The shebang lines will be obeyed by the Windows Python Launcher py if you have it installed (https://docs.python.org/3/using/windows.html#shebang-lines).

Make sure you have that installed and try launching using:

py words.py

Sebastian
  • 2,678
  • 25
  • 24
  • 2
    Using a forward slash in the cmd shell generally requires double quotes, so use either `.\words.py` or `"./words.py"`. There's no reason to run py.exe directly. The default installation should have associated .py with the `Python.File` progid, which runs the script using py.exe. – Eryk Sun Nov 09 '15 at 06:24
  • 2
    In Vista+ if you set the environment variable [`NoDefaultCurrentDirectoryInExePath`](https://msdn.microsoft.com/en-us/library/ms684269), e.g. `set NoDefaultCurrentDirectoryInExePath=1`, then the cmd shell *requires* the explicit `.\words.py` or `"./words.py"`. In this case you could also use `py words.py`. Without setting the environment variable, you can simply run `words.py` from the working directory; this is the classic, less-secure behavior, which is still the default in Windows. – Eryk Sun Nov 09 '15 at 06:31
1

There are multiple problems here.

  1. Shebangs are a Unix thing. Windows does not recognize them at all. Instead, Windows uses file extensions.
  2. There is no file named /usr/bin/env on Windows, so even if your shebang did work, it wouldn't do what you want.
  3. The Python installer normally associates Python with .py files on installation. If it did not in your case (because you disabled the option?) you need to go fix that.
  4. ./something doesn't work on cmd.exe. It's .\something or just something.
Kevin
  • 28,963
  • 9
  • 62
  • 81
1

Whoever told you, must have either misunderstood you or you misunderstood them.

Shebang lines have no effect with windows unless you're trying to use cygwin. The other reason you'll need them to ensure smooth transition between windows and linux if you're passing the code to someone who might be running on linux.

As far as the "." it is not used in windows. Its reason in linux OS is to inform the command that the script is in the current directory.

For windows all you'll need is: python words.py.

Leb
  • 15,483
  • 10
  • 56
  • 75
  • You are [completely wrong](https://docs.python.org/3/using/windows.html#python-launcher-for-windows). – Eryk Sun Nov 09 '15 at 06:17
  • @eryksun very constructive, especially for an answer that addresses multiple questions you sure provided a lot of insight. From your link *Linux and other Unix like operating systems have native support for such lines (Shebang lines) and are commonly used on such systems to indicate how a script should be executed.* Like I said that line does not effect windows, the Python Launcher gives it that ability but if I'm *completely wrong* then you're saying all files need it for windows. – Leb Nov 09 '15 at 12:20
  • 1
    You said "[s]hebang lines have no effect with windows unless you're trying to use cygwin". That's wrong. A launcher can start any executable program, and even support virtual shebangs to ease cross-platform development. You can even associate it with files that have no extension. As to providing more information, I did so on the answer that showed the most merit, including explaining why the "." can definitely be necessary in cmd.exe on Vista+ (and is so on any Windows machine I use, since it's more secure and matches the behavior of PowerShell). – Eryk Sun Nov 09 '15 at 18:16
0

Your first problem is that Windows won't treat / as a path separator here. It has to be .\words.py or just words.py. That's why you get the error you see.

The next problem is that the shebang is not implemented by the windows command shell. It works on linux shells (and linux-like shells on Windows such as cygwin) because those shells read the front bit of executables to see how they should be executed. Windows does this by looking at the extension.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • That's half-wrong. Shells do not read executables, the kernel does. Here is the [relevant code](https://github.com/torvalds/linux/blob/master/fs/binfmt_script.c#L25) in Linux kernel. – spectras Nov 09 '15 at 02:48
  • @spectras I didn't get into the specifics of how its done because it didn't seem particularly important. OP was talking about the windows command shell so I kept the conversation on that level. – tdelaney Nov 09 '15 at 03:12
  • I understand the intent. Let's say "linux systems" then, that mirrors the "windows" of the next sentence, without getting the specifics wrong :) – spectras Nov 09 '15 at 03:22
  • 1
    The first part of this answer is also not entirely true. Windows seems to have no problem with the forward slash, at least for resolving files at the command prompt. I just ran C:/Python34/python.exe meow/meow.py and it didn't flinch (Windows 10). Apparently it's been this way for a long time: http://blogs.msdn.com/b/larryosterman/archive/2005/06/24/432386.aspx – Matthew MacGregor Nov 09 '15 at 05:14
  • `/` works as a path separator when in double quotes, e.g. `"./words.py"`. – Eryk Sun Nov 09 '15 at 06:34
  • @matsuzine, cmd's parsing is finicky. To reliably use `/` you need double quotes. In Windows 10 `./words.py` without quotes does fail. – Eryk Sun Nov 09 '15 at 06:36
  • @matsuzine no, its entirely true. For reasons unknown to me, when Windows parses relative path names, it treats `/` as the start of flag processing even if there is no intervening space. Try for instance `dir ./B` which will print in bare format. @eryksun mentions an interesting way to get around that. Although OP stumbled on this quirk, its not his main question so I just gave a quick blurb. – tdelaney Nov 09 '15 at 07:02
  • Interesting. So windows is happy with this: C:/Python34/python.exe meow/meow.py, but not with meow/meow.py or ./meow.py. So the confusion with the forward slash is positional? In the meow/meow.py case it's attempting to execute meow with an argument of /meow.py? – Matthew MacGregor Nov 09 '15 at 14:00
  • @Matsuzine Historically Windows doesn't require a space between a command and it's forward-slash delimited flags. Using the slash in a path creates ambiguities and the implementors had to choose one odd behavior or another. My guess is that this way of handling it broke fewer existing scripts. – tdelaney Nov 09 '15 at 15:35
  • https://stackoverflow.com/questions/33601349/python-shebang-not-working#comment54981811_33601520 – Smart Manoj Apr 14 '22 at 04:11