简体   繁体   中英

Python shebang not working

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?

./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

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 .

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM