본문 바로가기

리눅스

파이썬 가상 환경을 구성하고 설정하는 방법(python virtual environment)

728x90

파이썬 가상 환경을 구성하고 설정하는 방법(python virtual environment)

virtualenv 설치

python -m pip install --user -U virtualenv
$ python -m pip install --user -U virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/ef/e0/1295d8a0b34f71a81fdf0f09c1ef658ae6d611240829c3c39fb2b6b80967/virtualenv-20.16.6-py3-none-any.whl (8.8MB)
     |████████████████████████████████| 8.8MB 787kB/s 
Collecting filelock<4,>=3.4.1
  Downloading https://files.pythonhosted.org/packages/94/b3/ff2845971788613e646e667043fdb5f128e2e540aefa09a3c55be8290d6d/filelock-3.8.0-py3-none-any.whl
Collecting distlib<1,>=0.3.6
  Downloading https://files.pythonhosted.org/packages/76/cb/6bbd2b10170ed991cf64e8c8b85e01f2fb38f95d1bc77617569e0b0b26ac/distlib-0.3.6-py2.py3-none-any.whl (468kB)
     |████████████████████████████████| 471kB 11.5MB/s 
Collecting platformdirs<3,>=2.4
  Downloading https://files.pythonhosted.org/packages/ed/22/967181c94c3a4063fe64e15331b4cb366bdd7dfbf46fcb8ad89650026fec/platformdirs-2.5.2-py3-none-any.whl
Installing collected packages: filelock, distlib, platformdirs, virtualenv
Successfully installed distlib-0.3.6 filelock-3.8.0 platformdirs-2.5.2 virtualenv-20.16.6

가상 환경 생성(vitualenv env)

virtualenv env
$ virtualenv env
created virtual environment CPython3.8.11.final.0-64 in 675ms
  creator CPython3Posix(dest=/home/tuser/ansible-spec/env, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/tuser/.local/share/virtualenv)
    added seed packages: pip==22.3, setuptools==65.5.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
$ tree env -L 2
env
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.nu
│   ├── activate.ps1
│   ├── activate_this.py
│   ├── ansible
│   ├── ansible-community
│   ├── ansible-config
│   ├── ansible-connection
│   ├── ansible-console
│   ├── ansible-doc
│   ├── ansible-galaxy
│   ├── ansible-inventory
│   ├── ansible-playbook
│   ├── ansible-pull
│   ├── ansible-test
│   ├── ansible-vault
│   ├── deactivate.nu
│   ├── pip
│   ├── pip-3.8
│   ├── pip3
│   ├── pip3.8
│   ├── python -> /opt/rh/rh-python38/root/usr/bin/python
│   ├── python3 -> python
│   ├── python3.8 -> python
│   ├── wheel
│   ├── wheel-3.8
│   ├── wheel3
│   └── wheel3.8
├── lib
│   └── python3.8
├── lib64
│   └── python3.8
└── pyvenv.cfg
728x90

 

or

python -m venv env
$ tree -L 1 env
env
├── bin
├── include
├── lib
├── lib64 -> lib
└── pyvenv.cfg

가상 환경 활성화(시작)

source env/bin/activate
$ source env/bin/activate
(env) tuser@serv-24:ansible-spec$
(env) tuser@serv-24:ansible-spec$ python --version
Python 3.8.11
(env) tuser@serv-24:ansible-spec$ python3 --version
Python 3.8.11
(env) tuser@serv-24:ansible-spec$ pip list
Package    Version
---------- -------
pip        19.3.1 
setuptools 41.6.0 
WARNING: You are using pip version 19.3.1; however, version 22.3.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

pip 패키지를 최신 버전으로 업데이트

pip install --upgrade pip
(env) tuser@serv-24:ansible-spec$ pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/09/bd/2410905c76ee14c62baf69e3f4aa780226c1bbfc9485731ad018e35b0cb5/pip-22.3.1-py3-none-any.whl (2.1MB)
     |████████████████████████████████| 2.1MB 10.3MB/s 
Installing collected packages: pip
  Found existing installation: pip 19.3.1
    Uninstalling pip-19.3.1:
      Successfully uninstalled pip-19.3.1
Successfully installed pip-22.3.1
(env) tuser@serv-24:ansible-spec$ pip --version
pip 22.3.1 from /home/tuser/test/env/lib64/python3.8/site-packages/pip (python 3.8)
(env) tuser@serv-24:ansible-spec$ which python
~/ansible-spec/env/bin/python

가상 환경 비활성화(종료)

deactivate

가상 환경(virtualenv) 삭제

rm -rf env

 

728x90