Backend/Trouble Shooting

CLI 명령어가 잘못된 실행 파일을 참조하는 문제

sangwonYoon 2023. 12. 29. 20:23

실행 환경

  • OS: MacOS
  • Python: 3.10.13 버전
  • pyenv: 2.3.35 버전

 

문제 상황

pyenv 가상환경에서 streamlit 명령어를 실행하는 상황에서 아래와 같은 에러가 발생했다.

...
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: <https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates>

구글링해본 결과 protobuf 라이브러리의 호환성 문제이므로 protobuf를 다운그레이드하면 문제가 해결된다는 것을 알 수 있었다.

그러나, protobuf를 다운그레이드해도 문제가 해결되지 않았다.

 

문제 분석

그 이유는 streamlit 명령어가 사용하고 있는 가상환경의 streamlit을 가리키고 있는 것이 아니라, 전역에 설치된 streamlit 라이브러리를 가리키고 있었기 때문에 가상환경 내에서 protobuf의 버전을 아무리 바꿔도 소용이 없던 것이었다.

쉘에서 명령어를 실행할 때, 명령어의 실행 파일을 찾는 방법은 PATH 환경변수에 있는 모든 경로 하위에서 명령어를 찾는 것이다.

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/programs:/usr/programs

아래의 명령어를 실행하면

$ foo

아래의 순서로 실행 가능한 파일을 찾는다.

  • /usr/local/bin/foo
  • /usr/bin/foo
  • /bin/foo
  • /usr/local/programs/foo
  • /usr/programs/foo

실제로 내 가상 환경에서 PATH 환경변수를 출력해보면 다음과 같았다.

$ echo $PATH
/Users/sangwon/.pyenv/shims:/Users/sangwon/.pyenv/bin:/Library/Frameworks/Python.framework/Versions/3.10/bin: ...

which 명령어로 출력한 streamlit 명령어의 파일의 경로는 다음과 같았다.

$ which streamlit
/Library/Frameworks/Python.framework/Versions/3.10/bin/streamlit

내가 사용하고 싶었던 가상환경 내의 streamlit 파일 경로는 pip show를 통해 확인할 수 있다.

$ pip show streamlit
Name: streamlit
Version: 1.29.0
Summary: A faster way to build and share data apps
Home-page: <https://streamlit.io>
Author: Snowflake Inc
Author-email: hello@streamlit.io
License: Apache License 2.0
Location: /Users/sangwon/.pyenv/versions/3.10.13/envs/lablup_practice/lib/python3.10/site-packages
Requires: altair, blinker, cachetools, click, gitpython, importlib-metadata, numpy, packaging, pandas, pillow, protobuf, pyarrow, pydeck, python-dateutil, requests, rich, tenacity, toml, tornado, typing-extensions, tzlocal, validators
Required-by:

즉, /Users/sangwon/.pyenv/versions/3.10.13/envs/lablup_practice/lib/python3.10/site-packages/streamlit 파일을 읽어야 하는데, /Library/Frameworks/Python.framework/Versions/3.10/bin/streamlit 파일을 읽어서 문제였던 것이다.

 

문제 해결

PATH 환경변수를 수정하여 문제를 해결할 수 있었다.

 

오늘의 교훈

CLI 명령어에 문제가 발생하면 올바른 실행 파일을 참조하고 있는지 which 명령어를 통해 확인하자.