poetry shell은 해당 프로젝트의 가상환경을 활성화하는 명령어이다.

그러나 poetry 버전이 2.0으로 업데이트 되면서

해당 명령어가 삭제되었다.

 

갑자기 없어진 명령어가 불편하게 느껴져 해결방법을 찾던 중,

shell 명령어를 다시 사용할 수 있는 plug-in 설치 방법을 찾았다.

poetry self add poetry-plugin-shell

 

poetry에 추가패키지를 설치하는 self명령어로 간단하게 plug-in을 설치할 수 있다.

해당 플러그인을 설치한 후 poetry shell 명령어를 실행하면,

 프로젝트/.venv/bin 경로에 있는 activate 파일을 실행하여 가상환경을 활성화한다.

 

가상환경 활성화하는 과정이 궁금해서 살펴본 activate 파일의 스크립트는 아래와 같다.

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly


if [ "${BASH_SOURCE-}" = "$0" ]; then
    echo "You must source this script: \$ source $0" >&2
    exit 33
fi

deactivate () {
    unset -f pydoc >/dev/null 2>&1 || true

    # reset old environment variables
    # ! [ -z ${VAR+_} ] returns true if VAR is declared at all
    if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then
        PATH="$_OLD_VIRTUAL_PATH"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi
    if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
        PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
        export PYTHONHOME
        unset _OLD_VIRTUAL_PYTHONHOME
    fi

    # The hash command must be called to get it to forget past
    # commands. Without forgetting past commands the $PATH changes
    # we made may not be respected
    hash -r 2>/dev/null

    if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
        PS1="$_OLD_VIRTUAL_PS1"
        export PS1
        unset _OLD_VIRTUAL_PS1
    fi

    unset VIRTUAL_ENV
    unset VIRTUAL_ENV_PROMPT
    if [ ! "${1-}" = "nondestructive" ] ; then
    # Self destruct!
        unset -f deactivate
    fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV=/home/cybot_RAG/.venv
if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then
    VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV")
fi
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/"bin":$PATH"
export PATH

if [ "x"cybot-rag-py3.11 != x ] ; then
    VIRTUAL_ENV_PROMPT=cybot-rag-py3.11
else
    VIRTUAL_ENV_PROMPT=$(basename "$VIRTUAL_ENV")
fi
export VIRTUAL_ENV_PROMPT

# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
    _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
    unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
    _OLD_VIRTUAL_PS1="${PS1-}"
    PS1="(${VIRTUAL_ENV_PROMPT}) ${PS1-}"
    export PS1
fi

# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true

pydoc () {
    python -m pydoc "$@"
}

# The hash command must be called to get it to forget past
# commands. Without forgetting past commands the $PATH changes
# we made may not be respected
hash -r 2>/dev/null || true

1. 기존 가상환경을 정리

2. 새로운 가상환경을 활성화

3. 쉘 프롬프트 표시 변경

 

스크립트 주요 내용은 위와 같은 것을 확인할 수 있었다.

 

참고: 

https://github.com/python-poetry/poetry/issues/9962

 

The command "shell" does not exist. on Poetry 2.0.0 · Issue #9962 · python-poetry/poetry

Description While running poetry shell in any of my repositories I am getting The command "shell" does not exist.. poetry run is working fine, which is even more confusing. Workarounds Downgrading ...

github.com

 

'Language > Python' 카테고리의 다른 글

[Python] 깊은 복사(deepcopy)  (0) 2025.02.12

클래스의 인스턴스 변수를,
특정 메서드에서 다른 파라미터값으로 초기화하기 위한 방법을 고민하였다.

해당 문제는 깊은 복사 방법으로 해결하려 한다.
의사코드로 구현하면 아래와 같다.

import copy

class TestClass:
	def __init__(self):
    	'''초기화할 때는 temperature를 0.3으로 설정'''
        self.llm_model = modelInit(
            name = os.getenv("MODEL_NAME"),
            url = os.getenv("MODEL_URL"),
            temperature = 0.3,
        )
    
    def create_Achain(self):
    	'''A체인을 생성할 때는, temperature를 0.0으로 설정'''
    	auto_complete_llm_model = copy.deepcopy(self.llm_model)
        auto_complete_llm_model.temperature = 0.0
        
        chain = prompt | auto_complete_llm_model | parser
        
        return chain


기존에 초기화했던 self.llm_model의 인스턴스 변수를,
create_Achain이란 메서드에서 호출할때에만 다른 값으로 초기화하여 사용하고 싶었다.

파이썬의 경우, copy모듈에 정의된 deepcopy()를 활용하여 쉽게 깊은복사를 사용할 수 있었다.

깊은 복사의 trade-off로 객체를 생성하고 메모리를 할당하는 과정에서 비용이 발생한다는 점에 유의해야할 것 같다.


참고:
https://kevinitcoding.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%ACPython-%EC%96%95%EC%9D%80-%EB%B3%B5%EC%82%ACShallow-copy%EC%99%80-%EA%B9%8A%EC%9D%80-%EB%B3%B5%EC%82%ACdeep-copy%EC%97%90-%EB%8C%80%ED%95%9C-%EC%99%84%EB%B2%BD-%EC%A0%95%EB%A6%AC

+ Recent posts