print(…, sep=…) raises SyntaxError #777

Closed
lebigot opened this Issue Sep 8, 2011 · 7 comments

Comments

Projects
None yet
3 participants
@lebigot
Contributor

lebigot commented Sep 8, 2011

With IPython 0.11 (Mac OS X Lion, installed via Fink) and Python 2.7, the print() function raises a strange SyntaxError:

In [2]: from __future__ import print_function
In [3]: print("Helloe", "lkj", sep="lkj")
  File "<ipython-input-3-ff000ce1a53b>", line 1
    print("Helloe", "lkj", sep="lkj")
                          ^
SyntaxError: invalid syntax

The name print seems to be correctly defined:

In [6]: print?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:<built-in function print>
Namespace:  Python builtin
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout)

Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep:  string inserted between values, default a space.
end:  string appended after the last value, default a newline.

I'm not sure whether this matters, but the failure happens with both autocall on and off.

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Sep 8, 2011

Member

Hmm, I think I can see where this is coming from. We parse the code with ast.parse before we compile it. __future__ imports from previous cells are stored as flags to pass to the compiler, but they're not passed to ast.parse, so it is still parsing print as a statement.

I think I can see a way round it - I'll hopefully have a go later today.

Member

takluyver commented Sep 8, 2011

Hmm, I think I can see where this is coming from. We parse the code with ast.parse before we compile it. __future__ imports from previous cells are stored as flags to pass to the compiler, but they're not passed to ast.parse, so it is still parsing print as a statement.

I think I can see a way round it - I'll hopefully have a go later today.

@ghost ghost assigned takluyver Sep 8, 2011

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Sep 12, 2011

Member

See PR #784. Sorry I didn't get round to it on Thursday - it's been a busy few days.

Member

takluyver commented Sep 12, 2011

See PR #784. Sorry I didn't get round to it on Thursday - it's been a busy few days.

@takluyver takluyver closed this in e783b1c Sep 12, 2011

@lebigot

This comment has been minimized.

Show comment
Hide comment
@lebigot

lebigot Sep 13, 2011

Contributor

Thanks, that's great!

Contributor

lebigot commented Sep 13, 2011

Thanks, that's great!

mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this issue Nov 3, 2014

@bhandariaditya

This comment has been minimized.

Show comment
Hide comment
@bhandariaditya

bhandariaditya Jan 18, 2018

Hi, I just started learning python and came across the same error as described below:

  File "<ipython-input-13-024003e548d7>", line 5
    print(row, row, row, sep=div)
                                            ^
SyntaxError: invalid syntax

I'm using python 2.7.14 on macosx High Sierra v 10.13.2. I just can't get past it. The sep is argument present in print object but it's just not working on my machine. Please help @takluyver @lebigot

bhandariaditya commented Jan 18, 2018

Hi, I just started learning python and came across the same error as described below:

  File "<ipython-input-13-024003e548d7>", line 5
    print(row, row, row, sep=div)
                                            ^
SyntaxError: invalid syntax

I'm using python 2.7.14 on macosx High Sierra v 10.13.2. I just can't get past it. The sep is argument present in print object but it's just not working on my machine. Please help @takluyver @lebigot

@takluyver

This comment has been minimized.

Show comment
Hide comment
@takluyver

takluyver Jan 18, 2018

Member

I'd recommend using Python 3. If you can't for some reason, use from __future__ import print_function to allow that syntax on Python 2.

Member

takluyver commented Jan 18, 2018

I'd recommend using Python 3. If you can't for some reason, use from __future__ import print_function to allow that syntax on Python 2.

@lebigot

This comment has been minimized.

Show comment
Hide comment
@lebigot

lebigot Jan 20, 2018

Contributor

To add to @takluyver's response, @bhandariaditya: you are quoting Python 3 code but use a Python 2 interpreter. You thus have three options (starting with the most modern one):

  • Use Python 3 directly,
  • Use Python 2 with the Python 3 print function by indeed starting with from __future__ import print_function, or
  • Use the Python 2 equivalent: print div.join([row, row, row]) (or if this doesn't work use the more robust print div.join(map(str, [row, row, row]))).
Contributor

lebigot commented Jan 20, 2018

To add to @takluyver's response, @bhandariaditya: you are quoting Python 3 code but use a Python 2 interpreter. You thus have three options (starting with the most modern one):

  • Use Python 3 directly,
  • Use Python 2 with the Python 3 print function by indeed starting with from __future__ import print_function, or
  • Use the Python 2 equivalent: print div.join([row, row, row]) (or if this doesn't work use the more robust print div.join(map(str, [row, row, row]))).
@bhandariaditya

This comment has been minimized.

Show comment
Hide comment
@bhandariaditya

bhandariaditya Jan 20, 2018

@takluyver thanks @lebigot thanks a lot, the last version worked using print div.join(map(str, [row,row,row])).
the import for future module is not working, I think I first need to add it somewhere. Also, I had trouble installing python 3 from the terminal because of the following but then downloaded it from python.org:

MacBook-Pro:~ adityabhandari$ sudo brew install python3
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
MacBook-Pro:~ adityabhandari$

bhandariaditya commented Jan 20, 2018

@takluyver thanks @lebigot thanks a lot, the last version worked using print div.join(map(str, [row,row,row])).
the import for future module is not working, I think I first need to add it somewhere. Also, I had trouble installing python 3 from the terminal because of the following but then downloaded it from python.org:

MacBook-Pro:~ adityabhandari$ sudo brew install python3
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
MacBook-Pro:~ adityabhandari$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment