강의영상

- (1/7) intro

- (2/7) import 사용방법, 도움말 작성기능

- (3/7) import 사용시 주의점

- (4/7) import 고급

- (5/7) site-packages 1

- (6/7) site-packages 2

- (7/7) 모듈, 패키지, 라이브러리, 숙제설명

intro

- 현재 파이썬은 길이가 2인 벡터의 덧셈을 지원하지 않음

a=[1,2]
b=[3,4]
a+b
[1, 2, 3, 4]

- 아래와 같은 기능을 구현하는 함수를 만들고 싶음

[1,2], [3,4] -> [4,6]

- 구현

def vec2_add(a,b): 
    return [a[0]+b[0], a[1]+b[1]]

- test

a=[1,2]
b=[3,4]
vec2_add(a,b)
[4, 6]

make myfuns.py

- 생각해보니까 vec2_add는 내가 앞으로 자주 쓸 기능임

- 그런데 현재 사용방법으로는 내가 노트북파일을 새로 만들떄마다 def vec2_add(a,b): 와 같은 형태로 vec2_add를 매번 정의해줘야 하는 불편한이 있다.

해결1

- 자주 사용하는 함수를 myfuns.py에 저장한다.

# myfuns.py
def vec2_add(a,b): 
    return [a[0]+b[0], a[1]+b[1]]

- %run myfuns를 실행

준비: "00" -> 커널재시작

%run myfuns 
vec2_add([1,2],[3,4])
[4, 6]

해결2

- 자주 사용하는 함수를 myfuns.py에 저장한다.

# myfuns.py
def vec2_add(a,b): 
    return [a[0]+b[0], a[1]+b[1]]

- import myfuns를 이용

(준비) "00" -> 커널재시작

import myfuns 
a=[1,2]
b=[3,4]
myfuns.vec2_add(a,b)
[4, 6]

import 기본

사용방법

- 사용방법1

준비: "00" -> 커널재시작

import myfuns 
myfuns.vec2_add([1,2],[3,4]) 
[4, 6]
  • myfuns.vec2_add 의 의미: myfuns.py 라는 파일안에 vec2_add라는 함수가 있음. 그것을 실행하라.
  • .의 의미: 상위.하위의 개념!

(주의) 아래와 같이 사용불가능 하다.

vec2_add([1,2],[3,4])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 vec2_add([1,2],[3,4])

NameError: name 'vec2_add' is not defined

- 사용방법2

준비: "00" -> 커널재시작

from myfuns import vec2_add 
vec2_add([1,2],[3,4])
[4, 6]

(주의) 이 경우는 오히려 아래가 불가능함

myfuns.vec2_add([1,2],[3,4]) # myfuns안의 vec2_add만 임포트했지 myfuns자체를 임포트 한것은 아님 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 myfuns.vec2_add([1,2],[3,4])

NameError: name 'myfuns' is not defined

- 사용방법3

준비: "00" -> 커널재시작

import myfuns
from myfuns import vec2_add
myfuns.vec2_add([1,2],[3,4])
[4, 6]
vec2_add([1,2],[3,4])
[4, 6]

- 사용방법4

준비: "00" -> 커널재시작

from myfuns import vec2_add, vec2_sub 
vec2_add([1,2],[3,4])
[4, 6]
vec2_sub([1,2],[3,4])
[-2, -2]

- 사용방법5

준비: "00" -> 커널재시작

from myfuns import * #*는 all의 의미 
vec2_add([1,2],[3,4])
[4, 6]
vec2_sub([1,2],[3,4])
[-2, -2]

- 사용방법6

준비: "00" -> 커널재시작

import myfuns as mf 
mf.vec2_add([1,2],[3,4])
[4, 6]
mf.vec2_sub([1,2],[3,4])
[-2, -2]

(오히려 아래는 실행불가능)

myfuns.vec2_add([1,2],[3,4])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 myfuns.vec2_add([1,2],[3,4])

NameError: name 'myfuns' is not defined
myfuns.vec2_sub([1,2],[3,4])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 myfuns.vec2_sub([1,2],[3,4])

NameError: name 'myfuns' is not defined

- 잘못된 사용방법1

준비: "00" -> 커널재시작

import myfuns as mf 
from mf import vec2_add 
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 2>()
      1 import myfuns as mf 
----> 2 from mf import vec2_add

ModuleNotFoundError: No module named 'mf'

- 사용방법7

준비: "00" -> 커널재시작

import myfuns as mf 
from myfuns import vec2_add 
mf.vec2_add([1,2],[3,4])
[4, 6]
vec2_add([1,2],[3,4])
[4, 6]

- 사용방법8

준비: "00" -> 커널재시작

import myfuns as mf 
from myfuns import vec2_add as add 
mf.vec2_add([1,2],[3,4])
[4, 6]
vec2_add([1,2],[3,4])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 vec2_add([1,2],[3,4])

NameError: name 'vec2_add' is not defined
add([1,2],[3,4])
[4, 6]

도움말 작성기능

- mf란 무엇인가?

준비: "00" -> 커널재시작

import myfuns as mf 
mf
<module 'myfuns' from '/home/cgb3/Dropbox/07_lectures/IP2022/_notebooks/myfuns.py'>
mf?
Type:        module
String form: <module 'myfuns' from '/home/cgb3/Dropbox/07_lectures/IP2022/_notebooks/myfuns.py'>
File:        ~/Dropbox/07_lectures/IP2022/_notebooks/myfuns.py
Docstring:   <no docstring>
type(mf)
module
  • mf의 타입은 모듈이라고 나옴, 현재 단계에서는 무엇인지 알기 어려움

- Docstring의 내용을 채울 수 있을까?

준비1: myfuns.py 파일을 아래와 같이 수정한다.

준비2: "00" -> 커널재시작

import myfuns as mf 
mf?
Type:        module
String form: <module 'myfuns' from '/home/cgb3/Dropbox/07_lectures/IP2022/_notebooks/myfuns.py'>
File:        ~/Dropbox/07_lectures/IP2022/_notebooks/myfuns.py
Docstring:   이것은 길이가 2인 벡터의 합 혹은 차를 구하는 모듈입니다.

주의점

- myfuns.py는 최초 한번만 import 된다.

준비: "00" -> 커널재시작

import myfuns
myfuns.vec2_add([1,2],[3,4])
[4, 6]

myfuns.py파일을 열고 함수를 아래와 같이 바꾸자.

"""이것은 길이가 2인 벡터의 합 혹은 차를 구하는 모듈입니다.""" 
def vec2_add(a,b): 
    print("이것은 myfuns.py에 정의된 함수입니다") 
    return [a[0]+b[0], a[1]+b[1]]
def vec2_sub(a,b): 
    return [a[0]-b[0], a[1]-b[1]]

다시 myfuns를 로드하고 myfuns.vec2_add 를 실행하여 보자.

import myfuns
myfuns.vec2_add([1,2],[3,4])
[4, 6]

바뀐내용이 적용되지 않는다.

커널을 다시 시작하고 임포트해보자.

"00" -> 커널재시작

import myfuns
myfuns.vec2_add([1,2],[3,4])
이것은 myfuns.py에 정의된 함수입니다
[4, 6]

- myfuns.py는 주피터노트북파일과 같은 폴더에 존재해야 한다.

준비1: "00" -> 커널재시작

준비2: myfuns.py을 복사하여 다른 폴더로 이동. 예를들면 IP0403 폴더를 만들고 그 폴더안에 myfuns.py파일을 복사해서 붙여넣은뒤에 파일이름을 myfuns2.py 로 변경.

import myfuns # 주피터노트북파일과 같은 폴더에 있는 myfuns는 잘 로드되지만 
import myfuns2 # 주피터노트북파일과 다른 폴더에 있는 myfuns2는 그렇지 않다. 
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 import myfuns2

ModuleNotFoundError: No module named 'myfuns2'

- IP0403 폴더에 있는 myfuns2.py를 실행하기 위해서는 아래와 같이 할 수 있다.

준비: "00" -> 커널재시작

from IP0403 import myfuns2
myfuns2.vec2_add([1,2],[3,4]) 
이것은 myfuns2.py에 정의된 함수입니다
[4, 6]

- 아래도 가능하다.

준비: "00" -> 커널재시작

from IP0403.myfuns2 import vec2_add as add 
add([1,2],[3,4])
이것은 myfuns2.py에 정의된 함수입니다
[4, 6]

참고로 아래는 모두 정의되지 않음

IP0403.myfuns2.vec2_add([1,2],[3,4]) 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 IP0403.myfuns2.vec2_add([1,2],[3,4])

NameError: name 'IP0403' is not defined
myfuns2.vec2_add([1,2],[3,4]) 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 myfuns2.vec2_add([1,2],[3,4])

