0

I have the following folder hierarchy.

apriltag
    python
        apriltag.py
my_notebook.ipynb

I simply want to import apriltag.py into my Jupyter Notebook. I tried doing it this way..

import sys
import os.path

sys.path.append(os.path.dirname(os.path.realpath('__file__')) + "/apriltag/python")

import apriltag

However, when I try to access a class from apriltag, like so:

print(apriltag.Detector)

I get the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-74-d1254ec9a372> in <module>()
     12 import apriltag
     13 
---> 14 print(apriltag.Detector)

AttributeError: module 'apriltag' has no attribute 'Detector'

Suggesting that the module was not imported correctly. I've tried creating __init__.py at the root of the python directory as well but the same thing occurred.

5
  • Does from apriltag import Detector work? Commented Sep 29, 2017 at 17:16
  • dir(apriltag) will probably tell you a lot. Commented Sep 29, 2017 at 17:21
  • @TreytenCarey cannot import name 'Detector' Commented Sep 29, 2017 at 17:42
  • @ScottMermelstein this is the output ['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__'] not sure how to interpret it Commented Sep 29, 2017 at 17:43
  • That's an empty module. The next thing I would check is print(apriltag.__path__), and see if it's pointing to apriltag/python/apriltag.py, or just apriltag. (Compare both the results of the dir and the path to what you'd get by inspecting sys or some other thing that imported well. dir(sys) and print(sys.__path__) would give you an idea of what to expect when it's properly loaded. Commented Sep 29, 2017 at 18:01

1 Answer 1

2

Ok, I'm going to buzz in a little bit early. I may have the solution to your problem. If I don't, we can go back to figuring it out.

As you've probably seen, the docs say

The directory containing the script being run is placed at the beginning of the search path,

Now, you're running my_notebook.ipynb, so it's directory is the very beginning of the search path. And you probably have an __init__.py file in the apriltag directory that's at the same level. As such, it's being found as a module, and loaded. And there are no .py files in that "module", and nothing in your __init__.py there, so it's loading as an empty module.

Instead, don't try modifying the sys.path directory, just make empty __init__.py files in both the apriltag and the python directories.

Then, you should be able to do the following:

from apriltag.python import apriltag

The apriltag that gets imported should be the one you need. (And dir(apriltag) will give you much nicer results.)

Generally, I always set up all my code in directories with empty __init__.py and invoke them all with the from syntax. It made modules a lot easier to handle.

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

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.