Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp module deprecated #325

Open
BenBradnick opened this issue Jan 29, 2021 · 1 comment
Open

imp module deprecated #325

BenBradnick opened this issue Jan 29, 2021 · 1 comment

Comments

@BenBradnick
Copy link
Contributor

The imp module is deprecated and needs replacing with importlib:

DeprecationWarning: the imp module is deprecated in favour of importlib;
see the module's documentation for alternative uses
    import imp

This is a bit more involved than just a straight replacement. This may help: https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path

@BenBradnick
Copy link
Contributor Author

I've had a play around with using importlib as a replacement but I can't quite get it working:

def import_package_from_path(name, path):
    print(f"Importing package {name} at path: {path}")
    dirname, basename = os.path.abspath(path).rsplit(os.sep, 1)
    print(f"Base name: {basename}, dirname: {dirname}")
    # spec = importlib.util.find_spec(basename, package=path)
    spec = importlib.util.find_spec(basename, package=dirname)
    if spec is None:
        spec = importlib.util.spec_from_file_location(basename, path)
    print(f"Spec: {spec}")
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    print(f"{dir(mod)}")

In the defines unit test, no spec is found and so the module from spec fails.

When testing with the simulator, the spec is found and the exec_module succeeds, but:

  1. the module doesn't have the same attributes as the module instance returned from imp.load_module
  2. it looks like when using imp.load_module an alias is used based on the name defined in the YAML, whereas when using importlib I have to give the package name (i.e. name of the directory where the module(s) are located) and I don't see how to substitute it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant