operatorequals/httpimport

Relative Path Importing/incorrect import package

rkbennett opened this issue · 2 comments

This is heavily related to #28, though I believe they were half way incorrect when they said importing a second time would result in a working execution. Based on what I've seen, when the import fails it ends up in a half-way state where the import isn't shown in globals() or dir(), but can be seen from sys.modules(). This breaks some stuff and likely won't result in an actually working function/module.

Took some time to dig into this issue and it's not actually an issue with relative path imports, that's more of a symptom, the actual issue stems from the fact that nested modules don't get the proper value set for their __package__ attribute.

This is caused by line 224 in the current code where, no matter what the path is, it splits it and grabs the first item in the index and uses that as the package. So a for example:

Package1_
         |_Module1
         |_Package2_
                    |_Module2
                    |_Module3

If you have the following file structure and Module3 contains 'import .Module2' that will fail, because python uses the __package__ attribute to determine relative paths and all of these modules have Package1 as their __package__.

Thank you so much for your detailed bug report and PR!
I am looking into it!

I'll go ahead and close this now, since it's been merged