본문 바로가기

Python

(89)
[python] 파이썬에서 전역 변수 사용하기 - global 파이썬에서 전역변수를 사용하다가 예상대로 코드가 실행되지 않는다는 것을 발견했다. 전역변수 사용의 잘못된 코드 # 전역 변수 val = False def change_val(): # 아래 val은 함수내의 '지역변수'다. val = True change_val() print(val) >> False val이라는 전역 변수를 change_val 이라는 함수내에서 변경하려고 했으나 변경되지 않고 False 값이 그대로 출력되었다. 이는 C언어의 문법을 기반으로 생각했기 때문에 함수 내에서 변수값이 변경될거라 생각했다. 하지만 파이썬 함수 내에서 전역 변수를 수정하려면 global 키워드를 통해서 전역변수임을 명시해주어야 한다. 그렇지 않으면 그저 함수내에 지역 변수로 인식될 것이다. 전역변수를 사용할래요 -..
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. 문제 해결 방법 bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library? 파이썬에서 beautifulsoup4를 제대로 설치했다고 생각했는데, 위와 같은 에러가 뜰 수 있다. 이떄는 아래와 같이 lxml 패키지도 같이 설치해주면 된다. # pip install lxml Collecting lxml Cache entry deserialization failed, entry ignored Downloading https://files.pythonhosted.org/packages/fd/9c/d0bbb2ac23561738629cf6a04c660f7217..
[python] pip와 python 버전이 다를때 pip 설치하기 파이썬에서 pip 를 이용해 requests 모듈을 설치했는데, 정작 파이썬 스크립트를 돌리니 requests 모듈을 찾을 수 없다. $ pip install requests Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (2.26.0) Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.7/dist-packages (from requests) (2.0.9) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-pac..
[python] selenium에서 Message: unknown error: session deleted because of page crash 에러 발생 파이썬에서 Selenium을 통해 데이터를 가져오려고 하니 아래와 같이 에러가 났다. # python test.py Traceback (most recent call last): File "test.py", line 12, in driver.get("https://google.com") File "/root/test/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get self.execute(Command.GET, {'url': url}) File "/root/test/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py"..
[python] replace() argument 2 must be str, not int 에러 해결 언제부턴가 내가 만든 컴포넌트에서 아래와 같은 메시지를 간헐적으로 뱉기 시작했다. Traceback (most recent call last): File "", line 1, in TypeError: replace() argument 2 must be str, not int 일단 에러메시지를 보면 원인을 명확히 알 수 있다. replace라는 함수 내부에 들어가는 2개의 인자는 무조건 str 형이어야 한다. 그런데 어떤 인자값 하나가 int 형으로 들어가서 생긴 문제이다. >>> str = "REP hi lim co" >>> >>> rep_str = 1 >>> >>> str.replace("REP", rep_str) Traceback (most recent call last): File "", line ..