[pythonvis] Re: a thought on unneeded lines of code - Re: Re: print"Hello world."

  • From: "Octavian Rasnita" <orasnita@xxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Thu, 1 May 2014 07:19:06 +0300

The shebang line is not a convention for Python programmers. It is used only on 
Unix-like operating systems when you want to be able to run the program 
directly instead of running python or another program and use your script only 
as a parameter.

With other words, you can run:

./your_program.py

instead of:

python your_program.py

If is OK for you to run the program using the second way, you don't need the 
shebang line.

Under some operating systems, like Windows for example, that line is treated as 
a comment and is not doing anything when you run the program in a command line. 
It might be helpful though if the program is a CGI program, but CGI programs 
are seldomly used these days.

So, as it was said, for beginners it would be easier to just run their programs 
using

python program_name.py

and don't bother with the shebang line. This way it will work the same under 
Windows, Mac, Linux...

--Octavian

  ----- Original Message ----- 
  From: Charles Rivard 
  To: pythonvis@xxxxxxxxxxxxx 
  Sent: Thursday, May 01, 2014 2:43 AM
  Subject: [pythonvis] a thought on unneeded lines of code - Re: Re: 
print"Hello world."


  As someone new to Python, wouldn't it be best to follow conventions rather 
than deviating because the program might?, might being the key word here, work? 
 I would rather learn it correctly, and then modify if necessary.

  ---
  Be positive!  When it comes to being defeated, if you think you're finished, 
you! really! are! finished!
    ----- Original Message ----- 
    From: Alex Hall 
    To: pythonvis@xxxxxxxxxxxxx 
    Sent: Tuesday, April 29, 2014 9:13 AM
    Subject: [pythonvis] Re: print"Hello world."


    On Macs, at least on mine, you don't need that line. Of course it is an 
option, but if you don't include it, your script will still run. Again, this is 
only my system - perhaps if I had multiple versions or a different OS things 
would not work, but as it stands, they do fine with no version set. As I said, 
I'm not deminishing the importance and convenience of the command, but if you 
are new to Python and didn't understand about the line, you may be able to 
ignore it for the moment.

    On Apr 29, 2014, at 10:10 AM, Jonathan C. Cohn <jon.c.cohn@xxxxxxxxx> wrote:


      In Linux and Macintosh you want to have a #! statement on the first line 
indicating the interpreter to be loaded.
      You also need to make sure the script has the permission to execute. 

      chmod +x ./myscript.py 

      Then in terminal or a Xterm window you can do ./myScript.py 
      I believe I sent an article this morning describing the #! line found on 
UNIX systems.

      Best wishes,

      Jonathan



      On Apr 29, 2014, at 8:21 AM, Hrvoje Katić <hrvojekatic@xxxxxxxxx> wrote:


        Hello,
        I just want to add that if there's a traceback or error in your code, 
you will not be able to see it if you run .py script just by pressing Enter on 
it since command line window will close instantly.
        So what I do is I open a folder with .py script, and I make sure that 
all files are unselected. Next, I press shift+applications key and then I 
choose Open command window here option. After that, I type in hello.py and 
press Enter. Then I can review the error details with my review cursor. That's 
how it functions on Windows. I have to check out how it works on Linux when I 
will be logged in at my Linux system, and I don't know for the Mac.
        Anyway, I will be glad if I can help You all with learning Python. I 
already wrote some programs with it such as SkypeTalking and PrintList as well 
as Extended Winamp appmodule for NVDA. I started to learn Python since 2009, 
and it's my favorite language, although it's not fast like C++ or JAVA, but 
definitely easier to learn and debug code. My Python experience is definitely 
not the best, but it grows up from time to time.

        Regards,
        Hrvoje

        Dana 29.4.2014. 0:23, James Scholes je napisao:

          To answer your questions about Python and how it interacts with
          Terminal: Python is what we call an interpreted language, which means
          that instead of compiling your entire program into machine code, i.e. 
an
          executable, the Python interpreter runs your program one instruction 
at
          a time.  So, if you tell it to run a file called hello.py, and that 
file
          contains these lines:

          print "Hello world!"
          print "Goodbye!"

          The Python interpreter will run those lines, one after the other until
          it reaches the end of the file.  So you will, of course, see:

          Hello world!
          Goodbye world!

          If there are any errors while doing this, the interpreter will pick up
          on them and give you some useful information, called a traceback, to
          tell you what your program was trying to do when it failed and what
          exactly went wrong.  So if we make a typo on the second line and try 
to
          run a program containing:

          pritn "Goodbye world!"

          You will see:

          Hello world!
           File "hello.py", line 2
             pritn "Goodbye world!"
                                  ^
          SyntaxError: invalid syntax

          First, our correctly typed statement is run, so we see our "Hello
          world!" message.  But then, because we typed the N and the T in the 
word
          "print" the wrong way round, Python tells us we've made a syntax 
error.
          It also gives us the line number, the line of code being interpreted
          for convenience, and if you can see or be bothered counting the 
spaces,
          a visual representation of exactly where the syntax error was found on
          the line of code.  We're also told that the offending code is 
contained
          in hello.py, which is invaluable information when you are writing
          programs with many different code files.

          The Python interpreter, on most systems, is invoked simply by typing
          "python" at the command line (or Terminal).  This is a program, just
          like any other, so on Windows it must be installed.  On Mac OS X and
          most flavours of Linux, a version of the interpreter is preinstalled
          which will be sufficient for your learning.  When following the
          exercises in Learn Python the Hard Way, most of the time you will run
          Python with one argument; the name of the file you want to run.  For
          example, "python hello.py".  If you invoke the interpreter with no
          arguments, i.e. by simply typing "python", you will get an interactive
          prompt which allows you to run lines of Python code instantaniously.
          For example:

          Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit
          (Intel)] on win32
          Type "help", "copyright", "credits" or "license" for more information.

                print "Hello world!"

          Hello world!

                10 + 10

          20

                14 / 7

          2

                exit()

          Hope that helps.






      Best wishes,


      Jonathan









    --
    Have a great day,
    Alex Hall
    mehgcap@xxxxxxxxxx






Other related posts: