2
import weka.core.jvm as jvm
jvm.start()

data_dir = "C:/Data/Python/Weka/Data/"
from weka.core.converters import Loader
loader = Loader(classname="weka.core.converters.ArffLoader")
data = loader.load_file(data_dir + "logistic.arff")
data.class_is_last()

print(data)

I'm executing the above example code of weka python wrapper from their doc. So I'm sure that there's no problem in the code. All modules are installed. But the code is not working when it is run as script (by pressing F5 in IDLE). It is throwing the following error:

Traceback (most recent call last):
  File "C:\Data\Python\Weka\weka.py", line 1, in <module>
    import weka.core.jvm as jvm
  File "C:\Data\Python\Weka\weka.py", line 1, in <module>
    import weka.core.jvm as jvm
ImportError: No module named core.jvm

But the code works when I copy and paste it line by line to the IDLE command prompt. No idea why. Where did I go wrong?

  • What have you named the script exactly? Make sure it doesn't clash in any way with any of the modules you are trying to import. – Totem Nov 22 '15 at 19:03
  • 1
    My first hunch is that the current directory, PYTHONPATH, or even Python program (ie. idle uses 3x and the shell is setup to use 2.7) launched differs - http://stackoverflow.com/questions/15252040/how-does-python-find-a-module-file-if-the-import-statement-only-contains-the-fil – user2864740 Nov 22 '15 at 19:05
  • Changing the name from weka.py to something else worked. – user3554510 Nov 22 '15 at 19:11

1 Answers1

5

Try changing the name of your file to something that's not the name of any of the module you're importing. For example change weka.py to myscript.py

Rahul
  • 3,208
  • 8
  • 38
  • 68
  • The clue to Rahul's answer is in the traceback. The first line is the file being executed as the main modules. The third line is the file it attempts to import. Both are the same. Note that `import weka.core.jvm` does `import weka` first, then `import weka.core`, and finally the import requested. – Terry Jan Reedy Nov 23 '15 at 15:28