Liondancer
Liondancer

Reputation: 16479

cannot find module in another directory

I have the project structure:

/hdfs-archiver
   /logs
   /qe
      __init__.py
      /tests
         __init__.py
         archiver.py
      /utils
         __init__.py
         HdfsConnector.py

I am trying to run archiver.py but I am getting this error:

Traceback (most recent call last):
  File "qe/tests/HdfsArchiver.py", line 8, in <module>
    from qe.utils import HdfsConnector
ImportError: No module named qe.utils

I read around and it seemed like most people that come across this issue fixed it with __init__.py

when I pwd:

$ pwd
/Users/bli1/Development/QE/idea/hdfs-archiver

my PYTHONPATH in .bashrc

export PYTHONPATH=$PYTHONPATH:/Users/bli1/Development/QE/idea/hdfs-archiver

I also tried having my PYTHONPATH as

/Users/bli1/Development/QE/idea/hdfs-archiver/qe

Upvotes: 0

Views: 3136

Answers (2)

adit-39
adit-39

Reputation: 79

Firstly, you could try a relative import such as

from ..utils import HdfsConnector

You'd also need to run the script as a module and not as a simple python script due to the __name__ being different. This wouldn't require you to modify the path. You can find more info here.

Upvotes: 1

Michelle Welcks
Michelle Welcks

Reputation: 3914

You're trying to import HdfsConnector as a function or class. Include the HdfsConnector module as part of your absolute import:

from qe.utils.HdfsConnector import my_function

You can also import the module:

import qe.utils.HdfsConnector
# or
import qe.utils.HdfsConnector as HdfsConnector

Upvotes: 1

Related Questions