Will it load the entire math module when a reference to any function in that module is made in my program, or will it expand the module only once in the program?

The math module will be loaded into memory once per Python program (or interpreter).

If it expands only once, I assume, the computer will load entire python file and all the modules it imports completely in memory

Yes, in normal circumstances.

Won't that cause a memory running out of space issue if I import too many native python code from the library?

No, not typically. Python modules would not put a dent in the memory of modern computers.

Is that the reason some people say it is always good to import exact function in your program instead of wild cards?

No, the entire module will be loaded regardless if you use just one function in it. This is because that one function can rely on any other code in the module.

The reason it is advised to import specific functions is more of a best practice or recommendation to be explicit about what you are using from the module.

Also, the module may contain function names in it that are the same as ones you define yourself or are even in another imported module so you want to be careful not to import a bunch of names, especially if you are not going to use them.

Answer from nlloyd on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the ...
🌐
W3Schools
w3schools.com › python › python_modules.asp
Python Modules
There is a built-in function to list all the function names (or variable names) in a module. The dir() function: List all the defined names belonging to the platform module: import platform x = dir(platform) print(x) Try it Yourself »
🌐
Python
docs.python.org › 3 › library › importlib.html
importlib — The implementation of import
One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter.
🌐
GeeksforGeeks
geeksforgeeks.org › python › import-module-python
Import module in Python - GeeksforGeeks
April 25, 2026 - Python modules are of two types: built-in (come with Python) module and external modules like pandas and numpy, which need to be installed separately. Built-in modules can be directly imported using "import" keyword without any installtion.
🌐
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 - All of the objects defined in the module or the package’s __init__.py file are made available to the importer. According to Python documentation, here is how an import statement searches for the correct module or package to import:
🌐
Real Python
realpython.com › python-import
Python import: Advanced Techniques and Tips – Real Python
July 8, 2020 - In practice, a module usually corresponds to one .py file containing Python code. The true power of modules is that they can be imported and reused in other code.
🌐
YouTube
youtube.com › watch
Python Tutorial for Beginners 9: Import Modules and Exploring The Standard Library - YouTube
In this Python Beginner Tutorial, we will begin learning how to import modules in Python. We will learn how to import modules we have written, as well as mod...
Published: May 17, 2017
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.
Top answer
1 of 2
1

Will it load the entire math module when a reference to any function in that module is made in my program, or will it expand the module only once in the program?

The math module will be loaded into memory once per Python program (or interpreter).

If it expands only once, I assume, the computer will load entire python file and all the modules it imports completely in memory

Yes, in normal circumstances.

Won't that cause a memory running out of space issue if I import too many native python code from the library?

No, not typically. Python modules would not put a dent in the memory of modern computers.

Is that the reason some people say it is always good to import exact function in your program instead of wild cards?

No, the entire module will be loaded regardless if you use just one function in it. This is because that one function can rely on any other code in the module.

The reason it is advised to import specific functions is more of a best practice or recommendation to be explicit about what you are using from the module.

Also, the module may contain function names in it that are the same as ones you define yourself or are even in another imported module so you want to be careful not to import a bunch of names, especially if you are not going to use them.

2 of 2
0

Importing a python module means to load the namespace of what is available in that python module, into memory. Specifically, writing "import " tells python to look for a module with that name on your python path (typically a folder), and if it finds such an object, to then run that objects __init__.py file. This file is typically blank, meaning that python should simply load what is available in the module by reading through the files, but this file can also be customized.

Python automatically tracks what is loaded and doesn't re-load already loaded items. Hence, writing

import time
import itertools
import itertools as it

doesn't reload time if itertools also uses the time module and doesn't reload itertools if you rename it to it. In general, this is done very quickly; in fact, if the code is compiled (some modules are installed already compiled) it can be as fast as literally copying the bytes into memory (down to ns of time regardless of module size). Importing is one of the fastest commands you can do in python and is rarely the source of any speed issues. Copying and loading bytes into ram can be done millions of times a second and costs the computer nothing. It is when the computer has to perform calculations and compute that there is a concern.

