0

I have this project structure

utilities.py 
/references
    module1.py
    module2.py
    ...

Into the utilities.py file, I would like to have a function with dynamically import with parameters like that :

def import(self, file):    
    from references.file import file  
    ...

Into the utilities.py file I use

import("module1")

But it doesn't work, I have the following error

from references.file import steps ModuleNotFoundError: No module named 'references.file'

I need some help, thank you

1 Answer 1

1

If you want to import using string names then try the importlib.import_module() function.

You could have a function that worked something like:

def dynamic_import(file):
    importlib.import_module(f'references.{file}')

For even more control you could use the __import__() builtin, but that's more involved and not really recommended

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.