Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6801741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:08:14+00:00 2026-05-26T19:08:14+00:00

With my python application, I have 40 modules (classes) which contain a parser for

  • 0

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.

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

However…. How do you actually do this in python?

eg;

file_name = 'lex_parser'
class_name = 'LexParser'

how can I do….

from {file_name} import {class_name}
Parser = {class_name}()

Follow what I mean?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:08:14+00:00Added an answer on May 26, 2026 at 7:08 pm

    Try this:

    file_name = 'lex_parser'
    class_name = 'LexParser'
    
    Parser = getattr(__import__(file_name), class_name)
    

    Note that file_name must not contain .py.

    This won’t work if the module is within a package because __import__ would return the top level package. In that case you can do this:

    import sys
    
    file_name = 'parsers.lex_parser'
    class_name = 'LexParser'
    
    __import__(file_name)
    Parser = getattr(sys.modules[file_name], class_name)
    

    This will work in both cases and is recommeded by the __import__ function documentation.

    In both examples Parser is a class which you have to instantiate as normally:

    parser = Parser()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Python web application in which I have some small modules
I'm thinking how to arrange a deployed python application which will have a Executable
I have a Python application in a strange state. I don't want to do
I want to run javascript/Python/Ruby inside my application. I have an application that creates
I have complex GUI application written in Python and wxPython. I want it to
We have a Linux application that makes use of OpenSSL's Python bindings and I
I am trying to use py2exe to distribute a python application I have written.
I have an application that consists of multiple python scripts. Some of these scripts
I have a large python code with many modules and classes. I have a
I have a C++ application dynamically linked to the Python interpreter. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.