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.
Answer from g.d.d.c on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
Absolute imports may use either the import <> or from <> import <> syntax, but relative imports may only use the second form; the reason for this is that: ... The __main__ module is a special case relative to Python’s import system.
🌐
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 Python documentation says the following about Python 3’s handling of relative imports: The only acceptable syntax for relative imports is from .[module] import name. All import forms not starting with .
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

🌐
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.
🌐
Career Karma
careerkarma.com › blog › python › python import statements: a guide
Python Import Statements: A Guide | Career Karma
December 1, 2023 - The Python import statement lets you import a module into your code. A module is a file that contains functions and values that you can reference from your program. The import statement syntax is: import modulename.
🌐
Real Python
realpython.com › lessons › import-statement
The import Statement (Video) – Real Python
In this video, you’ll take a deep dive into the import statement. So, what forms can the import statement take? In its simplest form, it’s like what you’ve already used in the previous video: import and then the module’s name. In that particular…
Published: January 30, 2020
🌐
Real Python
realpython.com › python-import
Python import: Advanced Techniques and Tips – Real Python
July 8, 2020 - However, let’s get started with the basics: importing modules and packages. ... An object that serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
Find elsewhere
🌐
AskPython
askpython.com › python › python-import-statement
Python import Statement - AskPython
August 6, 2022 - Python import statement enables the user to import particular modules in the corresponding program.
🌐
Zean
zean.be › articles › definitive-guide-python-imports
The definitive guide to Python import statements - Zean Qin
In Python 2, one could write from <module> import * within a function. In Python 3, the from <module> import * syntax is only allowed at the module level, no longer inside functions.
🌐
iO Flood
ioflood.com › blog › python-import
Python Import: Using The Module Import Statement
February 5, 2024 - Python’s import statement is a built-in function that allows you to include external modules in your program. It’s an essential tool for every Python developer, as it provides access to a wealth of functions, classes, and methods that can simplify your code and increase its functionality. The syntax of the import statement is straightforward.
🌐
W3Schools
w3schools.com › python › python_modules.asp
Python Modules
Note: When using a function from a module, use the syntax: module_name.function_name. The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc): ... There are several built-in modules in Python, which you can import whenever you like.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Import Modules, Packages, Functions in Python | note.nkmk.me
May 12, 2023 - The from <module_name> import <identifier_name> syntax allows you to import specific items.
🌐
Medium
medium.com › cold-brew-code › a-quick-guide-to-understanding-pythons-import-statement-505eea2d601f
A Quick Guide to Understanding Python’s Import Statement | by Richard Russell | Cold Brew Code | Medium
June 5, 2020 - To import the module, Python needs a fully qualified name of the module being imported. This may come from various arguments such as the import statement or from parameters to the importlib.import_module() or __import__() functions.
🌐
Readthedocs
ref.readthedocs.io › en › latest › understanding_python › imports
Python Imports — Reference Pages 0.2 documentation
The import statement has been a part of Python since the very beginning, though with more limited behavior. Like many things in Python, the syntax for the import statement has its roots in Modula-3.
🌐
Python
docs.python.org › 3.10 › reference › import.html
5. The import system — Python 3.10.22 documentation
Absolute imports may use either the import <> or from <> import <> syntax, but relative imports may only use the second form; the reason for this is that: ... The __main__ module is a special case relative to Python’s import system. As noted elsewhere, the __main__ module is directly initialized ...
🌐
PyTutorial
pytutorial.com › python-import-statements-guide
PyTutorial | Python Import Statements Guide
May 10, 2025 - Import statements are key in Python. They let you use code from other files. This guide covers syntax and best practices. Basic Import Syntax The simplest impor
🌐
Real Python
realpython.com › absolute-vs-relative-python-imports
Absolute vs Relative Imports in Python – Real Python
May 9, 2026 - When you use the second syntax, you import the resource from another package or module. Here’s an example: ... This renames the imported resource abc to other_name within the script. It must now be referenced as other_name, or it will not be recognized. PEP 8, the official style guide for Python, has a few pointers when it comes to writing import statements.
🌐
Python Morsels
pythonmorsels.com › 4-ways-import-module-python
4 ways to import a module in Python - Python Morsels
August 3, 2026 - Let's talk about the four ways to import something from a module in Python. The first way is to use the import statement, using the syntax import module_name:
🌐
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?
🌐
Snarky
snarky.ca › unravelling-the-import-statement
Unravelling the import statement
January 6, 2021 - As part of my series on Python's syntactic sugar [https://snarky.ca/tag/syntactic-sugar/], I am going to cover import statements [https://docs.python.org/3.8/reference/simple_stmts.html#the-import-statement]. This will include delving into the quirky interface of __import__() [https://docs.python.org/