0

Jython doesn't have the -m option, and it raises an error with from .utils import *.

The solution is not to use relative path

sys.path.insert(0, os.path.dirname(__file__))
from utils import *

As I need to use the code both Jython and CPython, I came up with this command:

try:
    from .utils import *
except Exception: # Jython or Python with python context/context.py invocation
    sys.path.insert(0, os.path.dirname(__file__))
    from utils import *

However, Jython doesn't seem to catch the exception, and still generates an exception.

  File "context/context.py", line 9
SyntaxError: 'import *' not allowed with 'from .'

Based on How to know that the interpreter is Jython or CPython in the code?, I tried

binary = sys.executable
if binary is None:
    sys.path.insert(0, os.path.dirname(__file__))
    from utils import *
else:
    from .utils import *

I still get the SyntaxError, why Jython interpreter keeps parsing from .utils import * when it was supposed to do so; I mean, this code works.

binary = sys.executable
if binary is None:
    sys.path.insert(0, os.path.dirname(__file__))
    from utils import *
else:
    pass

This is my Jython information:

Jython 2.7b1 (default:ac42d59644e9, Feb 9 2013, 15:24:52) 
[Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)] on java1.7.0_51
Type "help", "copyright", "credits" or "license" for more information.
Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • witch Jython version are you using? Depending on the version relative imports are not allowed – juankysmith Mar 10 '14 at 16:31
  • unrelated: there could be some [`import` issues if you name your package and a module within the same](http://stackoverflow.com/questions/14183541/why-python-finds-module-instead-of-package-if-they-have-the-same-name), don't: `xyz/xyz.py` if `xyz` is a package. – jfs Mar 10 '14 at 16:49
  • unrelated: do not add a directory *inside* a package to `sys.path`. It makes all the modules in the package available under two names (all modules became top-level modules). It may cause hard to debug issues with the imports and the global state. – jfs Mar 10 '14 at 17:14

2 Answers2

0

Somehow it doesn't like your relative import, I am thinking about 2 possibles solutions:

  • Try to import the utils package from the root of your project.
  • Add the path to utils package to your PYTHONPATH.
juankysmith
  • 11,839
  • 5
  • 37
  • 62
  • I have an easy way to solve the relative import, the real issue is that Jython keep parsing the code that was not supposed to do. I updated my post. – prosseek Mar 10 '14 at 17:02
0

issubclass(SyntaxError, Exception) says that it should be caught and indeed, if you raise it manually it is being caught:

try:
   raise SyntaxError
except Exception:
   print('caught it')

It prints caught it. Though

try:
     from . import *
except Exception:
     print('nope')

leads to:

 File "<stdin>", line 2
SyntaxError: 'import *' not allowed with 'from .'

If you add some code above then you see that it is not executed i.e., the latter exception is not runtime. It is the same in CPython e.g.:

import sys
sys.stderr.write("you won't see it\n")

try:
    1_ # cause SyntaxError at compile time
except:
    print("you won't see it either")

The error is raised during compilation.

To workaround the problem, you could try to use absolute imports:

from __future__ import absolute_import
from context.utils import * # both Jython and CPython
jfs
  • 399,953
  • 195
  • 994
  • 1,670