Programatically importing a file and creating an instance of a class in Python

Posted by benofsky on Stack Overflow See other posts from Stack Overflow or by benofsky
Published on 2010-05-02T15:23:00Z Indexed on 2010/05/02 15:27 UTC
Read the original article Hit count: 192

Filed under:
|

I have a (python) list of strings which refer to python source files and subsequently classes within those files which I want to import and then create an instance of the classes within the files (everything follows a strict naming convention, making this theoretically possible), in Ruby I would do something like:

require "lib/sources/#{source}.rb"
s = source.constantize.new

How would I do something similar in Python?

Thanks, Ben

© Stack Overflow or respective owner

Related posts about python

Related posts about metaprogramming