Import (beginner level)

Hello everyone. Right now I’m learning the modules.

We’ve got 2 files** “” **and **“” **(<- in this file we defined some functions)

Let’s *import * the functions of “” into ** “” ** (they are in the same folder)

So we have two options:

  1. Import functions


  1. from functions import function_x, function_y

In ** “” **outside of definitions let’s write print(“something”)

It’s gonna be printed out for Import functions , which is normal as I think, because we import the entire file.
However, it’s gonna be printed out for from functions import function_x, function_y as well, that can’t understand, because there we specified what function we want to import and from what file. Nothing else.

So, theprint(“something”)IS outside the definitions we asked to import, how is it getting imported?
If it’s gonna be printed out, anyways, so why did we specify each function one by one carefully?

When you import a module for the first time, the module has to be run by the Python interpreter. It doesn’t matter whether you use import module or from module import function the module still has to run. So any prints inside the module will get run.

Using the from form of imports is just a shortcut to save typing later on. Instead of doing this:

import math

a = math.sin(2.4)

b = math.sqrt(math.cos(0.5))

we can do this:

from math import cos, sin, sqrt

a = sin(2.4)

b = sqrt(cos(0.5))