1

In mydir/test/testing/pqtest.py

import os,sys
lib_path = os.path.abspath('../../lib/mine')
sys.path.append(lib_path)
import Util <---- get a traceback
import string
import random
# my code

 Traceback (most recent call last):
 File "pqtest.py", line 5 in ?
 import Util
 File "mydir/lib/mine/Util.py", line 89
 yield v if l > 0 else '' Error is point at if
SyntaxError: invalid syntax

However, there are other files that import Util.py inside mydir/lib/mine that does not have any problems with this file.

So why is it giving me this traceback when i am importing from somewhere else, in this case mydir/test/testing?

Syntax error on yield v if l> 0 else ''

def expand():
 for l,v in zip(field,val):
 yield l
 yield v if l > 0 else ''

this is good for python 2.5 but not for python 2.4

I am assuming I need to tell pqtest.py to use python 2.5 but not sure how

Stephan
18.4k8 gold badges38 silver badges61 bronze badges
asked Aug 7, 2013 at 18:54
16
  • 1
    You've skipped the most iportant part of the Exception traceback: the actual Exception... Commented Aug 7, 2013 at 18:57
  • I'm guessing the error is no such module? Commented Aug 7, 2013 at 18:57
  • @Stephan syntax error Commented Aug 7, 2013 at 18:58
  • @ealeon you showed us the wrong code, we are looking for a yield statement on line 89 in Util.py Commented Aug 7, 2013 at 18:59
  • 1
    Are you perhaps starting the failing script with python 2.4? The ternary if-then-else operator got introduced in 2.5. Commented Aug 7, 2013 at 19:01

2 Answers 2

1

If you're willing to change Util.py, the obvious thing to do is to rewrite the code so it's 2.4 compatible. From a comment, the only reason not to change Util.py is:

... others are depending on it as python 2.5.

But as long as your new code has the exact same effect on Python 2.5+ as the original code, this isn't a problem.

For example, instead of this:

def expand():
 for l,v in zip(field,val):
 yield l
 yield v if l > 0 else ''

Do this:

def expand():
 for l,v in zip(field,val):
 yield l
 if l > 0:
 yield v
 else:
 yield ''

Now the other people who are depending on it as python 2.5 will have no change, but it will also work in 2.4.

answered Aug 7, 2013 at 19:45
Sign up to request clarification or add additional context in comments.

7 Comments

this was my original answer, i was told "changing it is not an option"
@Stephan: Your original answer changed the API of expand in a way that would break all users of Utils.py; mine leaves the behavior exactly the same. Also, note that the OP said changing it is "not an option because others are depending on it as python 2.5". This change allows those others to continue to depend on it at python 2.5, with no changes to their code required.
wrong one, my original one before the edit had the same if statement you're showing
@Stephan: I linked to revision 1, before the edit. At any rate, even if you did have an equivalent answer, the OP clearly didn't understand that, given the quoted wording to his comment. So, instead of changing it to a different answer that wasn't appropriate, you should have explained why you answer wouldn't affect the 2.5 users.
i made that third answer before he mentioned the 2.5 users the answer was not inappropriate before the change in criteria (btw those links dont work for sub 20k users)
|
1

run python by itself by typing

python

If it shows less than 2.5, then you can't use the ternary conditional syntax. That was introduced in 2.5

If it DOES show 2.5 you can do this

python pqtest.py

to force pqtest.py to use that version

answered Aug 7, 2013 at 19:04

5 Comments

ah okay but what if python is 2.4?
you tell me what it is @ealeon
@ealeon if you see 2.5 when you type python, this solution will work for you. Let me know if it doesn't
well python is 2.4 but i solved the problem by making pqtest.py exec and having #!/usr/bin/python2.5 in the code.
@ealeon i'd recommend posting that as an answer and accepting it since none of these were the right answer for you.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.