Using Shebangs with Python 3.5 on Windows


#1

Hello, I am running Python 3.5 (64 bit) under Windows 10 Professional. I am also using the Script package to enable running python scripts from within Atom. However, I am not sure what to do to enable me to run scripts which have the shebang line #!/usr/bin/python3 at the top. If I remove the line, all is fine, but I would like my system to automatically ignore the shebang line if it is in there and use my Python installation of C:\Python35. Can anyone help? Thanks in advance.


Auto-hide shebang
#2

Windows doesn’t support shebang lines.


#3

So are you saying that for all of the thousands of python scripts out there that have originated on Linux systems, a Windows user has to manually remove the shebang line from each script, and furthermore are you saying that no-one has had the wit or intelligence to address this problem?


#5

No, I’m saying the Windows operating system doesn’t support shebang lines. They are a Unix convention. On a Unix-based OS they are read by the program loader and used to execute the rest of the script. Part of their design is that, in most Unix scripting languages including Perl, Python, Ruby, bash, etc, a # is treated as the beginning of a comment. This allows the shebang to be ignored when it isn’t applicable.

Because the shebang line is a comment, executing such scripts is generally not problematic on Windows and the shebang line can be ignored.

If you have some program or system that is attempting to use the shebang line on your Windows machine as if it is a Unix machine, then I would assume that is the source of the problem.


#6

Thanks for the detailed response. The problem is that although the shebang line is a comment, Script does appear to use it and consequently falls over when the #!/usr/bin/python3 path cannot be found. I just cannot believe that I am the only one who has encountered this problem, and that no-one has sought to solve the problem.


#7

Does the script with the shebang execute when you go into the command line and invoke it directly with python?


#8

The script executes perfectly if I invoke it from the command line.


#9

Okay, it’s definitely the package. The file command-context.coffee contains the culprit.

    try
      if not runOptions.cmd? or runOptions.cmd is ''
        # Precondition: lang? and lang of grammarMap
        commandContext.command = codeContext.shebangCommand() or grammarMap[codeContext.lang][codeContext.argType].command
      else
        commandContext.command = runOptions.cmd

So if there’s nothing entered in runOptions, the package first looks at the shebang and then tries to figure out what the command should be based on grammarMap.

The simplest solution for you would be to create a profile with python as the command, and then the package will defer to that over the shebang line.


#10

That sounds like a good move. One slight problem, I haven’t yet sussed out how to create separate profiles in Atom. Any clue??!!


#11

This doesn’t have anything to do with Atom. You want to create a profile in the Script package. You do that by pulling up the Run Options modal ctrl-alt-shift-o, entering your options, and clicking Save as Profile. Then ctrl-alt-shift-b will invoke the Run as Profile command.


#12

Excellent, that works. While you’re here, if I may be cheeky, any idea what I need to enable a python program to interact normally with the user. For example, if my program has an input command, Scipt/Atom doesn’t seem to support pausing and prompting the user for input. Sorry these are very basic questions but I’ve been out of the loop as far as programming in general and python particular are concerned.


#13

I haven’t actually used the package in question for anything. I just found the answer to the previous question by using what I learned from your description of the symptoms and circumstances of the problem to figure out where to look in the code. I don’t know anything more than I can see in the readme file and a cursory examination of the package’s source. My attempt to execute a Python script to take input has also failed.


#14

No worries. I’m grateful for your input.


#15

Good luck on getting your issue answered.


#16

Having dug through many posts on this subject, it seems that the consensus on this question is that Atom/Script ain’t never gonna support STDIN and, therefore, ain’t never gonna support INPUT. It has been suggested however that if I had a decent Terminal package, life would be easier. Any suggestions?


#17

I haven’t used any terminal packages, mostly because it’s a significantly more pleasant experience using Cmder and I don’t feel like I lose much from having my command line open in a second window. With Windows shortcuts and how my taskbar is set up, I can press Win-4 to focus/open Atom and Win-5 to focus/open my terminal. It’s very convenient.