0

Note: This was not answered by the question that was marked as the original. This is more than just a Python v2 vs v3 problem, which I explain in the comments below.

Original post: I am trying to learn Python at work, so I am currently using Portable Python 3.2.1.1 (which will henceforth be referred to as PP). (I mention this because this problem doesn't happen at home when I use my Mac and regular Python.)

I am working through exercise 16 of Learning Python the Hard Way (http://learnpythonthehardway.org/book/ex16.html). I've heard this isn't the best learning tool, but I am a complete programming n00b and I'm a hands-on learner. If you have any better suggestions, I'm open!

The first few lines of the exercise read:

    from sys import argv
    script, filename = argv
    print "We're going to erase %r." % filename
    print "If you don't want that, hit CTRL-C (^C)."

My script is titled Ex16.py and the file I am using is Python.txt, and both of these are in the same folder as the PP .exes. I don't think that's necessary, but hoped maybe it would fix the problem... negative. When I press "Run" in PP, it doesn't work because argv requires you provide an argument when you start the script: python Ex16.py Python.txt

When I launch Python.exe (which, in PP is Portable-Python.exe), I get the standard Python prompt, >>>, but whatever I enter I get the same error message:

    File "<stdin>", line 1
    with whatever I've just tried repeated back to me with the marker to 
    indicate where the problem is. (has not been helpful so far)

    SyntaxError: invalid syntax

I have tried typing the following at the >>> prompt: python Ex16.py Python.txt,, Ex16.py Python.txt,, "%PATH&\Ex16.py" "%PATH%\Python.txt" (with the actual filepaths),, print 'hello world'

I just keep getting the same invalid syntax error over and over. Even a basic print command returned an invalid syntax error. The only one that triggered a different error was the one where I tried whole filepaths. That one returned:

    File "<stdin>", line 1
    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in 
    position 2-3: truncated \UXXXXXXXX escape

Yes, I have Googled the crap outta both errors. I read that sometimes the problem is not doubling the backspaces, so I tried that, too, putting two \ where just one had been before in both filepaths. I even tried putting — # -*- coding: utf-8 -*- at the beginning of the script thinking maybe there was some unicode error. That, with the full filepaths, resulted in the same unicode error mentioned earlier.

Yes, I have checked that my code is matching that in the exercise.
Yes, this works at home on non-PP.

All this leads me to believe that the problem is probably in the way I'm trying to run the scripts in PP (but why won't print work?), but I haven't a clue what I'm doing wrong.

Thanks!

user2195503
  • 17
  • 2
  • 8
  • If you could paste a complete terminal session, exactly as it appears, it might be easier to help. – Ned Batchelder Mar 21 '13 at 15:22
  • I'd love to copy and paste (it would be much easier), but it won't let me select any text. Even right clicking doesn't produce any options. :( – user2195503 Mar 21 '13 at 15:32
  • _I have tried typing the following at the >>> prompt: python Ex16.py Python.txt_ - once you are at the python prompt (>>>), enter python statements, not shell commands. `>>> import sys`, '>>> sys.stdout.write(sys.version + ', ' + sys.executable + '\n')`, and etc... That two-liner is a great way to figure out which python you are really running. – tdelaney Mar 21 '13 at 16:17

2 Answers2

5

print is a function in Python 3:

print('my string with content and the like')

It is no longer supported as being a 'statement'. You might want to check out a list of things that changed from python2.x to python3.x (there's a number of incompatibilities). Also, you might be better off finding a tutorial using Python3.

akaIDIOT
  • 9,171
  • 3
  • 27
  • 30
  • That's a good point I'll have to read up on. – user2195503 Mar 21 '13 at 15:31
  • So, in part it was a 2 vs 3 error (I was using PP 3 but trying to write with 2), BUT it ended up being more of a problem with the portable-python.exe, python.exe, and the way Windows was interpreting my commands. I shall explain: Once installed, Portable Python has 3 things in the main folder- App folder, PyScripter-Portable.exe, and Python-Portable.exe. In the App folder is python.exe, mentioned by @Charl-Botha, provides the python shell. Python-Portable.exe and App\Python.exe both provide a python shell. – user2195503 Mar 29 '13 at 17:06
  • Once I got the PP versions the same, I went to the CMD prompt (not the interpreter), and entered the full filepath in quotation marks (which did have spaces in the path) for Python.exe, one space, the full script filepath without quotation marks (the filepath DIDN'T have spaces), one space, and the full filepath for the target file (Python.txt) in quotation marks (again, path had spaces). That successfully ran my script. So thanks! :) – user2195503 Mar 29 '13 at 17:13
2

You have to type:

Portable-Python.exe Ex16.py Python.txt

at your command prompt. To get a command prompt, press WindowsKey-R, then type "cmd" and press enter. You should now be looking at something like c:\>. Navigate to your portable python installation by using the cd command.

Charl Botha
  • 4,373
  • 34
  • 53
  • I tried navigating to the folder via command line and then using what you suggested, but it just launches portable-python.exe and gives me the >>> prompt. – user2195503 Mar 21 '13 at 15:30
  • hmmm, somewhere amongst those files you should still find a python.exe. It looks like Portable-Python.exe ignores parameters. Nice! :) If there's a python.exe in that directory, try my invocation above with python.exe instead of Portable-Python.exe. – Charl Botha Mar 21 '13 at 16:32