ealeon
ealeon

Reputation: 12452

Python Error when imported from different directory

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

Upvotes: 1

Views: 120

Answers (2)

Stephan
Stephan

Reputation: 17971

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

Upvotes: 1

abarnert
abarnert

Reputation: 365657

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.

Upvotes: 1

Related Questions