Either way you "import" the complete math
module in a sense that it's compiled and stored in sys.modules
. So you don't have any optimisation benefits if you do from math import sqrt
compared to import math
. They do exactly the same thing. They import the whole math
module, store it sys.modules
and then the only difference is that the first one brings the sqrt
function into your namespace and the second one brings the math
module into your namespace. But the names are just references so you wont benefit memory wise or CPU wise by just importing one thing from the module.
If you want the math.sqrt
syntax then just use import math
. If you want the sqrt()
syntax then use from math import sqrt
.
If your concern is protecting the user of your module from polluting his namespace if he does a star import: from your_module import *
then define a __all__
variable in your module which is a list of strings representing objects that will be imported if the user of your module does a start import.