Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91629 > unrolled thread

A simple print cannot run in Python Shell

Started byfl <rxjwg98@gmail.com>
First post2015-06-01 00:50 -0700
Last post2015-06-01 09:46 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  A simple print cannot run in Python Shell fl <rxjwg98@gmail.com> - 2015-06-01 00:50 -0700
    Re: A simple print cannot run in Python Shell Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-06-01 10:59 +0300
    Re: A simple print cannot run in Python Shell Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-06-01 18:30 +1000
      Re: A simple print cannot run in Python Shell Tim Golden <mail@timgolden.me.uk> - 2015-06-01 09:46 +0100

#91629 — A simple print cannot run in Python Shell

Fromfl <rxjwg98@gmail.com>
Date2015-06-01 00:50 -0700
SubjectA simple print cannot run in Python Shell
Message-ID<caa1cc9e-2f9b-423d-9298-a511180d15f7@googlegroups.com>
Hi,

When I try the following (They are saved in a main.py file)

#!/usr/bin/python
print r'C:\\nowhere'


It works as the tutorial, i.e. it echoes in a Windows 7
command console:


C:\\nowhere








When I run the following command in a Python 2.7.9 Shell on Windows 7,


print r'C:\\nowhere'



It has error:

>>> print r'C:\\nowhere'
SyntaxError: invalid syntax




What is the problem? Why does it behave different at .py file
and Python Shell?


Thanks, 

[toc] | [next] | [standalone]


#91632

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2015-06-01 10:59 +0300
Message-ID<lf5382bq2ou.fsf@ling.helsinki.fi>
In reply to#91629
fl writes:

> When I run the following command in a Python 2.7.9 Shell on Windows 7,
>
> print r'C:\\nowhere'
>
> It has error:
>
>>>> print r'C:\\nowhere'
> SyntaxError: invalid syntax
>
> What is the problem? Why does it behave different at .py file
> and Python Shell?

Have you executed the following in that shell?

   from __future__ import print_function

That replaces the Python 2 print command with the Python 3 print
function and then you would get that error message.

Apologies if you've done that because I used it in one of my examples
about reversed(). I didn't mean to confuse - I've just never quite
learnt the Python 2 print syntax myself.

There is no "from __past__ import print_command", is there? To recover
the original functionality in that shell :)

[toc] | [prev] | [next] | [standalone]


#91634

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-06-01 18:30 +1000
Message-ID<556c1826$0$13004$c3e8da3$5496439d@news.astraweb.com>
In reply to#91629
On Monday 01 June 2015 17:50, fl wrote:

> Hi,
> 
> When I try the following (They are saved in a main.py file)
> 
> #!/usr/bin/python
> print r'C:\\nowhere'

Aside: Don't use raw strings for file names. They aren't intended for file 
names, and while they will *usually* work, some day you will try something 
like this:

    path = r'C:\\path\ending\with\slash\'

and then you will discover that raw strings aren't for file names.

So just get used to using forward slashes.



> It works as the tutorial, i.e. it echoes in a Windows 7
> command console:
> 
> 
> C:\\nowhere


How do you run it in Windows 7? The hashbang line

    #!/usr/bin/python

is for Linux and Unix, and won't work on Windows. So you must be doing 
something to execute the file. What is that?


> When I run the following command in a Python 2.7.9 Shell on Windows 7,
>
> print r'C:\\nowhere'
> 
> 
> 
> It has error:
> 
>>>> print r'C:\\nowhere'
> SyntaxError: invalid syntax

In the future, please copy and paste the *complete* traceback, as that may 
include more information. Syntax errors usually include a line pointing to 
the location of the error, or just past it:

py> print foo
  File "<stdin>", line 1
    prin foo
           ^
SyntaxError: invalid syntax


Notice the line with the ^ caret?



> What is the problem? Why does it behave different at .py file
> and Python Shell?

Are you absolutely sure the Python Shell is running Python 2.7? It looks 
like you are running Python 3. What does this display in the shell?

import sys
sys.version


Otherwise you must have run this at some point:

from __future__ import print_function


If you exit the shell, and start it up again, that should reset import.

If not, then there is a possibility that your version of Python is 
configured to do the __future__ import. Try this:

print
print_function

What do you see? If you get an error, copy and paste the *entire* traceback.


-- 
Steve

[toc] | [prev] | [next] | [standalone]


#91635

FromTim Golden <mail@timgolden.me.uk>
Date2015-06-01 09:46 +0100
Message-ID<mailman.274.1433148415.5151.python-list@python.org>
In reply to#91634
On 01/06/2015 09:30, Steven D'Aprano wrote:
> How do you run it in Windows 7? The hashbang line
> 
>     #!/usr/bin/python
> 
> is for Linux and Unix, and won't work on Windows. So you must be doing 
> something to execute the file. What is that?

Actually, it will for anywhere with a recent Python 3.x or where the
standalone launcher has been installed:

https://www.python.org/dev/peps/pep-0397/
(cf particularly the section "Python Script Launching")

Of course, I don't know if the OP does in fact have
the launcher installed, but if they do then the answer is: py.exe.

Even if the launcher isn't installed, Windows will happily run a .py
file via the file extension association, assuming that this has been
selected when installing Python (which it is, by default, ISTR).

TJG

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web