import os os.chdir('C:\\doit') import mod2 print(mod2.PI) result = mod2.add(3,4) print(result)
# 모듈이란 함수나 변수 또는 클래스를 모아 놓은 파일이다. # 다른 파이썬 프로그램에서 불러와 사용할 수 있게끔 만든 파이썬 파일 # import (모듈내임) # mod1.py 로 저장했다면 # def add(a, b): # return a + b # def sub(a, b): # return a - b import mod1 import mod2 from mod1 import add, sub #from mod1 import * print(add(1, 4)) print(sub(4, 2)) PI = 3.141592 class Math: def solv(self, r): return PI * (r**2) # 원의 넓이 계산 print(Math.solv(PI, 5)) print(mod1.add(3, 4)) print(mod2.add(mod2.PI, 4.4))
print(add(6, 4)) print('=' * 50) from mod1 import * # mod1에 있는 모든 함수를 사용하겠다는 의미 print(add(4, 3)) print(sub(4, 3)) print('=' * 50) # 클래스나 변수 등을 포함한 모듈 import mod2 print(mod2.PI) a = mod2.Math() print(a.solv(2)) print(mod2.add(4, 2)) print('=' * 50) # 다른 디렉토리에 있는 모듈 사용 방법. # 1. sys.path.append(모듈을 설치한 디렉토리) 사용 # import sys # print(sys.path) # 여기에 포함된 경로에 있으면 바로 모듈을 불러서 사용 가능하다. # sys.path.append('./Modules') # print(sys.path) # sys.path.pop() # print(sys.path) # 2.PYTHONPATH 환경변수 사용
def add(a, b): return a + b import mod2 print(mod2.PI) a = mod2.Math() print(a.solv(2)) ##### 다른 파일에서 모듈 불러오기 # 다른 파이썬 파일에서 이전에 만들어 놓은 모듈을 불러와서 사용하는 방법 # modtest.py import mod2 result = mod2.add(3, 4) print(result) import modtest ### 모듈을 불러오는 또 다른 방법 # 모듈을 저장한 디렉토리로 이동하지 않고 모듈을 불러와서 사용하는 방법 # 명령프롬프트창에 입력 # cd c:\Python\doit # mkdir mymod # move mod2.py mymod ## 1. sys.path.appendO(모듈을 저장한 디렉토리)사용하기 import sys
# -*- coding: utf-8 -*- """ Created on Mon Feb 8 16:16:19 2021 @author: dhtmd """ import mod import mod2 mod.add(3, 4) mod.sub(4, 2) mod2.add(3, 4) mod2.sub(4, 2)
cal = MaxLimitCalculator() cal.add(50) # 50 더하기 cal.add(60) # 60 더하기 print(cal.value) # 100 출력 import mod1 print(mod1.add(3,5)) print(mod1.sub(3000,5)) import sys print(sys.path) sys.path.append("C:/") print(sys.path) import mod2 print(mod2.add(3,4)) # # import tensorflow as tf # # hello=tf.constant("hello(안녕)") # print(type(hello)) # # #세션에 데이터를 담는다. 캡슐화 # sess=tf.Session() # print(sess.run(hello).decode("utf-8")) import tensorflow as tf x_data = [1., 2., 3.] y_data = [1., 2., 3.]
import mod1 #모듈 불러오기 import mod2 from mod1 import add, sub #모듈 이름 안붙임 from mod1 import * print(mod1.add(3, 4)) #모듈명.함수명 print(add(3, 4)) a = mod2.Math() print(a.solv(2)) print(mod2.add(mod2.PI, 4.4)) result = mod2.add(3, 4) print(result)
# -*- coding: utf-8 -*- """ Created on Mon Feb 8 08:30:32 2021 @author: myung modex1.py : mod 모듈 사용하기 """ import mod #모듈 가져오기. import mod2 print("mod 모듈", mod.add(3, 4)) print("mod 모듈", mod.sub(4, 2)) print("mod2 모듈", mod2.add(3, 4)) print("mod2 모듈", mod2.sub(4, 2))
# import의 사용 방법 # import 모듈이름 # from 모듈이름 import 모듈함수 # mod1.py from mod1 import add print(add(3, 4)) from mod1 import add, sub # 두 가지 함수 불러오기 from mod1 import * # mod1의 모든 함수 불러오기 # if __name__ == "__main__": 의 의미 # __name__ 변수란? # 파이썬의 __name__ 변수는 파이썬이 내부적으로 사용하는 특별한 변수 이름이다 ## 클래스나 변수 등을 포함한 모듈 # mod2.py # import mod2 # print(mod2.add(mod2.PI, 4.4)) ## 다른 파일에서 모듈 불러오기 # 1) sys.path.append(모듈을 저장한 디렉터리) 사용하기 import sys print(sys.path) sys.path.append('D:\Heechul\Python_study\jump_to_python\chapter05\mymod') print(sys.path) import mod2 print(mod2.add(3, 5))
import mod2 print(mod2.__doc__) from mod2 import add, sub print(add(3, 4)) print(sub(3, 1))
import mod1, mod2 a = mod1.add(1, 2) print(a) b = mod2.add(3, 4) print(a) c = mod2.PI d = mod2.Math().solv(2) print(c) print(d)
# seems like 라이브러리? #만약 다른 폴더에 모듈이 있을경우 sys 에 경로를 추가해주고 from 으로 가져온다. import sys sys.path.append("C:\\Users\\dkrle\\Desktop\\project\\python\\subfolder") from mod2 import add from mod1 import sum #이렇게 모드1 안에 있는 함수임을 명시해줘야하나 보다. #import mod1 이라고 해줄때는 밑과 같이 적는다. #print(mod1.sum(4,5)) #from mod1 import sum 이라고 해주면 밑과 같이 해준다. print(sum(4, 5)) print(add(4, 5)) #모듈에 print 해주니까 print 도 불러오고 sum도 불러옴 # 만약 패키지 이면 이게 진짜 라이브러리랑 비슷 ㅇㅇ # game/ # __init__.py # sound/ # __init__.py # echo.py # graphic/ # __init__.py # render.py # 위와 같이 폴더가 구성되어 있다면 # from game.sound import echo # echo.loud() 어쩌구 이렇게 쓸 수 있고
C:\Users\pahkey> cd C:\doit C:\doit> python Type "help", "copyright", "credits" or "license" for more information. import mod2 print(mod2.PI) >>> 3.141592 위 예에서 볼 수 있듯이 mod2.PI처럼 입력해서 mod2.py 파일에 있는 PI 변수 값을 사용할 수 있다. mod2.py에 있는 Math 클래스를 사용하는 방법 a = mod2.Math() print(a.solv(2)) >>> 12.566368 모듈 안에 있는 클래스를 사용하려면 "."(도트 연산자)로 클래스 이름 앞에 모듈 이름을 먼저 입력해야 한다. print(mod2.add(mod2.PI, 4.4)) >>> 7.541592 add 함수도 당연히 사용할 수 있다. # 다른 파일에서 모듈 불러오기 python.py파일과 python2.py 파일이 동일한 디렉터리에 있으면 어디서든 불러올수있다. # sys.path.append(모듈을 저장한 디렉터리) 사용하기 sys 모듈은 파이썬을 설치할 때 함께 설치되는 라이브러리 모듈이다. 이 sys 모듈을 사용하면 파이썬 라이브러리가 설치되어 있는 디렉터리를 확인할 수 있다. sys.path >>> ['', 'C:\\Windows\\SYSTEM32\\python37.zip', 'c:\\Python37\\DLLs', 'c:\\Python37\\lib', 'c:\\Python37', 'c:\\Python37\\lib\\site-packages'] sys.path는 파이썬 라이브러리가 설치되어 있는 디렉터리를 보여 준다.
# 서로다른 주소 값을 가지게 된다 # 즉, 변경 전에 Family.lasname 과 a.lastname,b.lastname은 같지만 # a.lastname의 값을 변경하면 Family.lastname, Family.lastname과 다른 값을 가진다 # Family.lastname을 변경하면 a,b 둘다 바뀜 # a값을 바꾸면 family를 변경해도 안바뀜. # a, b 변경 후 family를 변경해도 a,b 값 변경안됨 # 모듈 # 함수나 변수 또는 클래스를 모아 놓은 파일 # .py로 만든 파일은 모두 모듈 # import 모듈이름(파일이름) // mod1.add() # mod1.add 쓰지않고 add처럼 쓰고 싶을 때 # from 모듈이름(파일이름) import 모듈함수(메소드) # 모듈 내 모든 메소드 쓰고 싶을 때 # from 모듈 import 메소드, 메소드 # from 모듈 import * # import mod1 # print(mod1.add(3, 4) #모듈은 동일한 디렉토리 안에 있어야 한다 #혹은 라이브러리가 저장된 폴더에 있어야 한다 #모듈 내 변수, 클래스 메소드 사용 #터미널에서 setPYTHONPATH = C:\doit\mymod(폴더위치) import mod2 print(mod2.PI) #모듈 내 변수 a = mod2.Math() # 모듈 내 클래스 print(a.solv(2)) # 모듈 내 클래스 내에 있는 함수 print(mod2.add(mod2.PI, 4.4)) #모듈 내 함수
import mod1 import mod2 if __name__ == "__main__": mod1.pat1(10) mod1.pat2(10) print(mod2.add(4, 5)) print(mod2.sub(5, 6)) mod2.hello('sachin')