NameError: name 'myfuns2' is not defined
vec2_add([1,2],[3,4]) 
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 vec2_add([1,2],[3,4])

NameError: name 'vec2_add' is not defined

import 고급

폴더와 함께 사용할시

- 언뜻 생각하면 아래가 가능할 것 같다.

import IP0403 
IP0403.myfuns2.vec2_add([1,2],[3,4])

- 하지만 불가능하다.

준비: "00" -> 커널재시작

import IP0403 
  • 되는거아냐?
IP0403.myfuns2.vec2_add([1,2],[3,4])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 IP0403.myfuns2.vec2_add([1,2],[3,4])

AttributeError: module 'IP0403' has no attribute 'myfuns2'
  • 여기서 불가능하다.

- (암기) IP0403 폴더안에 __init__.py라는 파일을 만들고 내용에 아래와 같이 쓰면 가능하다.

# ./IP0403/__init__.py 
from . import myfuns2

준비1: 위의 지침을 따른다.

준비2: "00" -> 커널재시작

import IP0403 
IP0403.myfuns2.vec2_add([1,2],[3,4])
이것은 myfuns2.py에 정의된 함수입니다
[4, 6]

컴퓨터 상식

  • .: 현재폴더를 의미
  • ..: 상위폴더를 의미
  • ./myfuns.py: 현재폴더안에 있는 myfuns.py를 의미
  • ./IP0403/myfuns2.py: 현재폴더만에 IP0403폴더안의 myfuns2.py 파일을 의미
  • ../myfuns.py: 현재폴더보다 한단계상위폴더에 있는 myfuns.py를 의미
  • cd ./IP0403: 현재폴더안에 있는 IP0403폴더로 이동해라. (cd IP0403으로 줄여쓸 수 있음)
  • cd .. 현재폴더보다 한단계 상위폴더로 이동하라.

따라서 from . import myfuns2는 현재폴더에서 myfuns2를 찾아서 임포트 하라는 의미로 해석가능

- 의미상으로 보면 아래가 실행가능할듯 한데 불가능하다.

from . import myfuns
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Input In [3], in <cell line: 2>()
      1 #import myfuns
----> 2 from . import myfuns

ImportError: attempted relative import with no known parent package

site-packages (실습금지)

- 의문: 왜 현재폴더에 numpy.py라든가 numpy라는 이름의 폴더가 없는데도 import 가능한지?

준비: "00" -> 커널재시작

import numpy as np
import IP0403 as ip 
ip?
Type:        module
String form: <module 'IP0403' from '/home/cgb3/Dropbox/07_lectures/IP2022/_notebooks/IP0403/__init__.py'>
File:        ~/Dropbox/07_lectures/IP2022/_notebooks/IP0403/__init__.py
Docstring:   <no docstring>
np?
Type:        module
String form: <module 'numpy' from '/home/cgb3/anaconda3/envs/py310/lib/python3.10/site-packages/numpy/__init__.py'>
File:        ~/anaconda3/envs/py310/lib/python3.10/site-packages/numpy/__init__.py
Docstring:  
NumPy
=====

Provides
  1. An array object of arbitrary homogeneous items
  2. Fast mathematical operations over arrays
  3. Linear Algebra, Fourier Transforms, Random Number Generation

How to use the documentation
----------------------------
Documentation is available in two forms: docstrings provided
with the code, and a loose standing reference guide, available from
`the NumPy homepage <https://www.scipy.org>`_.

We recommend exploring the docstrings using
`IPython <https://ipython.org>`_, an advanced Python shell with
TAB-completion and introspection capabilities.  See below for further
instructions.

The docstring examples assume that `numpy` has been imported as `np`::

  >>> import numpy as np

Code snippets are indicated by three greater-than signs::

  >>> x = 42
  >>> x = x + 1

Use the built-in ``help`` function to view a function's docstring::

  >>> help(np.sort)
  ... # doctest: +SKIP

For some objects, ``np.info(obj)`` may provide additional help.  This is
particularly true if you see the line "Help on ufunc object:" at the top
of the help() page.  Ufuncs are implemented in C, not Python, for speed.
The native Python help() does not know how to view their help, but our
np.info() function does.

To search for documents containing a keyword, do::

  >>> np.lookfor('keyword')
  ... # doctest: +SKIP

General-purpose documents like a glossary and help on the basic concepts
of numpy are available under the ``doc`` sub-module::

  >>> from numpy import doc
  >>> help(doc)
  ... # doctest: +SKIP

