🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
When a submodule is loaded using ... module’s namespace to the submodule object. For example, if package spam has a submodule foo, after importing spam.foo, spam will have an attribute foo which is bound to the ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › import-module-python
Import module in Python - GeeksforGeeks
April 25, 2026 - from math import * print(pi) # Accessing the constant 'pi' print(factorial(6)) # Using the factorial function ... While convenient, this method is not recommended in larger programs as it can lead to conflicts with existing variables and functions.
Discussions

python - from ... import OR import ... as for modules - Stack Overflow
* In Python 3.6 and before, there was a bug with the initialization ordering of packages containing other modules, where in the loading stage of the package using import contained.module.something as alias in a submodule would fail where from contained.module import something as alias would not. See Imports in __init__.py and 'import as' statement for a very illustrative example ... More on stackoverflow.com
🌐 stackoverflow.com
Can somebody ELI5 importing modules?

a python module is

  1. a python file

  2. a directory full of python files (optionally with a __init__.py in it)

when you do import foo it thus searches for foo.py or a directory called foo in

  1. the directory containing the script with that import statement.

  2. everything in the environment variable $PYTHONPATH

  3. a bunch of other directories like /usr/lib/python3/dist-packages

you can see where it searches by looking at sys.path.

if you want to use imports properly, use one directory for your modules and within modules, use explicitly relative imports.

like when you write a module called textutils with a submodule replacers, you have textutils/replacers.py. and when importing replacers from anywhere directly inside textutils (e.g. in textutils/__init__.py), you do from . import replacers

and when you import it from anywhere else (outside of textutils), do import textutils.replacers.

when you added the parent directory of textutils to $PYTHONPATH, this works. just put all your modules in there and it works.

(e.g. put export PYTHONPATH="$HOME/python_modules" in ~/.bashrc, and put all modules into ~/python_modules)

More on reddit.com
🌐 r/learnpython
11
21
March 2, 2014
The Definitive Guide to Python import Statements
Also, Python imports are case-sensitive. IIRC this only holds true on case sensitive filesystems. Which is the kind you'd most encounter. But still. More on reddit.com
🌐 r/programming
13
35
August 21, 2018
A Guide on How Importing Works in Python.
For anyone with three hours to kill, David Beazley is my favorite Python instructor and he covered this topic in the video below: Modules and Packages: Live and Let Die Part 1 Background- 6:30 Part 2 Packages - 14:00 Discussion of __init__.py - 24:00 Case where they use 'import *' in the init.py file in asyncio in stdlib - 27:50 Every package has its own PATH - 37:50 Part 3 All about __main__ - 41:30 Using python to make zip files from the command line - 48:30 Part 4 sys.path - 51:45 How virtualenv works - 1:00:00 The part on importing is all between 6:30 and 14:00 and the rest is all about packaging and how the whole directory structure functions. On this rewatch, I only went through the first hour, so there's more in there, who knows what. More on reddit.com
🌐 r/learnpython
11
135
August 2, 2017
🌐
Real Python
realpython.com › python-import
Python import: Advanced Techniques and Tips – Real Python
July 8, 2020 - Similarly, Python’s global namespace is also a dictionary. You can access it through globals(). It’s fairly common to import subpackages and submodules in an __init__.py file to make them more readily available to your users. You can see one example of this in the popular requests package. ... You’ve already seen from...import statements such as from math import pi, but what does the dot (.) in from .
🌐
OpenClassrooms
openclassrooms.com › us › courses › 6900846-set-up-a-python-environment › 6989471-import-python-packages-and-modules
Import Python Packages and Modules - OpenClassrooms
An alternative to using the import <package> syntax is to instead import everything from a package using from <package> import * . This can be useful when you are using the Python shell, because it means you can type less.
🌐
W3Schools
w3schools.com › python › gloss_python_module_import_from.asp
Python Import From Module
Note: When importing using the from keyword, do not use the module name when referring to elements in the module. Example: person1["age"], not ... Python Modules Tutorial Create a Module Variables in Modules Renaming a Module Built-in Modules Using the dir() Function
🌐
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.
🌐
iO Flood
ioflood.com › blog › python-import
Python Import: Using The Module Import Statement
February 5, 2024 - As you grow more comfortable with Python’s import statement, you’ll find that there are several advanced techniques that can provide you with more control and flexibility. Instead of importing an entire module, you can choose to import only specific functions or classes. This can help prevent namespace conflicts and reduce memory usage. Here’s how you can do it: from math import pi print(pi) # Output: # 3.141592653589793 · In this example, we’re importing only the pi constant from the math module.
🌐
CloudBoost
blog.cloudboost.io › python-import-from-as-cheatsheet-tutorial-example-beginner-help-module-function-def-5f03b02a1dbe
[Python] import, from, as, cheatsheet | by Peter Chang | CloudBoost
June 15, 2020 - [Python] import, from, as, cheatsheet A module is a Python file that (generally) has only definitions of variables, functions, and classes. Example, we have a lib called module.py # module.py def …
Find elsewhere
🌐
Career Karma
careerkarma.com › blog › python › python import statements: a guide
Python Import Statements: A Guide | Career Karma
December 1, 2023 - The import statement syntax is: import modulename. Python is accompanied by a number of built-in modules that allow you to perform common operations in your code. int(), for example, converts a value to an integer.
🌐
W3Schools
w3schools.com › python › python_modules.asp
Python Modules
from mymodule import person1 print (person1["age"]) Run Example »
🌐
Mend
mend.io › blog › developer › python import: mastering the advanced features
Python Import: Mastering the Advanced Features
November 8, 2023 - Python’s standard library is a treasure trove of modules that cover a wide range of functionalities, from handling data structures to working with dates and times. Understanding how to import and utilize these modules effectively is a crucial skill for any Python developer. # 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
🌐
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 - The first time that a package or one of its modules is imported, Python will execute the __init__.py file in the root folder of the package if the file exists. All objects and functions defined in __init__.py are considered part of the package namespace. Consider the following example. ... ## this import makes a1_func directly accessible from packA.a1_func from packA.a1 import a1_func def packA_func(): print("running packA_func()")
🌐
Programiz
programiz.com › python-programming › modules
Python Modules (With Examples)
August 21, 2024 - For example, # import only pi from math module from math import pi print(pi) # Output: 3.141592653589793 · Here, we imported only the pi attribute from the math module. In Python, we can import all names(definitions) from a module using the following construct:
Top answer
1 of 6
61

