0
from ** import **
from somewhere import certain function
2 Réponses
+ 2
from the python docs:
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 only way. Functions such as importlib.import_module() and built-in __import__() can also be used to invoke the import machinery.
Other usefull links:
https://stackoverflow.com/questions/9439480/from-import-vs-import
https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import
python docs --->>> https://docs.python.org/3/reference/import.html
https://www.w3schools.com/python/python_modules.asp
0
The main difference is in namespaces.
When you use bare import of a module by it's name or alias, you can access it's functions by calling module name(alias) and a respective function.
Whtn you use a from statement, you can access the imported code by it name directly.
Assume, we've got a module called "sololearn" and like to use its "becool" subroutine. Then, with import you can use the following code:
import sololearn
my_task=sololearn.becool()
And with "from" constuction you can do the following:
from sololearn import becool
my_task=becool()
Note, that in the later example we have becool routine imported directly in our code's namespace. Along with some convenience it has also some dangers like overriding builtin functions with the same name. So use it with care.