Something like:

from itertools import combinations

does not load itertools any faster or slower than if you simply loaded the whole module (there are exceptions to this where some modules are so big that they are broken into sub-packages and so loading at the highest level doesn't load anything at all, for example, scipy, you have to specify the sub-package you want to load).

The reason it is recommended to not run from itertools import * is because this loads your namespace with dozens of functions which no one can track. Suddenly, you use the function combinations and no one that reads your code has any idea which module it came from. Hence, it is considered bad practice to * import. Ironically, some programming languages like C do nothing by * importing and it really is impossible to track where variables have come from.

Top answer
1 of 6
340

It depends on how you want to access the import when you refer to it.

from urllib import request
# access request directly.
mine = request()

import urllib.request
# used as urllib.request
mine = urllib.request()

You can also alias things yourself when you import for simplicity or to avoid masking built ins:

from os import open as open_
# lets you use os.open without destroying the 
# built in open() which returns file handles.
2 of 6
275

Many people have already explained about import vs from, so I want to try to explain a bit more under the hood, where the actual difference lies.

First of all, let me explain exactly what the basic import statements do.

import X

Imports the module X, and creates a reference to that module in the current namespace. Then you need to define completed module path to access a particular attribute or method from inside the module (e.g.: X.name or X.attribute)

from X import *

Imports the module X, and creates references to all public objects defined by that module in the current namespace (that is, everything that doesn’t have a name starting with _) or whatever name you mentioned.

Or, in other words, after you've run this statement, you can simply use a plain (unqualified) name to refer to things defined in module X. But X itself is not defined, so X.name doesn't work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.

This makes all names from the module available in the local namespace.

Now let's see what happens when we do import X.Y:

>>> import sys
>>> import os.path

Check sys.modules with name os and os.path:

>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>

Check globals() and locals() namespace dict with name os and os.path:

 >>> globals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> locals()['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> globals()['os.path']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'os.path'
>>>    

From the above example, we found that only os is added to the local and global namespaces. So, we should be able to use os:

 >>> os
 <module 'os' from     
  '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
 >>> os.path
 <module 'posixpath' from      
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
 >>>

…but not path:

>>> path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined 
>>>

Once you delete the os from locals() namespace, you won't be able to access either os or os.path, even though they do exist in sys.modules:

>>> del locals()['os']
>>> os
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>> os.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>

Now let's look at from.

from

>>> import sys
>>> from os import path

Check sys.modules with name os and os.path:

>>> sys.modules['os']
<module 'os' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc'>
>>> sys.modules['os.path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>

So sys.modules looks the same as it did when we imported using import name.

Okay. Let's check what it the locals() and globals() namespace dicts look like:

>>> globals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> locals()['path']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['os']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'os'
>>>

You can access by using path, but not by os.path:

>>> path
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> os.path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
 

Let's delete 'path' from locals():

>>> del locals()['path']
>>> path
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'path' is not defined
>>>

One final example using aliasing:

>>> from os import path as HELL_BOY
>>> locals()['HELL_BOY']
<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>> globals()['HELL_BOY']
<module 'posixpath' from /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.pyc'>
>>>

And no path defined:

>>> globals()['path']
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
KeyError: 'path'
>>>

One pitfall about using from

When you import the same name from two different modules:

>>> import sys
>>> from os import stat
>>> locals()['stat']
<built-in function stat>
>>>
>>> stat
<built-in function stat>

Import stat from shutil again:

>>>
>>> from shutil import stat
>>> locals()['stat']
<module 'stat' from 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>> stat
<module 'stat' from 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/stat.pyc'>
>>>

THE LAST IMPORT WILL WIN

🌐
OpenClassrooms
openclassrooms.com › us › courses › 6900846-set-up-a-python-environment › 6989471-import-python-packages-and-modules
Import Python Packages and Modules - OpenClassrooms
As an alternative to this, we can instead choose to import selected functionality from the os package as follows: ... We import third-party Python packages into Python files in the same way as we import modules from the Python Standard Library into Python files.
🌐
DataCamp
datacamp.com › tutorial › modules-in-python
Python Modules Tutorial: Importing, Writing, and Using Them | DataCamp
June 1, 2026 - Instead of copying and pasting the same function across multiple scripts, you define it once in a module and import it later when you need it. This is exactly how Python's own standard library works, which are collections of pre-built modules like math, os, and random that you can pull into any program with a single line.
🌐
Mend
mend.io › blog › developer › python import: mastering the advanced features
Python Import: Mastering the Advanced Features
November 8, 2023 - In the realm of Python programming, ... and effort. At its core, importing allows you to access and utilize functions, classes, and variables defined in external files, known as modules....
🌐
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 - Similarly, when you import modules, you’re selecting specific functions or features to use in your code, making your program more efficient and organized. A Python package is a directory that contains Python modules and an optional special file called __init__.py.
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › the shortest python `import` tutorial • a picture story
The Shortest Python `import` Tutorial • A Picture Story
January 22, 2025 - The difference between the three is best understood through Python's namespaces. When you import the whole module, such as import some_module, the name some_module is part of the program's main namespace. However, the module's contents aren't. They're in the module's namespace.
🌐
Reddit
reddit.com › r/learnprogramming › what does a python import statement actually do?
r/learnprogramming on Reddit: What does a Python import statement actually do?
August 1, 2022 -

I mean, I know it lets you use functions from that library, but how does it do this?

Like, in C/C++, I know a #include statement essentially tells the preprocessor to paste the contents of the .h file into the file with the #include statement, and then the linker matches the function declarations to their definitions in the relevant .c files.

Does something similar happen with import statements in Python? Or is it a totally different process, since Python is normally interpreted, not compiled? I would think it's at least sort of similar, since a lot of Python libraries are written in C to begin with.

Top answer
1 of 3
3
It does work quite differently, though there are some similarities. Here, basically is how import works in the simple case (importing a pure python module that's not in a package). Python calls the special function __import__ This function first looks for the module in sys.modules which is basically a dictionary of module names to module objects. If it finds a module object proceed to step 8. If the module isn't in the cache, it uses the module resolution rules to find the file to import. if the file isn't found, error. If a .pyc bytecode file reqd and execute it If not open the file. Translate it to byte code, write this bytecode to a .pyc file and execute the byte code. Bind any top level variables, including functions and classes, to the module object. You can basically think of the module object as a big dictionary of variable name to value. If you did import foo, assign the module object to the variable foo (or whatever the module name is) If you did from foo import bar assign the value associated with the key bar to the variable bar. Do similarly if you use as to rename the import Put the module object in sys.modules if it isn't already there. A few key things to note: A module is automatically only imported once per program. Essentially 'include guards' are automatic. A module is run. That means top level statements in modules meant to be imported are dangerous. All modules have their own namespaces. There is no such thing as a Python variable outside of a namespace. Even 'global' variables are not truly global. All python files are modules. Unlike some languages, there is no distinction between a module and a script.
2 of 3
1
This is going to sound facetious, but it has to do with the answer to your question. Do you understand how python executes your file?
🌐
W3Schools
w3schools.com › python › gloss_python_module_import_from.asp
Python Import From Module
Python Examples Python Compiler ... Q&A Python Training ... You can choose to import only parts from a module, by using the from keyword....
🌐
Python
docs.python.org › 3.10 › reference › import.html
5. The import system — Python 3.10.22 documentation
Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. 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.
🌐
Python for the Lab
pythonforthelab.com › blog › complete-guide-to-imports-in-python-absolute-relative-and-more
Complete Guide to Imports in Python: Absolute, Relative, and More | Python For The Lab
If we have a file called module.py, we can simply use the following syntax to import it: ... In this case, sys is a module that belongs to the Python Standard Library. It provides functions to interact with the interpreter itself. For example, we can use it to find out if any arguments where passed while executing a script.