How does Python know where to import math from?

Question

How does Python know where to import math from?

Answer

Python first checks to see if the module you’re trying to import matches any modules that come built in with the language. If it does, all good! If not, it searches through locations specified by sys.path, which just gives directions to where it can find modules that aren’t built in.
If you want to use modules that aren’t built in, you’ll eventually need to learn how to use pip, which installs and manages Python packages for you!

12 Likes

2 posts were split to a new topic: Why do we Need to Import Modules?

What I don’t understand is why do we have to import these modules if they come built in?

5 Likes

System will be slower with more memory it needs to locate/allocate. Some modules may have the same names or variables, which can confuse the machine. Having that said, if it were possible to have it all installed and imported (the modules), then it requires a manager to overlook all of the packages to make sure there isn’t any redundancies and of the such. No one can learn every library/module, so it’s going to be hard to have them sort through which to script or not script.

5 Likes

You use the module JUST when you need. It’s a matter of memory. Remember try to do the most with less code

1 Like