[module] sys, sys.path, int 최대 값

markyang92·2021년 5월 16일
0

python

목록 보기
25/42
post-thumbnail

import sys

sys.version

import sys
print(sys.version)

  • #!/usr/bin/env python3 지정해도
    python 명령이 Python 2.7.16을 가리키고 있다면..!

$ python <PI_APP>.py

로 실행 시키면, python으로 지정된 버전으로 실행된다.


  1. $ chmod +x <PI_APP>.py로 permission을 x 권한 주고 스크립트로 실행해보자.
  2. #!/usr/bin/env python3로 지정한 버전으로 실행된다.

sys.argv: cmd args list

import sys

print( sys.argv )
--- 출력 ---
$ ./sample 1
['./sample.py', '1']
  • 커맨드라인 아규먼트를 리스트로 보관
    • 각 아규먼트는 sys.argv[<Nums>]를 사용하면 된다.
      • sys.argv[0]: ./sample.py
      • sys.argv[1]: 1

sys.exit()

  • 프로그램 종료

sys.modules: 현재 로딩된 모듈을 dictionary

import sys

print( sys.modules )
--- 출력 ---
{'sys': <module 'sys' (built-in)>, \
'builtins': <module 'builtins' (built-in)>, \
'_frozen_importlib': <module '_frozen_importlib' (frozen)>, \
'_imp': <module '_imp' (built-in)>, \
'_thread': <module '_thread' (built-in)>, \
'_warnings': <module '_warnings' (built-in)>, \
'_weakref': <module '_weakref' (built-in)>, \
'_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, \
'posix': <module 'posix' (built-in)>, \
'_io': <module 'io' (built-in)>, \
'marshal': <module 'marshal' (built-in)>, \
'time': <module 'time' (built-in)>, \
'zipimport': <module 'zipimport' (frozen)>, \
'_codecs': <module '_codecs' (built-in)>, \
'codecs': <module 'codecs' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/codecs.py'>, \
'encodings.aliases': <module 'encodings.aliases' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/encodings/aliases.py'>, \
'encodings': <module 'encodings' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/encodings/__init__.py'>, \
'encodings.utf_8': <module 'encodings.utf_8' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/encodings/utf_8.py'>, \
'_signal': <module '_signal' (built-in)>, 'encodings.latin_1': <module 'encodings.latin_1' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/encodings/latin_1.py'>, \
'_abc': <module '_abc' (built-in)>, 'abc': <module 'abc' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/abc.py'>, \
'io': <module 'io' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/io.py'>, \
'__main__': <module '__main__' from '/Users/yangdonghyeon/workspace/./sample.py'>, \
'_stat': <module '_stat' (built-in)>, \
'stat': <module 'stat' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/stat.py'>, \
'_collections_abc': <module '_collections_abc' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_collections_abc.py'>, \
'genericpath': <module 'genericpath' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/genericpath.py'>, \
'posixpath': <module 'posixpath' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/posixpath.py'>, \
'os.path': <module 'posixpath' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/posixpath.py'>, \
'os': <module 'os' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py'>, \
'_sitebuiltins': <module '_sitebuiltins' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_sitebuiltins.py'>, \
'_locale': <module '_locale' (built-in)>, \
'_bootlocale': <module '_bootlocale' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_bootlocale.py'>, \
'types': <module 'types' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/types.py'>, \
'enum': <module 'enum' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/enum.py'>, \
'_sre': <module '_sre' (built-in)>, \
'sre_constants': <module 'sre_constants' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_constants.py'>, \
'sre_parse': <module 'sre_parse' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_parse.py'>, \
'sre_compile': <module 'sre_compile' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/sre_compile.py'>, \
'_heapq': <module '_heapq' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload/_heapq.cpython-39-darwin.so'>, \
'heapq': <module 'heapq' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/heapq.py'>, \
'itertools': <module 'itertools' (built-in)>, 'keyword': <module 'keyword' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/keyword.py'>, \
'_operator': <module '_operator' (built-in)>, 'operator': <module 'operator' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/operator.py'>, \
'reprlib': <module 'reprlib' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/reprlib.py'>, \
'_collections': <module '_collections' (built-in)>, \
'collections': <module 'collections' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/collections/__init__.py'>, \
'_functools': <module '_functools' (built-in)>, \
'functools': <module 'functools' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/functools.py'>, \
'copyreg': <module 'copyreg' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/copyreg.py'>, \
're': <module 're' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py'>, \
'sitecustomize': <module 'sitecustomize' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/sitecustomize.py'>, \
'site': <module 'site' from '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site.py'>}

sys.path: 현재 Python PATH

  • system의 $PATH가 아님!! 파이썬이 보는 PATH!
import sys

print( sys.path )
--- 출력 ---
['/Users/yangdonghyeon/workspace', \
'/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', \
'/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9', \
'/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', \
'/opt/homebrew/lib/python3.9/site-packages']

sys.path 활용해 나의 Python file import

  • bitbake를 예로 둔다.

sys.exc_info(): exception return

  • 현재 발생한 익셉션을 튜플로 반환
    • 없는 경우 None 반환

sys.prefix: 파이썬 설치 경로

  • 현재 실행되는 파이썬이 설치된 경로
  • sys.exec_prefix도 똑같음

sys.executable

  • 현재 실행되는 파이썬 bin file

sys.exit([args])

  • 프로세스를 종료
    • return args == 0 : 정상 종료
    • return args != 0 : 비 정상 종료

sys.getrefcount(object): 객체 참조 값 카운트


sys.getdefaultencoding()

import sys

print(sys.getdefaultencoding()) # 'utf-8'

sys.stdin.write("hi python")

  • sys.stdin.write() stdin에 문자열을 write

sys.stderr.write("hi python")

  • sys.stderr.write() stderr에 문자열 write

int 최대값

sys.maxsize

import sys

print(sys.maxsize)

--------------------
9223372036854775807
profile
pllpokko@alumni.kaist.ac.kr

0개의 댓글