When you import FooPackage, Python searches the directories on PYTHONPATH until it finds a file called FooPackage.py or a directory called FooPackage containing a file called __init__.py. However, having found the package directory, it does not then scan that directory and automatically import all .py files.

There are two reasons for this behaviour. The first is that importing a module executes Python code which may take time, memory, or have side effects. So you might want to import a.b.c.d without necessarily importing all of a huge package a. It's up to the package designer to decide whether a's __init__.py explicitly imports its modules and subpackages so that they are always available, or whether or leaves the client program the ability to pick and choose what is loaded.

The second is a bit more subtle, and also a showstopper. Without an explicit import statement (either in FooPackage/__init__.py or in the client program), Python doesn't necessarily know what name it should import foo.py as. On a case insensitive file system (such as used in Windows), this could represent a module named foo, Foo, FOO, fOo, foO, FoO, FOo, or fOO. All of these are valid, distinct Python identifiers, so Python just doesn't have enough information from the file alone to know what you mean. Therefore, in order to behave consistently on all systems, it requires an explicit import statement somewhere to clarify the name, even on file systems where full case information is available.

Answer from Ben on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The __init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported. For example, the following file system layout defines a top level parent package with three subpackages:
Top answer
1 of 6
78

When you import FooPackage, Python searches the directories on PYTHONPATH until it finds a file called FooPackage.py or a directory called FooPackage containing a file called __init__.py. However, having found the package directory, it does not then scan that directory and automatically import all .py files.

There are two reasons for this behaviour. The first is that importing a module executes Python code which may take time, memory, or have side effects. So you might want to import a.b.c.d without necessarily importing all of a huge package a. It's up to the package designer to decide whether a's __init__.py explicitly imports its modules and subpackages so that they are always available, or whether or leaves the client program the ability to pick and choose what is loaded.

The second is a bit more subtle, and also a showstopper. Without an explicit import statement (either in FooPackage/__init__.py or in the client program), Python doesn't necessarily know what name it should import foo.py as. On a case insensitive file system (such as used in Windows), this could represent a module named foo, Foo, FOO, fOo, foO, FoO, FOo, or fOO. All of these are valid, distinct Python identifiers, so Python just doesn't have enough information from the file alone to know what you mean. Therefore, in order to behave consistently on all systems, it requires an explicit import statement somewhere to clarify the name, even on file systems where full case information is available.

2 of 6
44

You need to import the submodule:

import FooPackage.foo

What you're doing is looking for foo in FooPackage/__init__.py. You could solve it by putting import FooPackage.foo as foo (or from . import foo) in FooPackage/__init__.py, then Python will be able to find foo there. But I recommend using my first suggestion.

Discussions

