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
Liondancer
  • 15,721
  • 51
  • 149
  • 255
  • @hiroprotagonist yes it is. Still I get the same error – Liondancer Nov 30 '15 at 08: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`? – Burhan Khalid Nov 30 '15 at 09: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` – Liondancer Nov 30 '15 at 09:18
  • would it make any difference if you had `__init__.py` in the `hdfs-archiver` directory (root). – GIRISH RAMNANI Nov 30 '15 at 09: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. – Burhan Khalid Nov 30 '15 at 09:21
  • @BurhanKhalid I added `qe` at the very end of my `PYTHONPATH` but still same error =[ – Liondancer Nov 30 '15 at 09:24

2 Answers2

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
Michelle Welcks
  • 3,513
  • 4
  • 21
  • 34
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.

Community
  • 1
  • 1
adit-39
  • 79
  • 6