Ask coding questions

← Back to all posts
import *
WilliamXing (45)

In many of your python programs, you use from _ import . What does import import?

Answered by Coder100 (17038) [earned 5 cycles]
View Answer
Comments
hotnewtop
SameeraMurthy (110)

@WilliamXing The import keyword imports other modules, and gives you new features.
For Example:

# Imports the math module, lets you do math.
import math 

# Use the math module
# Get the Square Root of 9
print (math.sqrt(9))

Result:

3.0
WilliamXing (45)

@SameeraMurthy I know but what does

from tkinter import *

do?

WilliamXing (45)

My question wasn't "What does import do?", but it's "What does

import *

do?

WilliamXing (45)

An example is

from tkinter import *

What does

import *

import from tkinter?

Coder100 (17038)

it imports all the functions into the scope

it's sort of analagous to

from math import sin

and then in some langs, * means anything, so

from math import *

sort of speaks from math import everything
@WilliamXing

WilliamXing (45)

Then why don't we do

import math

? doesn't that also import everything? @Coder100

SameeraMurthy (110)

@WilliamXing if u import it this way:

import math

U have to do math.pi

If you do

from math import *

You can do pi

Coder100 (17038)

no, that imports everything like
math.sin

it's scoped smh @WilliamXing

RYANTADIPARTHI (6001)

Solution

the * just means import everything from this module, or file.

module

from random import *

file.

from FILENAME import *

like that.
That's it.

Coder100 (17038)

The * keyword imports everything into the global scope

that way

from math import *

pi # 3.141592653589793238462643383279502