웹사이트 검색

파이썬 모듈


Python 모듈은 본질적으로 변수, 함수 및 클래스를 포함할 수 있는 Python 스크립트 파일입니다. Python 모듈은 코드를 구성하고 다른 클래스나 Python 스크립트에서 참조하는 데 도움이 됩니다.

파이썬 모듈

Python 정의 및 명령문을 포함하는 파일을 Python 모듈이라고 합니다. 따라서 당연히 파일 이름은 접미사 .py가 추가된 모듈 이름입니다. 더 나은 이해를 위해 파이썬 모듈을 만들어 완전히 탐색해 봅시다. 먼저 다음 내용으로 printNumbers.py라는 파일을 만듭니다.

def printForward(n):

    #print 1 to n
    for i in range(n):
        print(i+1)


def printBackwards(n):

    #print n to 1
    for i in range(n):
        print(n-i)

이제 파이썬 인터프리터에서 다음 명령을 사용하여 이 모듈을 가져옵니다.

import printNumbers

Python 모듈의 특정 기능 가져오기

Python 모듈에 대한 FAQ

Python 모듈과 관련하여 자주 묻는 몇 가지 질문을 살펴보겠습니다.

Python의 내장 모듈은 무엇입니까?

파이썬에는 많은 내장 모듈이 있습니다. 중요한 것 중 일부는 - 시간입니다. Python 셸에서 help(modules) 명령을 실행하여 사용 가능한 모듈 목록을 가져올 수 있습니다.

Python에서 모듈과 패키지의 차이점은 무엇입니까?

Python 패키지는 Python 모듈 모음입니다. Python 모듈은 단일 Python 파일인 반면 Python 패키지는 패키지 세부 정보를 정의하는 여러 Python 스크립트와 __init__.py 파일이 있는 디렉토리입니다.

Python 모듈 목록은 어디에서 찾을 수 있습니까?

Python 모듈 색인의 공식 페이지에서 Python 모듈 목록을 찾을 수 있습니다. 그러나 사용 가능한 Python 모듈을 찾고 있다면 Python 셸에서 help(modules) 명령을 실행하여 사용 가능한 모듈 목록을 얻을 수 있습니다.

가장 중요한 파이썬 모듈 목록은 이 GitHub 리포지토리를 확인하고 특정 자습서 및 예제 프로그램을 통해 배우십시오.

다른 디렉터리에서 모듈을 어떻게 가져올 수 있습니까?

파이썬 모듈을 가져오려고 하면 현재 디렉토리와 PATH 변수 위치를 조사합니다. 따라서 이 위치에 Python 파일이 없으면 ModuleNotFoundError가 발생합니다. 해결책은 sys 모듈을 가져온 다음 필요한 디렉토리를 해당 경로 변수에 추가하는 것입니다. 아래 코드는 다른 디렉터리에서 가져오려고 할 때 발생하는 오류와 해당 디렉터리를 경로 변수에 추가하여 오류를 수정하는 방법을 보여줍니다.

$ python3.7
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import test123
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'test123'
>>> import sys
>>> sys.path.append('/Users/pankaj/temp')
>>> import test123
>>> test123.x
10
>>> test123.foo()
foo
>>> 

파이썬 모듈 목록

수천 개의 Python 모듈이 있으며 매일 더 많은 모듈이 개발되고 있습니다. 많은 인기 있는 Python 모듈에 대한 자습서를 작성했습니다. 이 모듈을 배우려면 아래 표의 링크를 따르십시오.

Python Modules
Python os module
Python sys module
Python time
Python MySQL
Python CSV
Python multiprocessing
Python pickle
Python time sleep
Python queue
Python unittest
Python socket
Python SimpleHTTPServer
Python json
Python signal
Python random
Python System Command
Python Daemon Thread
Python Copy
Python threading module
Python struct
Python logging
Python subprocess
Python argparse
Python functools
Python itertools
Python getopt
Python ftp
Python tarfile
Python lxml
Python ConfigParser
Python datetime
Python decimal module
Python collections
Python zipfile
Python pdb
Python io
Python fractions
Python AST
Python HTTP
Python xmltodict
Python gzip
Python HTML Parser
Python inspect module
Python Send Email
Python tempfile
Python SQLite
Python shutil
Python timeit
Python getpass module
Python urllib
Python pytz
Python pendulum
Python arrow module

참조:

  • https://docs.python.org/3/tutorial/modules.html
  • https://docs.python.org/3/py-modindex.html