3
$\begingroup$

I'm having the hardest time trying to import a few things. I have an addon that has multiple files all under one folder with an __init__.py in the base. I'm performing all of these imports from this __init__.py. The addon folder resides in my addon folder as scripts/addons/RLD_Toolkit

My directory hierarchy looks like this Directory Hierarchy

The part of the script where it errors

def register():
    #These imports do not work
    from . import rld_panel
    import rld_panel

    #This import does work
    import RLD_Toolkit.rld_panel

The error I'm getting is

Import Error: cannot import name `rld_panel`

Why won't it perform the relative imports (or even the straight up import) from my __init__.py?

Edit: Here's a printout of my globals() variables and sys.path globals and path

$\endgroup$
6
  • $\begingroup$ My guess: Blender screws up the pythonpath. Print it and see if it makes sense. $\endgroup$
    – Mörkö
    Sep 5, 2015 at 6:08
  • $\begingroup$ @user277143 Updated post with printout of globals and sys.path $\endgroup$
    – Cobertos
    Sep 5, 2015 at 6:13
  • $\begingroup$ As you can see, the folder you are in isn't in the import path. As addons is in the python path, saying import RLD_Toolkit.rld_panel imports from the module (that is the whole folder with your init.py) the file rld_panel.py. I would advise you to add the current folder into the path. $\endgroup$
    – Mörkö
    Sep 5, 2015 at 6:15
  • $\begingroup$ I'm confused? You're talking about __path__ right? That's the folder with all my stuff in it. $\endgroup$
    – Cobertos
    Sep 5, 2015 at 6:16
  • $\begingroup$ Nope. path is the path of the file. When you try to import things, python looks in sys.path, which doesn't contain the folder. $\endgroup$
    – Mörkö
    Sep 5, 2015 at 6:18

1 Answer 1

1
$\begingroup$

To answer the question, you will have to add the line:

sys.path.append(__path__)

before importing anything. This will add the current folder into the path Python looks in for imports. Then you can import files with:

import foo

instead of:

from bar import foo

.

$\endgroup$
4
  • 1
    $\begingroup$ This fixes it. Can you provide any insight as to why this won't work for me yet I have two other addons that perform normal relative imports and it works? Python + Blender is kind of a mystery to me sometimes so some speculation on the inner workings would be helpful to know. $\endgroup$
    – Cobertos
    Sep 5, 2015 at 6:26
  • 1
    $\begingroup$ No living being can hope to understand what happens inside Blender. Turn around, mortal. Your quest is hopeless. $\endgroup$
    – Mörkö
    Sep 5, 2015 at 6:28
  • $\begingroup$ I need to publish an update to this with code samples, through browsing the Blender Python bindings and trial and error, I've kind of come up with a home brew relative import system that even supports reloading (as long as your code is written in a specific way that doesn't reuse globals and unregisters everything properly). $\endgroup$
    – Cobertos
    Dec 15, 2017 at 18:21
  • $\begingroup$ This doesn't work with currently Blender / Python. It doesn't recognize __path__ anymore. $\endgroup$ Sep 28, 2021 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.