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

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here's a quick overview: You need to create an input… May 16, 2026 at 5:10 pm
  • Editorial Team
    Editorial Team added an answer LINQPad defines two extension methods (in LINQPad.Extensions), namely Dump() and… May 16, 2026 at 5:10 pm
  • Editorial Team
    Editorial Team added an answer You are assigning fileElementId:'uploadFile', but your file field doesn't in… May 16, 2026 at 5:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

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/
The directory and file structure is as follows: C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded C:\xampp\htdocs\testA.php // as follows $userID
I have a canonical file structure like that (I'm giving sensible names to ease
I have a hello.clj as follows. (ns hello) (defn hi [] (println HI)) Normally,
I need advice if this setup is ok. I have a personal dev server
This is my first experience using the Zend Framework. I am attempting to follow
How does one read a data file in an iPhone project? For example, lets
Very likely I'm going about this in the wrong way entirely. I'm completely new
gcc 4.4.3 c89 linux I am using log4c and have placed the inc and

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.