2
$\begingroup$

In VSC, this code works

import bpy
from subp.panel import Panel

Panel.test()

However, in the Blender text editor, Suddenly it doesn't work

ModuleNotFoundError: No module named 'subp'

If I change the second line to from .subp.panel import Panel then

ImportError: attempted relative import with no known parent package

Why can't I run the code in the blender text editor like this?

When I move the panel.py to the outside of the subp folder and change to from .panel import Panel, it throws the same error

ImportError: attempted relative import with no known parent package

$\endgroup$
7
  • $\begingroup$ Try removing the first "." from the from .subp.panel import Panel and see if that works. $\endgroup$
    – Jakemoyo
    Commented Apr 26, 2022 at 19:51
  • $\begingroup$ @Jakemoyo It doesn't work neither. Terminal throws the errorModuleNotFoundError: No module named 'subp'. $\endgroup$ Commented Apr 26, 2022 at 19:56
  • $\begingroup$ Do you have an __init__.py file in the root of your custom module? $\endgroup$
    – Jakemoyo
    Commented Apr 26, 2022 at 19:58
  • 1
    $\begingroup$ blender.stackexchange.com/a/181036/86891 ? $\endgroup$
    – Gorgious
    Commented Apr 26, 2022 at 20:03
  • $\begingroup$ @Jakemoyo I think it is, yes. My directory looks like this Overlay - __init__.py, panel.py right now. $\endgroup$ Commented Apr 26, 2022 at 20:25

1 Answer 1

3
$\begingroup$

EDIT: Hopefully clearer answer in response to comments.

The short answer is 'No, you can't do that'.

The long answer

The reason this works in VSC is that Python has a system variable called PYTHONPATH. Python searches all of the directories in PYTHONPATH to find the file. VSC knows this and adds the directory containing your module to PYTHONPATH.

For various reasons, Blender ignores PYTHONPATH by default. So it can't find modules that VSC can.

The workaround

You can work around this by

  • running Blender with the command line argument --python-use-system-env and
  • Adding the parent directory of your module to the search path using code along the lines of
import sys
sys.path.append("PATH_TO_YOUR_MODULE")

before you try to import it. That's only the path to the directory containing the module. Don't include the module name.

Background

You may want to read the Python manual section on the import system; especially the paragraph about searching. The key bit has to do with import path

A list of locations (or path entries) that are searched by the path based finder for modules to import. During import, this list of locations usually comes from sys.path, but for subpackages it may also come from the parent package’s path attribute.

For a discussion of Blender and PYTHONPATH, see this answer

$\endgroup$
7
  • $\begingroup$ Then what are the other custom addons? They still use normal import syntax for fetching their own sub-modules especially like this one; github.com/curtisjamesholt/BY-GEN-public $\endgroup$ Commented Apr 27, 2022 at 4:21
  • $\begingroup$ Custom addons are Python files. That's why ordinary syntax works in them. The console is not a Python file. $\endgroup$ Commented Apr 27, 2022 at 13:47
  • $\begingroup$ So you say I can't use the import syntax on built-in text editor of the Blender because it doesn't support? Sorry if I bother you I can't fully understand because I'm not a Native English speaker. $\endgroup$ Commented Apr 27, 2022 at 14:00
  • $\begingroup$ Don't worry, you're not bothering me. I have not written a clear enough answer, so I will try to clarify it. $\endgroup$ Commented Apr 27, 2022 at 14:56
  • $\begingroup$ Thanks for understanding me. Speaking of which, I found a related answer from a question which has the same problem with me. blender.stackexchange.com/questions/183773/… I followed exactly same way such as changing files’ and folders' names, inserting register-unregister functions to every each .py files etc.. But sadly, it still throws the attempted relative error. $\endgroup$ Commented Apr 27, 2022 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .