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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:10:55+00:00 2026-05-29T09:10:55+00:00

I have following files and directories in my project root directory main.py bar.py foo

  • 0

I have following files and directories in my project root directory

main.py
bar.py
foo \
    __init__.py
    alice.py
    bob.py

the files in directory foo are all empty files, and the content of bar.py is

alice = None
bob = None

and main.py is

import foo
import bar
print 'foo:', dir(foo)
print 'bar:', dir(bar)

When execute python main.py the output is

foo: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
bar: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'alice', 'bob']

Why there is no alice or bob in foo? And, what should I do other than

from foo import alice, bob

if I want to use alice and bob of the module foo, since there might be a lot of files in that folder?

EDIT

My question is not about the built-in function dir giving weird result. If I do this in main.py

import foo
foo.alice

An exception will occur: AttributeError: ‘module’ object has no attribute ‘alice’

There seems no alice in foo? I think I have some problem understanding how to import a directory as a module.

  • 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-29T09:10:55+00:00Added an answer on May 29, 2026 at 9:10 am

    It basically comes down to the difference between a module and a package. From the docs:

    Packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A. Just like the use of modules saves the authors of different modules from having to worry about each other’s global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other’s module names.

    The purpose of the __init__.py file within a directory is to make it a package. This file provides a place specify the public facing interface for the package. There’s two ways you can get alice and bob into foo:

    1. Use __all__

    In your __init__.py file, you can explicitly declare what modules you want to expose with __all__. The following will expose alice and bob.

    `__all__` = ['alice', bob']
    

    2. import alice and bob directly

    Alternatively, importing the modules within the __init__.py file will also expose these modules.

    It will also import these modules at initialization time, so whenever you import anything in foo they will also get imported.

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

Sidebar

Related Questions

I have a project in which i have the following files main.wxs - directory
I have following script that executes all the .reg files in the current directory.
I have the following three files in the same directory: citysim.cpp #include utils.h using
I have a directory named 'backup' its contains following files (with some specific details)
If, for example, I have a directory which contains the following files: Test-20120626-1023.txt Test-20120626-0710.txt
I have a netbeans project setup with the following absolute paths pointing to files
If I have the following files, I get this error (c2593 in VC9). If
I have the following js files in my code for EditInPlace... <?php echo $javascript->link('jquery.js');?>
I have the two following text files: First one: chr10 1000 1001 DEL 2.4807
For completeness I have downloadded the following archive files from the JOGL website; jogamp-linux-amd64.7z

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.