Assuming that bar is a module or package in foo, there is no difference*, it doesn't matter. The two statements have exactly the same result:

>>> import os.path as path
>>> path
<module 'posixpath' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/posixpath.pyc'>
>>> from os import path
>>> path
<module 'posixpath' from '/Users/mj/Development/venvs/stackoverflow-2.7/lib/python2.7/posixpath.pyc'>

If bar is not a module or package, the second form will not work; a traceback is thrown instead:

>>> import os.walk as walk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named walk

Note that if bar is both a module inside foo and an object in the foo package namespace (so imported into the __init__.py module or defined there), then from foo import bar will import the object, and import foo.bar as bar imports the module.


* In Python 3.6 and before, there was a bug with the initialization ordering of packages containing other modules, where in the loading stage of the package using import contained.module.something as alias in a submodule would fail where from contained.module import something as alias would not. See Imports in __init__.py and 'import as' statement for a very illustrative example of that problem, as well as Python issues #23203 and #30024.

2 of 6
34

This is a late answer, arising from what is the difference between 'import a.b as b' and 'from a import b' in python

This question has been flagged as a duplicate, but there is an important difference between the two mechanisms that has not been addressed by others.

from foo import bar imports any object called bar from namespace foo into the current namespace.

import foo.bar as bar imports an importable object (package/module/namespace) called foo.bar and gives it the alias bar.

What's the difference?

Take a directory (package) called foo which has an __init__.py containing:

# foo.__init__.py
class myclass:
    def __init__(self, var):
        self.__var = var

    def __str__(self):
        return str(self.__var)

bar = myclass(42)

Meanwhile, there is also a module in foo called bar.py.

from foo import bar
print(bar)

Gives:

42

Whereas:

import foo.bar as bar
print(bar)

Gives:

<module 'foo.bar' from '/Users/..../foo/bar.py'>

So, import foo.bar as bar can be used if you want to be sure to import a foo/bar.py submodule even in the presence of a bar attribute in foo's namespace.

🌐
DataCamp
datacamp.com › tutorial › modules-in-python
Python Modules Tutorial: Importing, Writing, and Using Them | DataCamp
June 1, 2026 - This means the print statement above won't fire unexpectedly when someone imports calculation into their own program. ... The from..import statement allows you to import specific functions/variables from a module instead of importing everything. In the previous example, when you imported ...
🌐
Guru99
guru99.com › home › python › import module in python with examples
Import module in Python with Examples
3 weeks ago - When you want only specific things to be imported, you can make use of the “from” keyword to import what you want. ... defdisplay_message(): return "Welcome to Guru99 Tutorials!" def display_message1(): return "All about Python!"
🌐
Python Morsels
pythonmorsels.com › 4-ways-import-module-python
4 ways to import a module in Python - Python Morsels
August 3, 2026 - The first way is to use the import statement, using the syntax import module_name: ... We've just imported the math module. We can use the things within this module by looking up attributes on the math module object.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Import Modules, Packages, Functions in Python | note.nkmk.me
May 12, 2023 - AttributeError: module 'xxx' has no attribute 'yyy' This might happen when the correct file is not imported. For example, when you use import <module_name>, the Python interpreter first looks for a module with that name in the same directory ...
🌐
Medium
medium.com › pythoneers › understanding-python-imports-types-usage-and-examples-e6460bd947fa
Understanding Python Imports: Types, Usage, and Examples | by Roopika J | The Pythoneers | Medium
January 1, 2025 - Here’s a simple guide to the types of imports in Python, complete with examples and tips on when to use each. This imports an entire module, allowing access to its functions, classes, and variables through dot notation. ... ModuleNotFoundError: Happens if the module isn’t installed or is unavailable in the Python environment. Fix: Ensure the module is installed using pip install module_name. This imports only specific functions, classes, or variables from a module.