5

My python scripts run fine from IDLE, but when I try to run them from the command-line, things go wrong. First I had trouble importing pygame, but I added C:\Python27\Lib\site-packages to the PYTHONPATH environment variable and all was well, I thought. However, now when I attempt to run something from the command line, I get this:

C:\Users\Ian Sinke\Documents\Pong>python pong.py
'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "pong.py", line 3, in ?
    import pygame
  File "C:\Python27\Lib\site-packages\pygame\__init__.py", line 27, in ?
    import sys, os, string
  File "C:\Python27\Lib\os.py", line 63, in ?
    import ntpath as path
  File "C:\Python27\Lib\ntpath.py", line 401
    backslash, dot = (u'\\', u'.') if isinstance(path, unicode) else ('\\', '.')

                                    ^
SyntaxError: invalid syntax

Any ideas?

This is not a localize problem; when I try to run another script from the command line, I get this:

C:\Users\Ian Sinke\Documents>python app.py
'import site' failed; use -v for traceback
Traceback (most recent call last):
  File "app.py", line 4, in ?
    import urllib2
  File "C:\Python27\Lib\urllib2.py", line 92, in ?
    import base64
  File "C:\Python27\Lib\base64.py", line 346
    with open(args[0], 'rb') as f:
            ^
SyntaxError: invalid syntax

and that syntax is definitely OK...

Pong.py begins like this:

#import sys
import math
import pygame
import time
from pygame.locals import *

# Helper functions
def centerdist(paddletop, balltop):
    return balltop - paddletop - 30

# Constants
BLACK = 0, 0, 0

pygame.init()
11
  • Are you sure that the script is exactly the same as the one you are running from the IDE? Becuase it looks like the if statement is on the wrong line from the stactkrace. Commented May 10, 2012 at 19:07
  • It's the exact same script, the if statement isn't even in my file! It's in C:\Python27\Lib\ntpath.py. That syntax looked weird to me too... Commented May 10, 2012 at 19:09
  • Why are you using ntpath instead of os.path? Commented May 10, 2012 at 19:11
  • ntpath is part of the python stdlib, and works fine for me importing from the interactive interpreter on Win 7. I think the import site error is more important. Commented May 10, 2012 at 19:11
  • @japreiss it's a stack trace, that's the call from lib\os.py. He isn't doing it himself. Commented May 10, 2012 at 19:12

2 Answers 2

10

This sounds to me like you've got two different versions of Python on your computer. One is a more recent version that accepts Python's version of the ternary expression, and one is an older version. When you use IDLE, the newer version is called. When you use the command line, the older version is called. You can confirm or disprove this hypothesis by running python -V from the command line.

To elaborate, support for conditional expressions was added in Python 2.5. So when you modified PYTHONPATH, you wound up running a newer python file (from 2.7, it sounds like) with an older version of python (2.4, according to your test).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I found it... the culprit was Lilypond, which was running a separate Python24 exe and had added its bin directory to the PATH ahead of my Python27 bin. So I removed it and all is well. Thanks for your help guys!
0

Make sure your command-line python is at least version 2.5 because, before then, there was no ternary operator (http://marc-abramowitz.com/archives/2008/05/18/python-ternary-operator/).

1 Comment

Yeah, that was the problem, kinda.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.