0

This code works fine when I run it in my PyCharm IDE…

argnotparse.py

if __name__ == '__main__':
    print('Hello world')
    world = 'world'
    print(f'Hello {world}')
    print('Hello', 'world', sep=' ', end='\n')

It produces the expected output of…

argnotparse.py"
Hello world
Hello world
Hello world

but if I run it from the command line I see…

$ python argnotparse.py
  File "argnotparse.py", line 5
    print(f'Hello {world}')
                         ^
SyntaxError: invalid syntax

My first thought was that something in python's command line parser wasn't updated with the f-strings in python v3.6. So I commented that line out and tried again…

$ python argnotparse.py
  File "argnotparse.py", line 6
    print('Hello', 'world', sep=' ', end='\n')
                               ^
SyntaxError: invalid syntax

This ruled out my idea that the argparse module had not been updated as its tutorial demonstrates this python v3 print statement with 'end' as a parameter. (It's in the section titled 'Getting a little more advanced'.)

Why is this happening?

p.s. I'm running python 3.7

--edit--

See 'How to set Python's default version to 3.x on OS X?'

lemi57ssss
  • 1,287
  • 4
  • 17
  • 36
  • 3
    Try `python -V` to see what version you're actually running - it clearly isn't 3.7 as you think. – jasonharper Aug 10 '19 at 15:15
  • It's so long since I had anything knowingly to do with python 2 (I never downloaded it onto this machine) that I didn't notice the argparse tutorial clearly shows the correct command as 'python3 prog.py'. Ty. – lemi57ssss Aug 10 '19 at 15:55
  • Was the [argparse] tag needed? What tutorial are you using? – hpaulj Aug 10 '19 at 16:38
  • @hpaulj. The tutorial is 'Argparse Tutorial' from the python manual. https://docs.python.org/3/howto/argparse.html#id1 I have removed the `argparse` tag because this problem was not caused by that module. – lemi57ssss Aug 11 '19 at 20:03

0 Answers0