0

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
6
  • @hiroprotagonist yes it is. Still I get the same error Commented Nov 30, 2015 at 8:58
  • Where are you running the file from? So, are you typing python ~/Development/QE/idea/hdfs-archiver/qe/tests/archiver.py or are you typing python archiver.py? Commented Nov 30, 2015 at 9:06
  • @BurhanKhalid I am currently in the qe directory and I am running python tests/archiver.py. Ideally I could like to be able to run archiver.py from any directory and still have it work so long as I provide the correct path to archiver.py Commented Nov 30, 2015 at 9:18
  • would it make any difference if you had __init__.py in the hdfs-archiver directory (root). Commented Nov 30, 2015 at 9:19
  • The qe module is not in your PYTHONPATH variable, which is why the import is not working. You can get around this, by doing a relative import. Commented Nov 30, 2015 at 9:21

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.