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?

2
  • Are you running mock.py directly (as opposed to importing it from somewhere else)? Commented Sep 11, 2015 at 17:53
  • @BrenBarn I'm running it directly. Commented Sep 11, 2015 at 18:29

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.