0

I'm trying to use HTTPImport dynamically in Python , consider the following code:

downloadFrom = '------------SOME-DOMAIN---------------'
globals()['downloadMe'] = 'EmployeeExampleClass'
print('downloadMe value is ===>>> ' , downloadMe ) ##### Prints: EmployeeExampleClass
     
with httpimport.remote_repo([downloadMe], downloadFrom):
        from downloadMe.model import EmployeeExample

This piece of code throws: ModuleNotFoundError: No module named 'downloadMe'

How can we use a dynamic name when Importing modules with Python and globals ?

JAN
  • 21,236
  • 66
  • 181
  • 318
  • Any reason not to use `importlib` instead of manipulating `globals` dict? – buran Apr 11 '22 at 08:41
  • @buran: Does it solve our problem ? – JAN Apr 11 '22 at 08:50
  • check https://stackoverflow.com/q/301134/4046632 – buran Apr 11 '22 at 08:57
  • @buran: I can't understand how does it solve the above.. – JAN Apr 11 '22 at 09:30
  • You need to import module in order to use it. Right now you jsut create a name `downloadMe` that is bound to a str literal `EmployeeExampleClass`. Put a side that `'EmployeeExampleClass'` is misleading name for package/module. From what you show it looks like you want to dynamically import module given it's name as string. At least that is what I understand from your post/snippet. – buran Apr 11 '22 at 09:58
  • If you do `print(type(downloadMe))` you will see it's `` – buran Apr 11 '22 at 10:02
  • @buran: Yes , it is a string when I check the type , but when I try to use this string as a variable I get the `ModuleNotFoundError`. – JAN Apr 11 '22 at 10:58

0 Answers0