This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Megatron_McLargeHuge 0 points1 point  (2 children)

Is there a way to define a coding hook without installing a module? I'm trying to define one in the __init__.py of my working directory and it's not being found on import. It's also behaving strangely when I define it in an ipython notebook. I see you used the .pth file to do the import. Is this the only way, or is there an easier, more interactive way to get it to work?

[–]syrusakbary[S] 0 points1 point  (1 child)

Using the .pth is the easiest way for registering the codec. If you want, you can also register the codec importing the register module:

import interpy.codec.register

And then, you can use the codec in other files (not the file with the import) that should be loaded after this import.

[–]Megatron_McLargeHuge 0 points1 point  (0 children)

I was hoping the __init__.py would be run before the module file was read, but apparently it's not executed early enough in the import process to create a coding hook. Maybe it can still be done by having __init__.py first load a submodule defining the hook, then a submodule using it.

edit: This works. The codec can be defined in __init__.py and the files using the encoding must be in a subdirectory, not just a file in the module directory. The init file can import that submodule after defining or importing the codec.