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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:40:01+00:00 2026-05-16T00:40:01+00:00

I have a directory structure as follows: | main.py | scripts |–| __init__.py |

  • 0

I have a directory structure as follows:

| main.py
| scripts
|--| __init__.py
   | script1.py
   | script2.py
   | script3.py

In main.py, if I import scripts, this apparently does not allow me to use scripts.script1. I know I can use from scripts import * to access the modules in the scripts package, but then I can only use them directly as scripts1, scripts2 etc.

How can I write the code so that I can refer to scripts.script1 inside main.py?

I tried using pkgutils.walk_packages, as well as the __all__ attribute of the package, to get the submodule names, but I couldn’t figure out a way to use those strings to do the import.

  • 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-16T00:40:02+00:00Added an answer on May 16, 2026 at 12:40 am

    Edit: Here’s one way to recursively import everything at runtime…

    (Contents of __init__.py in top package directory)

    import pkgutil
    
    __all__ = []
    for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
        __all__.append(module_name)
        _module = loader.find_module(module_name).load_module(module_name)
        globals()[module_name] = _module
    

    I’m not using __import__(__path__+'.'+module_name) here, as it’s difficult to properly recursively import packages using it. If you don’t have nested sub-packages, and wanted to avoid using globals()[module_name], though, it’s one way to do it.

    There’s probably a better way, but this is the best I can do, anyway.

    Original Answer (For context, ignore othwerwise. I misunderstood the question initially):

    What does your scripts/__init__.py look like? It should be something like:

    import script1
    import script2
    import script3
    __all__ = ['script1', 'script2', 'script3']
    

    You could even do without defining __all__, but things (pydoc, if nothing else) will work more cleanly if you define it, even if it’s just a list of what you imported.

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

Sidebar

Related Questions

I have the following directory structure: Main.py A/ __init__.py B/ __init__.py C/ __init__.py The
I have a directory structure as follows Client_Site/ some_folder1/ some_folder2/ some_folder3/ Lots of files
I have a directory structure that looks like this: /a/f.xml /b/f.xml /c/f.xml /d/f.xml What
When creating gems, I often have a directory structure like this: |--lib |-- helpers.rb
I have a directory structure as follows dir----| | |--dir1\ some\ thing--result.pdf | |--dir2\
I have a directory structure as follows: Projects -> Libraries -> Box2d -> Collision->
I have a simple python application where my directory structure is as follows: project/
I am really confused i have a directory structure as follows. /foo/bar/project/ under project
I have an App Engine project structure setup as follows: ProjectRoot app.yaml index.yaml main.py
I have directory structure like so: |-- ROOT |-- Project1 | |-- application |

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.