Note: I edited your question to wrap the code with [code]…[/code] for formatting.
So yes, using a string to dynamically load python is generally bad, but it’s important to know why. If you are getting this class name from the user as input then there’s a very good chance they can type in Python code you didn’t expect and cause it to execute. Evaluating strings isn’t bad on its own. Technically every web framework on the planet does this in its templating language. What’s bad is if you eval strings that come from the user as those are nearly impossible to restrict correctly. In fact, trying to detect bad code in strings from users was proven impossible by Alan Turing, so nobody is going to be able to do that. https://en.wikipedia.org/wiki/Halting_problem
There is also the https://docs.python.org/3/library/inspect.html module, but since you’re setting variables and not necessarily just looking up a class, you might not find that too useful. It would still have the same problem as above though since, if I realize you’re setting variables to classes, I can simply find a way to make you set some important variable to whatever I want. You really are stuck in this case.
To make this work safely, you need to have these variables placed in a safe container that doesn’t actually impact your code, and the best place I can think of is a dict. In theory there’s no reason to do:
joe = Person()
When you can also just do this:
people['joe'] = Person()
That’d be safer, works better with how you’re probably using it, turns all your interactions with these objects into data not code, which is generally easy to code against generically. For example, how do you tell if one of these objects does not exist? You can do it in Python but python generally assumes variables exist and throws errors when they don’t. Put them in a dict and you can tell right away.
The final thing is, you could possibly create a module, and that module will have a dict (look at https://docs.python.org/3/library/inspect.html again), then you can set the variables in the module and get the ability to use these as variables while also firewalling them into a safe place that can’t be modified. Still, it’s not going to be as definitely safe as using a dict, but it might be a fun exercise to see how that’d work.