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

  • SEARCH
  • Home
  • 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 9046191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:35:57+00:00 2026-06-16T11:35:57+00:00

Possible Duplicate: Dynamic module import in Python I intend to make a suite of

  • 0

Possible Duplicate:
Dynamic module import in Python

I intend to make a suite of files at some point soon, and the best way to organize it is to have a list, that list will be at the very top of a file, and after it will come a ridiculous amount of code to handle what that list controls and how it operates. I’m looking to write said list only once, and said list is a list of folder and file names in this format:

[(folder/filename, bool, bool, int), (folder/filename, bool, bool, int)]

As you can see, folder/filename are the same (sort of). File name is folder name with .py on the end, but doing import XXX you don’t need to do import XXX.py, so I don’t see this causing an issue.

The problem I’m facing is importing using this method…

for (testName, auto, hardware, bit) in testList:
    print(testName)
    paths = "\\" + testName
    print paths
    addpath(paths)
    sys.modules[testName] = testName # One of a few options I've seen suggested on the net
    print("Path Added")
    test = testName + ".Helloworld()"
    eval(test)

So for each test I have, print the name, assemble a string which contains the path ("\\testName"), for this example, print the test path, then add the path to the list (sys.path.append(path)), then print to confirm it happened, then assemble a string which will be executed by eval for the tests main module and eventually eval it.

As you can see, I’m currently having to have a list of imports at the top. I can’t simply do import testName (the contents of testName are the name of the module I wish to import), as it will try to find a module called testName, not a module called the contents of testName.

I’ve seen a few examples of where this has been done, but can’t find any which work in my circumstances. If someone could literally throw a chunk of code which does it that would be wonderful.

I’d also request that I’m not hung, drawn, nor quartered for use of eval, it is used in a very controlled environment (the list through which it cycles is within the .py file, so no “end user” should mess with it).

  • 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-06-16T11:35:58+00:00Added an answer on June 16, 2026 at 11:35 am

    Not sure if I understood everything correctly, but you can import a module dynamically using __import__:

    mod = __import__(testName)
    mod.HelloWorld()
    

    Edit: I wasn’t aware that the use of __import__ was discouraged by the python docs for user code: __import__ documentation (as noted by Bakuriu)

    This should also work and would be considered better style:

    import importlib
    
    mod = importlib.import_module(testName)
    mod.HelloWorld()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Dynamic module import in Python I have a list of modules, like
Possible Duplicate: Dynamic module import in Python Probably a simple question! I need to
Possible Duplicate: Dynamic module import in Python I need a functionality that allows users
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? I have
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? Is there
Possible Duplicate: What is the best way to solve an Objective-C namespace collision? I
Possible Duplicate: Python dynamic inheritance: How to choose base class upon instance creation? I
Possible Duplicate: How to make dynamic form action address in codeigniter? I have the
Possible Duplicate: Dynamic loading of python modules python: How to add property to a
Possible Duplicate: How can I make this query to accept dynamic table names? I

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.