A simple print cannot run in Python Shell
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Jun 1 04:30:26 EDT 2015
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
More information about the Python-list
mailing list