0

Currently i try to implement my own mechanism for importing (Iron)Python files, but i'm note sure, if i'm consedering all cases.

For example i have the current structure in the filesystem for thesting the system:

/Sample/
    /ImportResolver/
        /Math/
            /__init__.py
            /Math2.py

__init__py contains:

# Sample math package
import Math2

Math.py contains:

# Add two values
def add(x, y):
    return x + y

Now if i execute the following import statement:

import Sample.ImportResolver.Math

My code tries to resolve the following python scripts, when importing:

Try import sub package: Sample/init.py
Try import sub package: Sample/ImportResolver/init.py
Try import module: Sample/ImportResolver/Math.py
Try import package: Sample/ImportResolver/Math/init.py

Looking at the list above, am i trying to import all things needed, or is the mecahnism completly wrong?

Furthermore, which files should the importer try to import when executing:

from Sample.ImportResolver import Math

Please tell me if it is totally unclear what i am looking for. Thank you!

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • Why would you reimplement a core part of the Python language? Do you have some special requirements for how `import` should work? If so, how can we possibly know whether your code is correct? You haven't specified your custom behaviour. – ChrisGPT was on strike Jan 03 '16 at 19:15
  • My custom behaviour is to provide a kind of import resolver, to import scripts from databases, web services and so on. – BendEg Jan 03 '16 at 19:19
  • @BendEg, have you seen [this answer](http://stackoverflow.com/a/34117632/468244) ? Does that help you out? You would just build on top of the existing infrastructure instead of replacing it. – Simon Opelt Jan 03 '16 at 20:01
  • Ok, thanks, this helps. I take a look at `path_hooks`, this seems to be the best way for completly custom importer... – BendEg Jan 03 '16 at 20:20

0 Answers0