4

I have some folders and .py files in the following structure:

parent/
       __init__.py
       test.ipynb
       code/
            __init__.py
            common.py
            subcode/
                    __init__.py
                    get_data.py

In the __init__ file under the parent folder, I have import code and in the one of code, I have import subcode. But when I tried import code.subcode, I got such an error:

ImportError: No module named 'code.subcode'; 'code' is not a package

But when I just import code, no error is thrown. However, when I call code.subcode, this error happens:

AttributeError: module 'code' has no attribute 'subcode' 

I try all of those mentioned above in the test.ipynb, which is at the root of the directory.

Do you know what is the reason and how can I fix it? Thanks!

7
  • Try just leaving your __init__.py files empty. Commented Jul 29, 2016 at 2:26
  • @DrK Same thing happened....... Commented Jul 29, 2016 at 2:27
  • 1
    After you import code does code.__file__ point to the directory that you expect? Commented Jul 29, 2016 at 2:32
  • @Dr.k Oh, really no....... it points to '/home/lcc/anaconda3/envs/parent/lib/python3.5/code.py'. Why does it happen... Commented Jul 29, 2016 at 2:34
  • My guess is that your PYTHONPATH environment variable does not contain your parent directory and if it does, then it is placed lower down in the list than the path to the code module that you are actually importing. Solution: add parent to your PYTHONPATH and maybe use a different name than code to avoid namespace collisions. Commented Jul 29, 2016 at 2:36

2 Answers 2

5

The problem is that you are importing another module named code that is installed on your system rather than your own module. You can verify this by checking the module file path in code.__file__ after you import code.

The first thing to do is change the name of your module to avoid namespace collisions with the other code package on your system. If your new package name doesn't collide with something else, you should now either successfully be importing it and have it behave as expected, or it fails to import entirely.

If it fails to import, it is most likely because your parent directory is not in your PYTHONPATH environment variable.

There can potentially also be other more technical reasons that a module is not recognized by the interpreter such as old definitions being cached (in which case restarting the interpreter is often enough. Possibly after deleting any precompiled versions of the module). Another problem I have seen ended up being that a module contained a bug that made the interpreter unable to parse it. I am sure there are other odd possibilities out there.

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

4 Comments

But shouldn't code be found in the current directory(parent/) before PYTHONPATH is searched? See: docs.python.org/2.7/tutorial/…
Given that parent is a package, the parent directory itself probably shouldn't be on the PYTHONPATH; the directory containing the parent directory should be there.
@user2357112 yeah, I was thinking that too but from the way he was importing, it seemed more like he meant for code to be the package. But you are right.
@sunqingyao You are right. He was probably also in the wrong directory. I gave your comment an up-vote.
2

You're on Python 3. You need to perform relative imports explicitly:

from . import code

The code module you're currently getting is the standard library code module.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.