# Python: Import and Modules

# project structure

+-- learn-how-to-import
|    +-- main.py
|    +-- my_code.py
1
2
3

# Keyword: import

my_code.py

number = 10

def add(a):
    print(a+number)
1
2
3

main.py

import my_code

print(my_code.number)
my_code.add(5)
1
2
3
4
output
10
15
1
2

# Keyword: from import

main.py

from my_code import number, add

print(number)
add(20)
1
2
3
4
outout
10
30
1
2

# Python Modules

# project structure


 
 
 



+-- lesson-01
|    +-- other_code
     |    +-- __init__.py  
     |    +-- code_a.py  
|    +-- main.py
|    +-- my_code.py
1
2
3
4
5
6

./other_code/__init__.py (2 underscores + init + 2 underscores)

./other_code/code_a.py

a = 10

def say_hello(name):
    print("Hi, my name is "+name)
1
2
3

main.py

from other_code import code_a

code_a.say_hello("CodeKids")
print(code_a.a)


from other_code.code_a import say_hello

say_hello("CodeKids")
1
2
3
4
5
6
7
8
9
output
Hi, my name is CodeKids
10
Hi, my name is CodeKids
1
2
3