모듈 임포트 하기¶
-
PyObject *PyImport_ImportModule(const char *name)¶
- 반환값: 새 참조. Part of the 안정 ABI.
This is a wrapper around
PyImport_Import()
which takes a const char* as an argument instead of a PyObject*.
-
PyObject *PyImport_ImportModuleNoBlock(const char *name)¶
- 반환값: 새 참조. Part of the 안정 ABI.
이 함수는
PyImport_ImportModule()
의 폐지된 별칭입니다.버전 3.3에서 변경: 이 기능은 다른 스레드가 임포트 잠금을 보유한 경우 즉시 실패했었습니다. 그러나 파이썬 3.3에서는, 잠금 방식이 대부분의 목적에서 모듈 단위 잠금으로 전환되었기 때문에, 이 함수의 특수한 동작은 더는 필요하지 않습니다.
-
PyObject *PyImport_ImportModuleEx(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)¶
- 반환값: 새 참조.
모듈을 임포트 합니다. 내장 파이썬 함수
__import__()
를 통해 가장 잘 설명할 수 있습니다.반환 값은 임포트 된 모듈이나 최상위 패키지에 대한 새로운 참조, 또는 실패 시 예외가 설정된
NULL
입니다.__import__()
와 마찬가지로, 비어 있지 않은 fromlist가 제공되지 않는 한, 패키지의 서브 모듈이 요청되었을 때의 반환 값은 최상위 패키지입니다.임포트 실패는
PyImport_ImportModule()
처럼 불완전한 모듈 객체를 제거합니다.
-
PyObject *PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)¶
- 반환값: 새 참조. Part of the 안정 ABI 버전 3.7 이후로.
모듈을 임포트 합니다. 표준
__import__()
함수가 이 함수를 직접 호출하기 때문에, 내장 파이썬 함수__import__()
를 통해 가장 잘 설명할 수 있습니다.반환 값은 임포트 된 모듈이나 최상위 패키지에 대한 새로운 참조, 또는 실패 시 예외가 설정된
NULL
입니다.__import__()
와 마찬가지로, 비어 있지 않은 fromlist가 제공되지 않는 한, 패키지의 서브 모듈이 요청되었을 때의 반환 값은 최상위 패키지입니다.버전 3.3에 추가.
-
PyObject *PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)¶
- 반환값: 새 참조. Part of the 안정 ABI.
PyImport_ImportModuleLevelObject()
와 비슷하지만, name은 유니코드 객체 대신 UTF-8로 인코딩된 문자열입니다.버전 3.3에서 변경: level의 음수 값은 더는 허용되지 않습니다.
-
PyObject *PyImport_Import(PyObject *name)¶
- 반환값: 새 참조. Part of the 안정 ABI.
이것은 현재 “임포트 훅 함수”를 호출하는 고수준 인터페이스입니다 (명시적인 level 0을 사용하는데, 절대 임포트를 뜻합니다). 현재 전역의
__builtins__
에 있는__import__()
함수를 호출합니다. 이는 현재 환경에 설치된 임포트 훅을 사용하여 임포트가 수행됨을 의미합니다.이 함수는 항상 절대 임포트를 사용합니다.
-
PyObject *PyImport_ReloadModule(PyObject *m)¶
- 반환값: 새 참조. Part of the 안정 ABI.
모듈을 다시 로드(reload)합니다. 다시 로드된 모듈에 대한 참조를 반환하거나, 실패 시 예외가 설정된
NULL
을 반환합니다 (이때 모듈은 여전히 존재합니다).
-
PyObject *PyImport_AddModuleObject(PyObject *name)¶
- 반환값: 빌린 참조. Part of the 안정 ABI 버전 3.7 이후로.
Return the module object corresponding to a module name. The name argument may be of the form
package.module
. First check the modules dictionary if there’s one there, and if not, create a new one and insert it in the modules dictionary. ReturnNULL
with an exception set on failure.참고
This function does not load or import the module; if the module wasn’t already loaded, you will get an empty module object. Use
PyImport_ImportModule()
or one of its variants to import a module. Package structures implied by a dotted name for name are not created if not already present.버전 3.3에 추가.
-
PyObject *PyImport_AddModule(const char *name)¶
- 반환값: 빌린 참조. Part of the 안정 ABI.
Similar to
PyImport_AddModuleObject()
, but the name is a UTF-8 encoded string instead of a Unicode object.
-
PyObject *PyImport_ExecCodeModule(const char *name, PyObject *co)¶
- 반환값: 새 참조. Part of the 안정 ABI.
주어진 모듈 이름(name)(
package.module
형식일 수 있습니다)과 파이썬 바이트 코드 파일에서 읽거나 내장 함수compile()
로 얻은 코드 객체로, 모듈을 로드합니다. 모듈 객체에 대한 새로운 참조를 반환하거나, 또는 에러가 발생하면 예외가 설정된NULL
을 반환합니다. 에러가 발생하면sys.modules
에서 name이 제거됩니다,PyImport_ExecCodeModule()
에 진입할 때 name이sys.modules
에 이미 있어도 그렇습니다.sys.modules
에 불완전하게 초기화된 모듈을 남겨 두는 것은 위험합니다, 그러한 모듈을 임포트 할 때 모듈 객체가 알 수 없는 (그리고 아마도 모듈 작성자의 의도에 비추어볼 때 손상된) 상태에 있음을 알 방법이 없기 때문입니다.The module’s
__spec__
and__loader__
will be set, if not set already, with the appropriate values. The spec’s loader will be set to the module’s__loader__
(if set) and to an instance ofSourceFileLoader
otherwise.The module’s
__file__
attribute will be set to the code object’sco_filename
. If applicable,__cached__
will also be set.이 함수는 이미 임포트 되었다면 모듈을 다시 로드합니다. 모듈을 다시 로드하는 의도된 방법은
PyImport_ReloadModule()
을 참조하십시오.name이
package.module
형식의 점으로 구분된 이름을 가리키면, 이미 만들어지지 않은 패키지 구조는 여전히 만들어지지 않습니다.PyImport_ExecCodeModuleEx()
와PyImport_ExecCodeModuleWithPathnames()
도 참조하십시오.
-
PyObject *PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)¶
- 반환값: 새 참조. Part of the 안정 ABI.
Like
PyImport_ExecCodeModule()
, but the__file__
attribute of the module object is set to pathname if it is non-NULL
.
-
PyObject *PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)¶
- 반환값: 새 참조. Part of the 안정 ABI 버전 3.7 이후로.
Like
PyImport_ExecCodeModuleEx()
, but the__cached__
attribute of the module object is set to cpathname if it is non-NULL
. Of the three functions, this is the preferred one to use.버전 3.3에 추가.
-
PyObject *PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)¶
- 반환값: 새 참조. Part of the 안정 ABI.
PyImport_ExecCodeModuleObject()
와 유사하지만, name, pathname 및 cpathname은 UTF-8로 인코딩된 문자열입니다. pathname의 값이NULL
로 설정된 경우 어떤 값이 cpathname에서 와야하는지 알아내려고 합니다.버전 3.2에 추가.
버전 3.3에서 변경: Uses
imp.source_from_cache()
in calculating the source path if only the bytecode path is provided.
-
long PyImport_GetMagicNumber()¶
- Part of the 안정 ABI.
파이썬 바이트 코드 파일(일명
.pyc
파일)의 매직 번호(magic number)를 반환합니다. 매직 번호는 바이트 코드 파일의 처음 4바이트에 리틀 엔디안 바이트 순서로 존재해야 합니다. 에러 시-1
을 반환합니다.버전 3.3에서 변경: 실패 시
-1
을 반환합니다.
-
const char *PyImport_GetMagicTag()¶
- Part of the 안정 ABI.
PEP 3147 형식 파이썬 바이트 코드 파일 이름의 매직 태그 문자열을 반환합니다.
sys.implementation.cache_tag
의 값은 신뢰할 수 있고 이 함수 대신 사용해야 함에 유의하십시오.버전 3.2에 추가.
-
PyObject *PyImport_GetModuleDict()¶
- 반환값: 빌린 참조. Part of the 안정 ABI.
모듈 관리에 사용되는 딕셔너리(일명
sys.modules
)를 반환합니다. 이것은 인터프리터마다 존재하는 변수임에 유의하십시오.
-
PyObject *PyImport_GetModule(PyObject *name)¶
- 반환값: 새 참조. Part of the 안정 ABI 버전 3.8 이후로.
주어진 이름으로 이미 임포트 된 모듈을 반환합니다. 모듈이 아직 임포트 되지 않았다면
NULL
을 반환하지만 에러는 설정하지 않습니다. 조회에 실패하면NULL
을 반환하고 에러를 설정합니다.버전 3.7에 추가.
-
PyObject *PyImport_GetImporter(PyObject *path)¶
- 반환값: 새 참조. Part of the 안정 ABI.
sys.path
/pkg.__path__
항목 path를 위한 파인더 객체를 반환합니다,sys.path_importer_cache
딕셔너리에서 꺼낼 수도 있습니다. 아직 캐시 되지 않았으면, 경로 항목을 처리할 수 있는 훅이 발견될 때까지sys.path_hooks
를 탐색합니다. 훅이 없으면None
을 반환합니다; 이것은 호출자에게 경로 기반 파인더가 이 경로 항목에 대한 파인더를 찾을 수 없음을 알려줍니다.sys.path_importer_cache
에 결과를 캐시 합니다. 파인더 객체에 대한 새로운 참조를 반환합니다.
-
int PyImport_ImportFrozenModuleObject(PyObject *name)¶
- Part of the 안정 ABI 버전 3.7 이후로.
name이라는 이름의 프로즌 모듈(frozen module)을 로드합니다. 성공하면
1
을, 모듈을 찾지 못하면0
을, 초기화에 실패하면 예외를 설정하고-1
을 반환합니다. 로드가 성공할 때 임포트 된 모듈에 액세스하려면PyImport_ImportModule()
을 사용하십시오. (잘못된 이름에 주의하십시오 — 이 함수는 모듈이 이미 임포트 되었을 때 다시 로드합니다.)버전 3.3에 추가.
버전 3.4에서 변경:
__file__
어트리뷰트는 더는 모듈에 설정되지 않습니다.
-
int PyImport_ImportFrozenModule(const char *name)¶
- Part of the 안정 ABI.
PyImport_ImportFrozenModuleObject()
와 비슷하지만, name은 유니코드 객체 대신 UTF-8로 인코딩된 문자열입니다.
-
struct _frozen¶
이것은 freeze 유틸리티(파이썬 소스 배포의
Tools/freeze/
를 참조하십시오)가 생성한 프로즌 모듈 디스크립터를 위한 구조체 형 정의입니다.Include/import.h
에 있는 정의는 다음과 같습니다:struct _frozen { const char *name; const unsigned char *code; int size; bool is_package; };
버전 3.11에서 변경: The new
is_package
field indicates whether the module is a package or not. This replaces setting thesize
field to a negative value.
-
const struct _frozen *PyImport_FrozenModules¶
이 포인터는
_frozen
레코드의 배열을 가리키도록 초기화되는데, 멤버가 모두NULL
이나 0인 레코드로 끝납니다. 프로즌 모듈이 임포트 될 때, 이 테이블에서 검색됩니다. 제삼자 코드는 이것을 사용하여 동적으로 생성된 프로즌 모듈 컬렉션을 제공할 수 있습니다.
-
int PyImport_AppendInittab(const char *name, PyObject *(*initfunc)(void))¶
- Part of the 안정 ABI.
기존의 내장 모듈 테이블에 단일 모듈을 추가합니다. 이것은
PyImport_ExtendInittab()
을 감싸는 편리한 래퍼인데, 테이블을 확장할 수 없으면-1
을 반환합니다. 새 모듈은 name이라는 이름으로 임포트 될 수 있으며, initfunc 함수를 처음 시도한 임포트에서 호출되는 초기화 함수로 사용합니다.Py_Initialize()
전에 호출해야 합니다.
-
struct _inittab¶
내장 모듈 목록에 있는 단일 항목을 기술하는 구조체. 파이썬을 내장하는 프로그램은
PyImport_ExtendInittab()
과 함께 이러한 구조체의 배열을 사용하여 추가 내장 모듈을 제공 할 수 있습니다. 구조체는 두 개의 멤버로 구성됩니다:-
const char *name¶
모듈 이름, ASCII로 인코딩된 문자열.
-
const char *name¶
-
int PyImport_ExtendInittab(struct _inittab *newtab)¶
내장 모듈 테이블에 모듈 컬렉션을 추가합니다. newtab 배열은
name
필드에NULL
을 포함하는 센티넬(sentinel) 항목으로 끝나야 합니다; 센티넬 값을 제공하지 않으면 메모리 오류가 발생할 수 있습니다. 성공하면0
을, 내부 테이블을 확장하기 위한 메모리가 충분하지 않으면-1
을 반환합니다. 실패하면, 내부 테이블에 모듈이 추가되지 않습니다.Py_Initialize()
전에 호출해야 합니다.If Python is initialized multiple times,
PyImport_AppendInittab()
orPyImport_ExtendInittab()
must be called before each Python initialization.