0

I am trying to import a module from another directory and run the script using python-mode. I am encountering, module not found error, but the module is present in the location and my sys.path shows the module path has been added successfully. I am having hard time troubleshooting/fixing. Could some one shed some light on this?

import numpy as np
import sys
sys.path.append('./extnsn/')
from extnsn import FX

The error stack is:

Feat/mFeat/feat_Xt_v2.py|7 error| in from extnsn import FX ImportError: No module named 'extnsn'

My directory structure is:

Feat
    |
    |--mFeat
    |
    |--feat_Xt_v2.py
    |
    |--extnsn
            |
            |--__init__.py
            |--FX.py

The extnsn directory has a __init__.py with the following:

from extnsn import FX

FX.py is the module name, for information.

sys.path contains the appended path as ./extnsn/ as the last entry in the list.

What makes me conclude this is not path issue is that, the program runs fine, if executed from atom with script plugin.

Any help is much appreciated.

EDIT:

This doesn't seem to be an issue with just python-mode, rather the way how vim invoke the python interpretor and execute the buffer. I tried with the following command without python-mode and the issue is the same.

Bussller
  • 1,961
  • 6
  • 36
  • 50

1 Answers1

2

To import a module or a package you have to add to sys.path its parent directory. In your case if you've added ./extnsn/ to sys.path you cannot import extnsn (it cannot be found in sys.path) but you can import FX directly:

import FX

But as FX seems to be a module in a package extnsn you better add to sys.path the parent directory of extnsn, i.e. Feat:

sys.path.append('../Feat')
from extnsn import FX
phd
  • 82,685
  • 13
  • 120
  • 165