3

I have two Python files, one stored in the location /Python/plata.py, and another in the location /Python/tao/mock.py. This is what my plata.py file looks like::

def printSomething():
    print 'This is a test.'

I'm trying to import the printSomething() function inside my mock.py file as follows:

from . import plata

plata.printSomething()

However, this is the error I'm encountering:

Traceback (most recent call last):
File "/home/manas/Python/tao/mock.py", line 1, in <module>
from . import plata
ValueError: Attempted relative import in non-package

I've included the __init__.py files in locations /Python/__init__.py and /Python/tao/__init__.py as well. However, I'm still encountering the same error.

What seems to be wrong here?

Manas Chaturvedi
  • 5,210
  • 18
  • 52
  • 104

2 Answers2

5

The parent directory of the package is not being included in sys.path for obvious security reasons. But, anyway...

import sys
sys.path.append('..')

import plata

Hope this helps you!

3442
  • 8,248
  • 2
  • 19
  • 41
2

See What's the difference between a Python module and a Python package? for an explanation of Module vs Package. The short of it is that your Python directory is not a package. plata.py is a stand alone module and should be imported as import plata.

Community
  • 1
  • 1
Josh J
  • 6,813
  • 3
  • 25
  • 47
  • The linked reason for this being a duplicate still does not explain to OP why adding random `__init__.py` files is not the answer. `tao` is a package, `plata` is a module. My linked answer is the real reason why this does not work. – Josh J Sep 11 '15 at 18:50