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
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

🌐
W3Schools
w3schools.com › python › ref_keyword_from.asp
Python from Keyword
Python Examples Python Compiler ... Q&A Python Bootcamp Python Training ... The from keyword is used to import only a specified section from a module....
Discussions

Difference between "import ____" and "from ___ import *"
By doing: import pygame In order for you to call any methods/attributes from that module, you would need to call pygame.method_one() pygame.attribute_zed If instead, you do: from pygame import * You can now call those same methods/attributes like so: method_one() attribute_zed See the difference? By using from __ import * you can call just the method name without havng to append the module name. Although this seems easier/better, it is considered bad practice, because in larger programs there could be multiple modules that have the same methods and/or it makes it difficult to look at code and determine from what module a particular method comes. More on reddit.com
🌐 r/learnpython
18
26
April 12, 2014
What is the difference between "import x" with "from x import *"
The issue comes when you have modules which contain things with the same name. Suppose you need to use the modules math, cmath, and numpy all in the same code (god help you). Each of these modules defines the trig functions, sin, cos, and tan; and each does something slightly different. So if you use a from import: from math import * from cmath import * from numpy import * what happens when I write sin(x)? What if I import like this? from cmath import * from numpy import * from math import * No, better to import the modules and reference that way. import math import cmath import numpy That way I can say which implementation I need with something like numpy.sin or cmath.sin. If there's something that you're using a lot and it's tedious to name the module each time, you could import just that thing by name. from dataclasses import dataclass from functools import wraps from math import sin, cos, tan, pi More on reddit.com
🌐 r/learnpython
50
196
February 18, 2019
Import or from for modules
I came across this in my reading. This was at the top of the script. My questions are: Why would you use “from” if you are loading all if it? I understood “from” was for importing parts of a module, example: from math import sin. Not the whole module. More on discuss.python.org
🌐 discuss.python.org
4
0
August 24, 2022
When/why should i use "from module import *" instead of "import module" ?
(Python 3.5.2 docs)[ https://docs.python.org/3/tutorial/modules.html] It's a namespace thing. When you use import module, you call it's functions/variables/etc. with module.function/attribute in the same way you would a different class: import math x = math.sin(math.sqrt(2)) class MyClass: def __init__(self, x): self.x = x foo = MyClass(4) print(foo.x) # 4 If you use from module import attr', it takes the nameattr' and brings it into the local namespace, so you don't need to call module.: from math import sqrt, sin x = sin(sqrt(2)) from module import * does this for every attribute (function/variable) in the module. from math import * x = sin(sqrt(2)) You can also use import ... as, which also defines a name. Say you had a module or attribute with a long name, like `This_Is_A_Long_Module_Namee". Rather than have to type that every time you need to reference it, you can do import This_Is_A_Long_Module_Name as longmod foo = longmod.bar() This works for methods as well: from foo import annoying_method_name as bar baz = bar() instead of import foo baz = foo.annoying_method_name() Edit - formatting More on reddit.com
🌐 r/learnpython
24
58
December 10, 2016
🌐
Real Python
realpython.com › ref › keywords › from
from | Python Keywords – Real Python
In Python, the from keyword is primarily used to import specific objects from a module into your current namespace.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-from-keyword
Python from Keyword - GeeksforGeeks
July 17, 2026 - DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 17 Jul, 2026 · The from keyword is used to import specific functions, classes, or variables from a module.
🌐
Codecademy
codecademy.com › docs › python › keywords › from
Python | Keywords | from | Codecademy
October 24, 2025 - The from keyword in Python is used to import specific items (such as functions, classes, or variables) from a module instead of importing the entire module.
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.2 documentation
Python’s default sys.meta_path has three meta path finders, one that knows how to import built-in modules, one that knows how to import frozen modules, and one that knows how to import modules from an import path (i.e.
🌐
Tutorialspoint
tutorialspoint.com › python › python_from_keyword.htm
Python from Keyword
The Python from keyword is used to import a specific section from a modules, classes or functions. It is a case-sensitive keyword. Without import keyword we cannot use the from because both are interdependent and raise a SyntaxError.
Find elsewhere
🌐
YouTube
youtube.com › watch
Python Full Course for Beginners - YouTube
Master Python from scratch 🚀 No fluff—just clear, practical coding skills to kickstart your journey! 🚀 Want to dive deeper?- Check out my complete Python m...
Published: February 12, 2025
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
What does "from . import *" mean. - Raspberry Pi Forums
from foo import * Python will go off looking in $PYTHONPATH for foo.py, and import all of its methods when it is found. If you specific a path, such as . for the current working directory, Python will read all of the *.py files in that directory and import their methods.
🌐
W3Schools
w3schools.com › python › python_modules.asp
Python Modules
The dir() function: List all the ... used on all modules, also the ones you create yourself. You can choose to import only parts from a module, by using the from keyword....
🌐
Python
python.org
Welcome to Python.org
The official home of the Python Programming Language
🌐
W3Schools
w3schools.com › python › gloss_python_module_import_from.asp
Python Import From Module
Python Examples Python Compiler ... Study Plan Python Interview Q&A Python Training ... You can choose to import only parts from a module, by using the from keyword....
Top answer
1 of 11
265
The issue comes when you have modules which contain things with the same name. Suppose you need to use the modules math, cmath, and numpy all in the same code (god help you). Each of these modules defines the trig functions, sin, cos, and tan; and each does something slightly different. So if you use a from import: from math import * from cmath import * from numpy import * what happens when I write sin(x)? What if I import like this? from cmath import * from numpy import * from math import * No, better to import the modules and reference that way. import math import cmath import numpy That way I can say which implementation I need with something like numpy.sin or cmath.sin. If there's something that you're using a lot and it's tedious to name the module each time, you could import just that thing by name. from dataclasses import dataclass from functools import wraps from math import sin, cos, tan, pi
2 of 11
22
The most important difference is that import x essentially gives you access to the x namespace where you can use its contents with syntax like x.foo(), which would call x’s function foo. from x import *, on the other hand, will import all of x’s info into your current namespace. This way, when you type foo(), you’re calling x’s function without having to use the namespace dot syntax to reach it. While this seems more convenient for some cases, it is considered bad practice. By importing everything into your current ns, it because difficult to understand where a function (or whatever) is coming from, and if you have any problems with the * imported info, it becomes significantly more problematic to debug. Additionally, as others pointed out in other comments, if you have functions or something which share the same name, then importing with * makes it far more difficult to understand where your foo() is coming from, which can lead to problems. That said, I would definitely encourage you not to forget it, because it’s a good tool to have when writing throwaway scripts for quick debugging or when you’re in an interactive shell session.
🌐
Python.org
discuss.python.org › python help
Import or from for modules - Python Help - Discussions on Python.org
August 24, 2022 - I came across this in my reading. This was at the top of the script. My questions are: Why would you use “from” if you are loading all if it? I understood “from” was for importing parts of a module, example: from math import sin. Not the whole module.
🌐
YouTube
youtube.com › watch
"import" vs. "from .. import" in Python: Which uses more memory? - YouTube
Python offers us two ways to import modules: We can use the "import" statement to load an entire module, or "from...import" to define specific names. The que...
Published: July 5, 2020
🌐
Core Electronics
core-electronics.com.au › guides › import-from-as-python
Import, From and As Keywords in Python - Tutorial Australia
April 19, 2022 - Greetings on your next step in your Python trip. This guide will focus on the | import |, | from | and | as | keywords. These keywords distinctly focus on ways to introduce new functionality to Python. For stock Python to retain its lightweight and speedy status it allows access only for a small core of modules.
🌐
Reddit
reddit.com › r/learnpython › when/why should i use "from module import *" instead of "import module" ?
r/learnpython on Reddit: When/why should i use "from module import *" instead of "import module" ?
December 10, 2016 -

Even after reading about it, i'm still a bit confused about with which method i should use. For example i wanted to use this guy's great module to have the logging module output to a file and the console at the same time.

But i tried using this it will return an error: NameError: name 'initialize_logger' is not defined

import loggerinitializer

And this will not and will work just fine:

from loggerinitializer import *

So i suppose there is something i still don't get about import, could someone help me ?

EDIT: added which of the 2 didn't work

🌐
W3Schools
w3schools.com › python › default.asp
Python Tutorial
Download Python from the official Python web site: https://python.org
🌐
Python
python.org › about › gettingstarted
Python For Beginners | Python.org
There is a fairly brief tutorial that gives you basic information about the language and gets you started. You can follow this by looking at the library reference for a full description of Python's many libraries and the language reference for a complete (though somewhat dry) explanation of Python's syntax.