Available subpackages
---------------------
doc
    Topical documentation on broadcasting, indexing, etc.
lib
    Basic functions used by several sub-packages.
random
    Core Random Tools
linalg
    Core Linear Algebra Tools
fft
    Core FFT routines
polynomial
    Polynomial tools
testing
    NumPy testing tools
f2py
    Fortran to Python Interface Generator.
distutils
    Enhancements to distutils with support for
    Fortran compilers support and more.

Utilities
---------
test
    Run numpy unittests
show_config
    Show numpy build configuration
dual
    Overwrite certain functions with high-performance SciPy tools.
    Note: `numpy.dual` is deprecated.  Use the functions from NumPy or Scipy
    directly instead of importing them from `numpy.dual`.
matlib
    Make everything matrices.
__version__
    NumPy version string

Viewing documentation using IPython
-----------------------------------
Start IPython with the NumPy profile (``ipython -p numpy``), which will
import `numpy` under the alias `np`.  Then, use the ``cpaste`` command to
paste examples into the shell.  To see which functions are available in
`numpy`, type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
down the list.  To view the docstring for a function, use
``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
the source code).

Copies vs. in-place operation
-----------------------------
Most of the functions in `numpy` return a copy of the array argument
(e.g., `np.sort`).  In-place versions of these functions are often
available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
Exceptions to this rule are documented.

- 추측: ~/anaconda3/envs/py310/lib/python3.10/site-packages/를 찾아가보자. 그곳에 numpy폴더가 있을 것이다.

!ls ~/anaconda3/envs/py310/lib/python3.10/site-packages | grep numpy
numpy
numpy-1.22.2.dist-info

- 추측2: ~/anaconda3/envs/py310/lib/python3.10/site-packages/에 내가 자주 쓰는 기능을 폴더로 만들어서 모아두면 어디서든지 import 할 수 있다.

!mkdir ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin # guebin 폴더 생성 
!cp ./myfuns.py ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin 
# 현폴더에 있는 myfuns.py를 아까만든 guebin 폴더로 복사 
from guebin import myfuns
myfuns?
Type:        module
String form: <module 'guebin.myfuns' from '/home/cgb3/anaconda3/envs/py310/lib/python3.10/site-packages/guebin/myfuns.py'>
File:        ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin/myfuns.py
Docstring:   이것은 길이가 2인 벡터의 합 혹은 차를 구하는 모듈입니다.
!rm  ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin -rf # guebin 폴더삭제 

- 추측3: guebin이 사라진 상태에서는 from guebin import myfuns 이 동작하지 않을 것이다.

준비: "00" -> 커널재시작

from guebin import myfuns
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 from guebin import myfuns

ModuleNotFoundError: No module named 'guebin'

- 추측4: ~/anaconda3/envs/py310/lib/python3.10/site-packages/에서 numpy를 지운다면 numpy를 import할 수 없다.

준비: "00" -> 커널재시작

import numpy as np
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import numpy as np

ModuleNotFoundError: No module named 'numpy'

- 추측5: !pip install numpy를 하면 다시 폴더가 생길 것이다.

!pip uninstall numpy -y 
Found existing installation: numpy 1.22.2
Uninstalling numpy-1.22.2:
  Successfully uninstalled numpy-1.22.2
!pip install numpy 
Collecting numpy
  Downloading numpy-1.22.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)
     |████████████████████████████████| 16.8 MB 11.4 MB/s eta 0:00:01
Installing collected packages: numpy
Successfully installed numpy-1.22.3

모듈, 패키지, 라이브러리?

- 모듈의 개념은 아까 살펴본것과 같다. (import를 하여 생기게 되는 오브젝트)

- 교수님들: 모듈이 모이면 패키지라고 부른다. 그리고 라이브러리는 패키지보다 큰 개념이다.

- 그런데 구분이 모호하다.

import numpy as np
type(np)
module

- python에서의 numpy의 type은 모듈

- 그런데 numpy package 라고 검색하면 검색이 된다.

- 심지어 numpy library 라고 해도 검색가능

- 내생각: 넘파이모듈, 넘파이패키지, 넘파이라이브러리 다 맞는 말임

숙제

myfuns.py 도움말 만드는 예제에서

이것은 길이가 2인 벡터의 합 혹은 차를 구하는 모듈입니다

대신에

이것은 길이가 2인 벡터의 합 혹은 차를 구하는 모듈입니다. (학번: 2022-43052)

와 같이 출력되도록 하고 스크린샷 제출