What is the best way to import a module from a path?
It's probably the responsibility of the script that boots your python script to set PYTHONPATH. From the perspective of python, make sure the environment is set up correctly before the interpreter boots up. In other words, don't do this! More on reddit.com
🌐 r/learnpython
28
66
November 14, 2019
Why does Python run an entire module when you only import one function from it?
When you import a module, all code that is in the module is ran, this may be needed as a setup for some module. If you dant want to run some code when you import the file, use this: if __name__ == "__main__": bar() This will make sure that bar() function is only ran when you run hello.py directly, but not when you import it. More on reddit.com
🌐 r/learnpython
29
47
June 4, 2023
i have a problem with importing packages in python
I think it would be a lot more easy if you would copy the code including the module name. Have you installed the module with pip or conda? Are you in the right env? More on reddit.com
🌐 r/vscode
14
0
August 3, 2023
Python package and imports
That's completely normal. You can import any name from a module, even names that the module imported. It's unusual to do but it does happen sometimes, most often in __init__.py files. More on reddit.com
🌐 r/learnpython
5
0
October 30, 2021
🌐
OpenClassrooms
openclassrooms.com › us › courses › 6900846-set-up-a-python-environment › 6989471-import-python-packages-and-modules
Import Python Packages and Modules - OpenClassrooms
For example, if you want to use the os module from the Python Standard Library to print the current working directory of a process, you could create a file called script.py with the following contents: ... In the script we import the entire os module and make use of a piece of its functionality by calling os.getcwd() . As an alternative to this, we can instead choose to import selected functionality from the os package ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-packages
Python Packages - GeeksforGeeks
In this example, we create a package called math_operations to organize mathematical functions. The package contains two sub-packages: ... Each operation is stored in its own module, which makes the code modular, reusable and easier to maintain.
Published: April 6, 2026
🌐
Programiz
programiz.com › python-programming › package
Python packages (With Examples)
In Python, we can import modules from packages using the dot (.) operator. For example, if we want to import the start module in the above example, it can be done as follows:
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Import Modules, Packages, Functions in Python | note.nkmk.me
May 12, 2023 - 5. The import system - Packages — Python 3.11.3 documentation · Although not strictly defined, packages and modules are sometimes called libraries. For example, import the math module.
🌐
Python Packaging
packaging.python.org › en › latest › discussions › distribution-package-vs-import-package
Distribution package vs. import package - Python Packaging User Guide
Most of the time, a distribution package provides one single import package (or non-package module), with a matching name. For example, pip install numpy lets you import numpy.
Find elsewhere
🌐
Python
docs.python.org › 3 › library › importlib.html
importlib — The implementation of import
For example, consider the case of an import from foo.bar import _whiz, where _whiz is implemented with the binary module sources/foo/bar/_whiz.abi3.so, with sources being the location registered on sys.path, relative to the application bundle.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python packages
Python Packages
March 30, 2025 - from <package> import * Code language: Python (python) Python will look for the __init__.py file. If the __init__.py file exists, it’ll load all modules specified in a special list called __all__ in the file.
🌐
Real Python
realpython.com › python-modules-packages
Python Modules and Packages – An Introduction – Real Python
1 day ago - Instead, Python follows this convention: if the __init__.py file in the package directory contains a list named __all__, it is taken to be a list of modules that should be imported when the statement from <package_name> import * is encountered.
🌐
Chris Yeh
chrisyeh96.github.io › 2017 › 08 › 08 › definitive-guide-python-imports.html
The Definitive Guide to Python import Statements | Chris Yeh
August 8, 2017 - For example, packB is a namespace package because it doesn’t have a __init__.py file in the folder. If we start a Python 3.6 interactive interpreter in the test/ directory, then we get the following output: >>> import packB >>> packB <module 'packB' (namespace)>
🌐
Learn Python
learnpython.org › en › Modules_and_Packages
Modules and Packages - Learn Python - Free Interactive Python Tutorial
That way it can be imported the same way as a module. If we create a directory called foo, which marks the package name, we can then create a module inside that package called bar. Then we add the __init__.py file inside the foo directory.
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module5_OddsAndEnds › Modules_and_Packages.html
Import: Modules and Packages — Python Like You Mean It
Our tour through the essentials of Python and NumPy required us to regularly make use of import statements. This allowed us to access the functions and objects that are provided by the standard library and by NumPy. # accessing `defaultdict` from the standard library's `collections` package from collections import defaultdict # import the entire numpy package, giving it the alias 'np' import numpy as np
🌐
YouTube
youtube.com › watch
Python Packages and Imports | Python Tutorial for Beginners - YouTube
Hey everyone!In this video, we go over importing code from other files and creating a python package.GITHUB: https://github.com/IVIURRAY/PythonTutorialSeries...
Published: June 26, 2023
🌐
Real Python
realpython.com › python-import
Python import: Advanced Techniques and Tips – Real Python
July 8, 2020 - Note: Directories without an __init__.py file are still treated as packages by Python. However, these won’t be regular packages, but something called namespace packages. You’ll learn more about them later. In general, submodules and subpackages aren’t imported when you import a package. However, you can use __init__.py to include any or all submodules and subpackages if you want. To show a few examples of this behavior, you’ll create a package for saying Hello world in a few different languages.
🌐
Python documentation
docs.python.org › 3 › tutorial › modules.html
6. Modules — Python 3.15.1 documentation
This would mean that from sound.effects import * would import the three named submodules of the sound.effects package. Be aware that submodules might become shadowed by locally defined names. For example, if you added a reverse function to the sound/effects/__init__.py file, the from sound.effects import * would only import the two submodules echo and surround, but not the reverse submodule, because it is shadowed by the locally defined reverse function:
🌐
Medium
medium.com › @officialyrohanrokade › mastering-python-imports-and-module-management-a-deep-dive-into-import-keywords-folder-d92aa1daaaf5
Mastering Python Imports and Module Management: A Deep Dive into Import Keywords, Folder Structures, and Module Control. | by Rohan Rokade | Medium
February 10, 2024 - Just like every house has a front door, every Python package has an “init.py” file. It’s the gateway to a world of functionality, enabling seamless integration of powerful tools into your projects. ... import my_package my_package.greet().
🌐
Mend
mend.io › blog › developer › python import: mastering the advanced features
Python Import: Mastering the Advanced Features
November 8, 2023 - # Example of importing a module from the standard library import math # Using a function from the math module result = math.sqrt(25) print(result) # Output: 5.0 · As your Python projects grow in complexity, you’ll often find yourself working with more than just individual modules. Enter packages.
🌐
AskPython
askpython.com › python › python-packages
Python Packages - Creating and Accessing - AskPython
September 5, 2019 - Python packages are used to organize modules into a directory. How to create and access a package modules, Python package __init__.py, import * from a package.
🌐
Medium
medium.com › @jameskabbes › importing-your-local-package-with-pythonpath-c459dc209c7
Importing your Local Package with PYTHONPATH | by James Kabbes | Medium
September 21, 2023 - >>> import sys >>> sys.path.append( '/path/to/parent_of_test_package' ) >>> import test_package >>> Let’s add to our package before moving along. This is a special file for Python packages.