0
$\begingroup$

I'm making a file importer addon (for a 3d model) for blender. Since the file format I'm working on is in binary ,for that I have been using a binaryreader for reading the needed values.

The binary reader that I'm using is a module that you can install with pip function and it works like how its intended to do when you run your scripts with it in VScode and other python ides

Now my addon structure looks like this

Addon
-> __init__.py
->importer.py
->binaryreader.py

When it comes with importing custom modules, it does its job and gives the output I require.

For an example

(bl_info block)

def some_data_block(some_values etc)

from . import test
print(test.add(20,20)) #gives the result as intended ,ans=40

However the problem arises when I try to import a module with classes (like the binaryreader used here)

(bl_info block)

def some_data_block(some_values etc)

from . import BinaryReader 

f = open(filepath, 'rb')

print(BinaryReader.read_str(3)) #prints the first 3 bytes of the file as a string

return {'FINISHED'}

(some other related code)

Now if you run this code (compile it to an addon first!) you will get a:

AttributeError: module 'addon.BinaryReader' has no attribute 'read_str'

I dont know why it causes this error, i tried using code snippets from other answers about import module related problems but it either causes an error or just does not work the way i want it to work, any help would be appreciated in fixing this error

$\endgroup$

1 Answer 1

0
$\begingroup$

After some research on quite a couple of python import related errors and troubleshooting, the solution to fix this problem is to:

remove: "__name__" =" __main__" this specific line causes relative imports to not just work for some reason

Also set your import lines to something like this:

from .binary_reader import BinaryReader

It might be a silly reason for why I cant import classes from another file but i hope this helps :)

$\endgroup$

This site is temporarily in read-only mode and not accepting new answers.

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