pipenv 설치와 사용(jupyterlab)
pipenv = pip + virtualenv : 두 개념을 합친 것임
출처
- Pipenv & Virtual Environments
- Virtual Environments for Data Science: Running Python and Jupyter with Pipenv
- pipenv, virtualenv, pyenv 비교
준비 : python3 와 pip
$ python3 --version
Python 3.8.10
$ sudo apt install python3-pip
$ pip --version
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
jupyter lab 을 사용하는 경우에만 선택(권장)
$ pip install jupyterlab
pipenv 설치
$ pip install --user pipenv
$ pip install --user --upgrade pipenv
This does a user installation
- to prevent breaking any system-wide packages
명령경로 확인 및 추가
$ python3 -m site --user-base
/home/**/.local
$ PATH=$PATH:/home/**/.local/bin
Pipenv manages dependencies on a per-project basis.
- Pipenv is designed to be used by non-privileged OS users.
- Running Pipenv as root or with sudo (or Admin on Windows) is highly discouraged
- That might lead to unintend breakage of your OS.
가상환경(mypipenv) 경로에 가서 패키지(예: requests) 설치
$ mkdir mypipenv
$ cd mypipenv
$ pipenv install requests
Pipenv 사용법 : python code 실행법
-
예를 들어 코드 파일명이 mypythoncode.py 이라면
$ pipenv run python3 mypythoncode.py
Your IP is **
- 없다면 예제로 다음과 같이 하나 만들어 실습
$ vi mypythoncode.py
- 명령실행 결과로 공란 나오면 다음 내용 복사-붙여넣기
import requests response = requests.get('https://httpbin.org/ip') print('Your IP is {0}'.format(response.json()['origin']))
- 공란에 직접 입력하려면 자판
a
눌러 편집모드 진입 - 저장하고 나오려면 esc 눌러 명령모드 진입해 다음 입력
:wq
- 코드 실행 및 결과 확인
$ pipenv run python3 mypythoncode.py
Your IP is **
- 패키지 의존성 관리 확인법
$ pipenv graph
- 실행 결과로 각 패키지별로 의존성 정보 나열됨
Jupyterlab 연결 사용법(선택)(권장)
1. 먼저 ipykernel 패키지 설치
$ cd pipenv_test
$ pipenv install ipykernel
2. 가상환경 작동 후 커널 연결
$ pipenv shell
$ python -m ipykernel install --user --display-name A --name B
- 가상환경이 사용할 커널의 이름과 표시명 결정
- 특별한 취향이 없다면 A, B 모두 가상환경 명칭(mypipenv) 사용
3. 이후로는 Jupyter 실행 가능
- 취향대로 Jupyter notebook 또는 Jupyter lab 선택 사용
$ jupyter lab
-
초기 화면에 보이는 커널명칭(예: mypipenv) 보고 선택
-
jupyterlab 종료는 ctrl + C
- pipenv 가상환경 종료는
$ eixt
Virtualenv mapping caveat
Pipenv automatically maps projects
- to their specific virtualenvs.
The virtualenv is stored globally
- with the name of the project’s root directory
- plus the hash of the full path to the project’s root
- (e.g., my_project-a3de50).
If you change your project’s path,
- you break such a default mapping and
- pipenv will no longer be able
- to find and to use the project’s virtualenv.
You might want to set
export PIPENV_VENV_IN_PROJECT=1
- in your .bashrc/.zshrc (or any shell configuration file)
- for creating the virtualenv inside your project’s directory,
- avoiding problems with subsequent path changes.