Dynamically import and create class instance
<p>With my python application, I have 40 modules (classes) which contain a parser for some text. In my function I only want to instanciate and use a particular module. These are all sorted in the database.</p>

<p>I am at the point now where I know my parser, and have both the python file name and class I want to import and create</p>

<p>However.... How do you actually do this in python?</p>

<p>eg;</p>

<pre><code>file_name = 'lex_parser'
class_name = 'LexParser'
</code></pre>

<p>how can I do....</p>

<pre><code>from {file_name} import {class_name}
Parser = {class_name}()
</code></pre>

<p>Follow what I mean?</p>
