Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python import as a variable name

I wanted to use import with a variable name. For example I wanted to do something like this

from var import my_class

I went through pythons documentation, but seems thats a little confusing. Also I seen some other posting on stack overflow that give the example of something like this

import importlib
my_module = importlib.import_module("var, my_class)

This second example does work to a certain extent. The only issue I see here var is imported but I don't see the attributes of my_class in python's namespace. How would I equate this to my original example of

from var import my_class
like image 600
Max Powers Avatar asked Sep 12 '25 01:09

Max Powers


1 Answers

Here's how to use importlib (there is no need for the second parameter):

var = importlib.import_module("var")

# Now, you can use the content of the module:
var.my_class()

There is no direct programmable equivalent for from var import my_class.

like image 171
DYZ Avatar answered Sep 13 '25 17:09

DYZ