๐Ÿฅ

[Python] ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ (virtualenv ์„ค์น˜, ์ƒ์„ฑ, ํ™œ์„ฑํ™”, ์ œ๊ฑฐ) ๋ณธ๋ฌธ

linux

[Python] ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ (virtualenv ์„ค์น˜, ์ƒ์„ฑ, ํ™œ์„ฑํ™”, ์ œ๊ฑฐ)

•8• 2023. 5. 16. 20:20

virtualenv vs venv

  • venv๋Š” ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ ์„ค์น˜ ๋ถˆํ•„์š”ํ•˜์ง€๋งŒ virtualenv๋Š” ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋ณ„๋„ ์„ค์น˜ ํ•„์š”
  • ์†๋„: virtualenv๊ฐ€ ๋” ๋น ๋ฆ„
  • ํ™•์žฅ์„ฑ: virtualenv๊ฐ€ ๋” ์ข‹์Œ
  • virtualenv๋Š” python ๋ฒ„์ „์ด ๋‹ค๋ฅธ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ venv๋Š” ๋ถˆ๊ฐ€๋Šฅ
  • virtualenv๋Š” pip ์—…๊ทธ๋ ˆ์ด๋“œ ๊ฐ€๋Šฅํ•˜๋‚˜ venv๋Š” ๋ถˆ๊ฐ€๋Šฅ

→ virtualenv๊ฐ€ ๋” ์ข‹์•„๋ณด์—ฌ์„œ virtualenv๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

 


1. virtualenv ์„ค์น˜

[testuser@test-server-1 ~]$ python3 -m pip install --user -U virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/18/a2/7931d40ecb02b5236a34ac53770f2f6931e3082b7a7dafe915d892d749d6/virtualenv-20.17.1-py3-none-any.whl (8.8MB)
    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 8.9MB 117kB/s 
Collecting importlib-metadata>=4.8.3; python_version < "3.8" (from virtualenv)
  Downloading https://files.pythonhosted.org/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl
Collecting filelock<4,>=3.4.1 (from virtualenv)
  Downloading https://files.pythonhosted.org/packages/84/ce/8916d10ef537f3f3b046843255f9799504aa41862bfa87844b9bdc5361cd/filelock-3.4.1-py3-none-any.whl
Collecting distlib<1,>=0.3.6 (from virtualenv)
  Downloading https://files.pythonhosted.org/packages/76/cb/6bbd2b10170ed991cf64e8c8b85e01f2fb38f95d1bc77617569e0b0b26ac/distlib-0.3.6-py2.py3-none-any.whl (468kB)
    100% |โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ| 471kB 1.8MB/s 
Collecting platformdirs<3,>=2.4 (from virtualenv)
  Downloading https://files.pythonhosted.org/packages/b1/78/dcfd84d3aabd46a9c77260fb47ea5d244806e4daef83aa6fe5d83adb182c/platformdirs-2.4.0-py3-none-any.whl
Collecting importlib-resources>=5.4; python_version < "3.7" (from virtualenv)
  Downloading https://files.pythonhosted.org/packages/24/1b/33e489669a94da3ef4562938cd306e8fa915e13939d7b8277cb5569cb405/importlib_resources-5.4.0-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata>=4.8.3; python_version < "3.8"->virtualenv)
  Downloading https://files.pythonhosted.org/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl
Collecting zipp>=0.5 (from importlib-metadata>=4.8.3; python_version < "3.8"->virtualenv)
  Downloading https://files.pythonhosted.org/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl
Installing collected packages: typing-extensions, zipp, importlib-metadata, filelock, distlib, platformdirs, importlib-resources, virtualenv
Successfully installed distlib-0.3.6 filelock-3.4.1 importlib-metadata-4.8.3 importlib-resources-5.4.0 platformdirs-2.4.0 typing-extensions-4.1.1 virtualenv-20.17.1 zipp-3.6.0

 

2. ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ธฐ

์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ๋กœ๋กœ ์ด๋™ ํ›„ ์•„๋ž˜ ๋ช…๋ น์–ด ์ˆ˜ํ–‰

# python3 -m virtualenv "๊ฐ€์ƒํ™˜๊ฒฝ๋ช…"
[testuser@test-server-1 ~]$ python3 -m virtualenv testenv
created virtual environment CPython3.6.8.final.0-64 in 6112ms
  creator CPython3Posix(dest=/home/testuser/testenv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/testuser/.local/share/virtualenv)
    added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

์™„๋ฃŒ๋˜๋ฉด ํ•ด๋‹น ๊ฒฝ๋กœ์— "๊ฐ€์ƒํ™˜๊ฒฝ๋ช…"์œผ๋กœ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์ƒ๊ธด๋‹ค.

[testuser@test-server-1 ~]$ ll ./testenv/*
-rw-rw-r-- 1 testuser testuser 206  5์›” 16 06:16 ./testenv/pyvenv.cfg

./testenv/bin:
ํ•ฉ๊ณ„ 56
-rw-rw-r-- 1 testuser testuser 2145  5์›” 16 06:16 activate
-rw-rw-r-- 1 testuser testuser 1437  5์›” 16 06:16 activate.csh
-rw-rw-r-- 1 testuser testuser 3022  5์›” 16 06:16 activate.fish
-rw-rw-r-- 1 testuser testuser 3333  5์›” 16 06:16 activate.nu
-rw-rw-r-- 1 testuser testuser 1754  5์›” 16 06:16 activate.ps1
-rw-rw-r-- 1 testuser testuser 1208  5์›” 16 06:16 activate_this.py
-rwxrwxr-x 1 testuser testuser  242  5์›” 16 06:16 pip
-rwxrwxr-x 1 testuser testuser  242  5์›” 16 06:16 pip-3.6
-rwxrwxr-x 1 testuser testuser  242  5์›” 16 06:16 pip3
-rwxrwxr-x 1 testuser testuser  242  5์›” 16 06:16 pip3.6
lrwxrwxrwx 1 testuser testuser   16  5์›” 16 06:16 python -> /usr/bin/python3
lrwxrwxrwx 1 testuser testuser    6  5์›” 16 06:16 python3 -> python
lrwxrwxrwx 1 testuser testuser    6  5์›” 16 06:16 python3.6 -> python
-rwxrwxr-x 1 testuser testuser  229  5์›” 16 06:16 wheel
-rwxrwxr-x 1 testuser testuser  229  5์›” 16 06:16 wheel-3.6
-rwxrwxr-x 1 testuser testuser  229  5์›” 16 06:16 wheel3
-rwxrwxr-x 1 testuser testuser  229  5์›” 16 06:16 wheel3.6

./testenv/lib:
ํ•ฉ๊ณ„ 0
drwxrwxr-x 3 testuser testuser 27  5์›” 16 06:16 python3.6

./testenv/lib64:
ํ•ฉ๊ณ„ 0
drwxrwxr-x 3 testuser testuser 27  5์›” 16 06:16 python3.6

 

3. ๊ฐ€์ƒํ™˜๊ฒฝ ํ™œ์„ฑํ™”/๋น„ํ™œ์„ฑํ™”

# ํ™œ์„ฑํ™”
[testuser@test-server-1 ~]$ source testenv/bin/activate
(testenv) [testuser@test-server-1 ~]$ 

# ๋น„ํ™œ์„ฑํ™”
(testenv) [testuser@test-server-1 ~]$ deactivate
[testuser@test-server-1 ~]$

 

4. ๊ฐ€์ƒํ™˜๊ฒฝ ์‚ญ์ œ

์‚ญ์ œ ์‹œ์—๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์‚ญ์ œํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

[testuser@test-server-1 ~]$ rm -r testenv