sys.modulse vs sys.path๐Ÿ™„

Inah-_-ยท2021๋…„ 2์›” 27์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
16/21
post-thumbnail

SYS๋ž€?

SYS(System-specific parameters and functions) ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— ์˜ํ•ด์„œ ์‚ฌ์šฉ๋˜๊ฑฐ๋‚˜
๊ด€๋ฆฌ๋˜๋Š” ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ํ•ญ์ƒ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ


๐Ÿ—ƒ sys.modulse๋ž€?

๋‹จ์ˆœํ•œ dictionary๋กœ, import๋˜์–ด ์žˆ๋Š” ๋ชจ๋“ˆ๊ณผ package๋“ค์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.
ํŒŒ์ด์ฌ์ด ๋ชจ๋“ˆ์ด๋‚˜ ํŒจํ‚ค์ง€๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด ๊ฐ€์žฅ ๋จผ์ € ํ™•์ธํ•˜๋Š” ๊ณณ์œผ๋กœ, ํ•œ๋ฒˆ import๋œ ๋ชจ๋“ˆ๊ณผ package๋“ค์„ ํŒŒ์ด์ฌ์ด ๋˜ ๋‹ค์‹œ ์ฐพ์ง€ ์•Š์•„๋„ ๋˜๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ƒˆ๋กœ importํ•˜๋Š” ๋ชจ๋“ˆ์€ sys.modulse์—์„œ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค.


๐Ÿ—ƒ built-in modules๋ž€?

ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ํŒŒ์ด์ฌ ๊ณต์‹ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Built-in ๋ชจ๋“ˆ๋“ค์€ ์ด๋ฏธ ํŒŒ์ด์ฌ์— ํฌํ•จ๋˜์–ด ๋‚˜์˜ค๋ฏ€๋กœ ํŒŒ์ด์ฌ์ด ์‰ฝ๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ“ƒ sys.path๋ž€?

ํŒŒ์ด์ฌ์ด ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณด๋Š” ์žฅ์†Œ
๊ธฐ๋ณธ์ ์œผ๋กœ list์ด๋ฉฐ string ์š”์†Œ๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” list

๊ฐ string ์š”์†Œ๋“ค์€ ๋‹ค์Œ ์ฒ˜๋Ÿผ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

['',
 '/Users/song-eun-u/anaconda3/bin',
 '/Users/song-eun-u/anaconda3/lib/python36.zip',
 '/Users/song-eun-u/anaconda3/lib/python3.6',
 '/Users/song-eun-u/anaconda3/lib/python3.6/lib-dynload',
 '/Users/song-eun-u/anaconda3/lib/python3.6/site-packages',
 '/Users/song-eun-u/anaconda3/lib/python3.6/site-packages/aeosa',
 '/Users/song-eun-u/anaconda3/lib/python3.6/site-packages/IPython/extensions',
 '/Users/song-eun-u/.ipython']

ํŒŒ์ด์ฌ์€ ์œ„ list์˜ ๊ฐ ๊ฒฝ๋กœ๋ฅผ ํ•˜๋‚˜ ํ•˜๋‚˜ ํ™•์ธํ•˜๋ฉฐ ํ•ด๋‹น ๊ฒฝ๋กœ์—
importํ•˜๊ณ ์ž ํ•˜๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์–ด๋””์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.



์ •๋ฆฌ

sys๋Š” ํŒŒ์ด์ฌ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ชจ๋“ˆ๋กœ,
sys ๋ชจ๋“ˆ์„ importํ•ด์„œ sys.modules์™€ sys.path ๋ฅผ ์ถœ๋ ฅํ• ์ˆ˜๋„ ์žˆ๊ณ  ์ˆ˜์ • ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.

import sys
 
print(sys.path)
print(sys.modules)

ํŒŒ์ด์ฌ์ด importํ•  ๋•Œ์˜ ์ˆœ์„œ

  1. sys.modules
  2. python built-in modules
  3. sys.path
  1. sys.modules์—์„œ import์— ๋ช…์‹œ ๋œ ๋ชจ๋“ˆ์„ ์ฐพ์•„๋ณธ๋‹ค.
  2. ์—†์„ ๊ฒฝ์šฐ python built-in modules์„ ํ™•์ธํ•œ๋‹ค.
  3. ์—†์„ ๊ฒฝ์šฐ sys.path์— ์ง€์ • ๋˜์–ด ์žˆ๋Š” ๊ฒฝ๋กœ๋“ค์„ ํ•˜๋‚˜ ํ•˜๋‚˜ ํ™•์ธํ•œ๋‹ค.
    sys.path์—๋„ ์—†์„ ๊ฒฝ์šฐ ModuleNotFoundError ์—๋Ÿฌ๋ฅผ ๋ฆฌํ„ด
profile
Backend Developer

0๊ฐœ์˜ ๋Œ“๊ธ€