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 8120481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:10:26+00:00 2026-06-06T05:10:26+00:00

I have a file structure like this: dir_a __init__.py mod_1.py mod_2.py dir_b __init__.py my_mod.py

  • 0

I have a file structure like this:

dir_a
    __init__.py
    mod_1.py
    mod_2.py
dir_b
    __init__.py
    my_mod.py

I want to dynamically import every module in dir_a in my_mod.py, and in order to do that, I have the following inside the __init__.py of dir_a:

import os
import glob
__all__ = [os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(os.path.abspath(__file__)) + "/*.py")]

But when I do:

from dir_a import *

I get ImportError: No module named dir_a

I wonder what causes this, is it because the parent directory containing dir_a and dir_b has to be in PYTHONPATH? In addition, the above approach is dynamic only in the sense that my_mod.py picks up everything when it is run, but if a new module is added to dir_a during its run this new module won’t be picked up. So is it possible to implement a truly dynamic importing mechanism?

  • 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-06T05:10:27+00:00Added an answer on June 6, 2026 at 5:10 am

    The answer to the first part of your question is a trivial yes: you cannot import dir_a when the directory containing dir_a isn’t in the search path.

    For the second part, I don’t believe that is possible in general – you could listen for ‘file created’ OS signals in dir_a, see whether the created file is a .py, and add it to dir_a.__all__ – but that is complicated, probably expensive, and doesn’t retroactively add the new thing to the global namespace, since from foo import * only sees what is in foo.__all__ at import time. And changing that would be error-prone – it allows your namespace to change at any time based on an unpredictable external event. Say you do this:

    from dir_a import *
    bar = 5
    

    And then a bar.py gets added to dir_a. You can’t write a “truly dynamic importer” without considering that situation, and deciding what you want to have happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file structure like this: root |_ fruits |___ apple |______images |________
I have a file with structure like this: [05:58:10 08.12.1990] 125.441 [05:58:21 08.12.1990] -2.4158
I have a file structure that looks like this ./501.res/1.bin ./503.res/1.bin ./503.res/2.bin ./504.res/1.bin and
I have a file system structure with symlinks, something like this: folder123 index.php config.php
I have this file structure: folderIWantStuffIn/ - old_stuff Now I want to add some
I have an xml file nodes structure like this <Employee> <EmpId></EmpId> <EmpName></EmpName> <Salary> <Basic></Basic>
I have a file structure like this: 00000010 000000001.file1 000000001.file2 00000020 00000003.file1 00000003.file2 00000003.file3
I have a file structure like this: file1.h extern const char *build_info[][3]; file1.cpp #include
So just a general question: if i have a file structure like this where
I have a file with the structure like so: http://gamedev.pastebin.com/8iESYTVY but it's much bigger

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.