python - from ... import OR import ... as for modules - Stack Overflow
Can somebody ELI5 importing modules?
a python module is
-
a python file
-
a directory full of python files (optionally with a
__init__.pyin it)
when you do import foo it thus searches for foo.py or a directory called foo in
-
the directory containing the script with that import statement.
-
everything in the environment variable
$PYTHONPATH -
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)
The Definitive Guide to Python import Statements
A Guide on How Importing Works in Python.
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.
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.