728x90

.env 파일을 이용해 환경 변수를 설정하는 방식이 널리 사용되고 있습니다.
개인적으로는 dotenv 패키지를 이용해서 .env 파일을 사용중인데요
오랜만에 프로젝트 환경을 구성 하다보니 dotenv 패키지가 없더군요.

그런데!

pip install dotenv를 아무리 해도 설치되지 않는 당혹스러움을 마딱드렸습니다. 
패키지가 없어진건가? 사내 네트워크에서 막힌건가? 온갖 생각을 하다 찾아보니...
dotenv의 패키지 이름은 python-dotenv 였습니다 ㅠㅠ 
오랜만에 만난 그 이름... 어쩐지 익숙합니다.

pip install python-dotenv

당황하지 말고, python-dotenv를 설치하십시오, 닝겐!

728x90
728x90

파이썬을 사용하여 XML을 다룰때 BeautifulSoup을 많이 사용합니다.
새로 환경을 구성하여 BeautifulSoup을 이용하는데
이전에 보지 못했던 에러가 발생했습니다. 

강의 예제 코드 돌리다만난 에러라니...

 

핵심 에러 메세지는 다음과 같습니다.
BeautifulSoup 사용시 지정한 Feature가 없다는 내용입니다. 

FeatureNotFound: Couldn't find a tree builder with the features you requested: xml. Do you need to install a parser library?

 

lxml 패키지 설치하기

`xml`이 문제라고 하는데 정확히 어떤 라이브러리를 설치해야 하는지 알려주지는 않는군요.
구글을 통해 검색을 해보니 `lxml` 라이브러리의 설치가 필요한 것 같습니다. 
Jupyter 노트북을 사용중이라 아래와 같이 설치를 진행해 봤습니다.

!pip 로 주피터 노트북에서 직접 패키지를 설치해 봅니다

설치가 되었으니 이제 잘 되겠지...했으나...
계속 패키지가 없다고 에러가 나옵니다.
난감하네요.

하아... 계속 에러가...

 

그래서 조금 더 검색을 해보니
주피터 노트북을 재기동 하라는 이야기가 있습니다. 
구동중인 주피터를 중지하고 다시 실행했습니다.

Shutdown this notebook server (y/[n])? y
[C 11:01:20.436 NotebookApp] Shutdown confirmed
[I 11:01:20.439 NotebookApp] Shutting down 3 kernels
[I 11:01:20.443 NotebookApp] Kernel shutdown: 7a48be4a-c426-427b-b974-e8e26fccc994
[I 11:01:20.443 NotebookApp] Kernel shutdown: 45325d5b-9d8f-4f43-956f-d2b030475625
[I 11:01:20.443 NotebookApp] Kernel shutdown: d1b14c79-3741-4a14-9d0c-a5fe03a65532
[I 11:01:20.567 NotebookApp] Starting buffering for 45325d5b-9d8f-4f43-956f-d2b030475625:2d7ed833d3fe445c8b3ad49096ce9deb
[I 11:01:20.571 NotebookApp] Kernel shutdown: 45325d5b-9d8f-4f43-956f-d2b030475625
...
...
[I 11:01:20.967 NotebookApp] Shutting down 0 terminals
%
% jupyter notebook
[I 11:01:28.154 NotebookApp] Serving notebooks from local directory: /Users/nopd/dev
[I 11:01:28.154 NotebookApp] Jupyter Notebook 6.4.8 is running at:
[I 11:01:28.154 NotebookApp] http://localhost:8888/?token=731a38a75b038a956951174a7aa6da6d75acd13fe855ebd6
[I 11:01:28.154 NotebookApp]  or http://127.0.0.1:8888/?token=731a38a75b038a956951174a7aa6da6d75acd13fe855ebd6
[I 11:01:28.154 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 11:01:28.157 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///Users/nopd/Library/Jupyter/runtime/nbserver-45577-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=731a38a75b038a956951174a7aa6da6d75acd13fe855ebd6
     or http://127.0.0.1:8888/?token=731a38a75b038a956951174a7aa6da6d75acd13fe855ebd6

다시 파일을 열어 코드를 실행해보니 이제 잘 됩니다. 

주피터 재기동후 성공!

 

요약

0. 에러를 만난다
1. 에러를 잘 보고 필요한 패키지를 찾는다 : lxml
2. 주피터 노트북을 재기동한다
3. 계속 파이썬 코드를 잘 짠다

728x90
728x90

파이썬 장고가 제공하는 단위 테스트는 코드 배포전에 에러를 검출해 볼 수 있는 방법으로 무척 유용합니다. 젠킨스 빌드시에도 활용하여 자칫 로컬 환경과 원격 빌드/운영 환경의 차이에도 대응할 수 있어 장애를 막는 지름길이 되기도 합니다.

파이썬 장고의 단위 테스트를 구성해서 잘 사용하던 중, 갑자기 (원인은 알 수 없습니다 ㅜㅜ) TransactionManagementError와 함께 테스트가 지속적으로 실패하기 시작하여 열심히 구글링하고 삽질한 내용을 간략히 정리해 봅니다. 


장고 단위 테스트를 사용하기 위해서는 아래와 같은 코드를 만들게 됩니다. 참고로 코드는 장고 공식 문서에서 가져왔습니다. 이때, 실제 테스트를 수행하는 클래스는 매개변수로 TestCase 클래스의 인스턴스를 받게 됩니다. 아래의 코드에서는 TestCase 클래스를 가져왔네요.

import unittest
from django.test import Client

class SimpleTest(unittest.TestCase):
    def setUp(self):
        # Every test needs a client.
        self.client = Client()

    def test_details(self):
        # Issue a GET request.
        response = self.client.get('/customer/details/')

        # Check that the response is 200 OK.
        self.assertEqual(response.status_code, 200)

        # Check that the rendered context contains 5 customers.
        self.assertEqual(len(response.context['customers']), 5)

 

코드를 보면 실제 테스트 함수들이 클래스 내에 기술이 되어 있습니다. 제 경우 문제는 테스트 함수들이 데이터베이스에 대한 질의를 수행하는 코드를 호출하면서 발생했습니다. 일반적인 TestCase의 서브 클래스로는 데이터베이스 관련 코드를 수행하고 테스트 결과를 돌려주는데 문제가 있었습니다. 테스트가 수행되면서 발생한 에러는 아래와 같습니다.

raise TransactionManagementError(
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

 

이 에러 메세지가 참 얄미운게 내용이 좀 뭉뚱그려져 있습니다. 굉장히 명확한 에러이기 때문에 TestCase 클래스를 트랜잭션을 지원하는 것으로 변경하라고 하면 되었을 것을 저렇게 표현하고 있어서 애를 먹게 만들더군요. 이 문제에 대한 첫번째 접근은 장고 공식문서에서 가이드하는 TransactionTestCase 클래스의 사용입니다. 

https://docs.djangoproject.com/en/3.2/topics/testing/tools/#django.test.TransactionTestCase

 

제 경우는 장고 REST Framework 를 사용하고 있던 관계로 이쪽에서 필요한 클래스의 변경 작업을 진행했습니다. 기존에 에러가 발생하던 테스트 코드에서는 REST Framework가 제공하는 test 패키지의 APITestCase 클래스를 쓰고 있었습니다. 이 클래스를 APITransactionTestCase로 변경하니 에러가 말끔히 사라지고 테스트도 정상적으로 수행되기 시작했습니다.

# 기존
from rest_framework.test import APITestCase

# 변경
from rest_framework.test import APITransactionTestCase

 


TransactionManagementError로 검색을 하다보면 여러가지 문제 상황과 이에 대한 대처법들이 소개되고 있습니다. 가령 1) 테스트 테이블에 데이터를 피딩하는 과정에 문제였고 이를 수정하여 해결 했다는 분도 계셨고, 2) 테스트가 수행하는 코드 내에 try~except 구문이 사용되는 경우 transaction.atomic() 을 이용해서 코드를 변경해야 된다는 글도 있었습니다.

다양한 트러블슈팅 사례와 방법이 있는 것을 보면 에러 메세지가 조금 더 친절하게 출력되면서 원인을 빠르게 확인할 수 있었으면 어땠을까 하는 생각이 듭니다. 그래도 트러블슈팅을 하면서 또 파이썬에 대해, 장고에 대해 조금 더 알 수 있었기에 의미 없는 시간은 아니었던 것 같습니다!

파이썬과 관련하여 최근에 겪었던 다른 이슈들과 해결 방법은 아래의 글을 참고해 보시기 바랍니다!

 

M1 Apple silicon 맥북에서 파이썬 cryptography 패키지 설치가 안되는 문제 / python cryptography package installa

M1 silicon 맥북에서는 안되는게 참 많습니다. 새로운 CPU 아키텍쳐라서 여기저기서 패키지들이 오동작하거나 설치가 안되는 문제들이 많이 발생합니다. 오늘의 주인공인 파이썬 cryptography 패키지

ondemand.tistory.com

 

 

애플실리콘 M1 맥북에서 파이썬 magic 패키지가 로딩되지 않는 현상

애플실리콘 기반의 맥북이 등장하면서 세상이 참 행복해졌습니다. 베터리도 오래가고 이륙하지도 않고... 이렇게 쾌적한 맥 운영체제는 간만에 느껴보는 즐거움이었습니다. 하지만 의외의 장소

ondemand.tistory.com

 

 

PIP를 통한 mysqlclient 라이브러리 설치 에러 (mysql_config not found)

새로운 개발 장비가 생기면 이전에 사용하던 장비의 환경과 동일하게 준비하는 것이 은근 번거롭습니다. 한동안 손대지 않고 있던 파이썬 Python 장고 Django 프레임웍으로 개발된 코드를 새 장비

ondemand.tistory.com

 

728x90
728x90

M1 silicon 맥북에서는 안되는게 참 많습니다. 새로운 CPU 아키텍쳐라서 여기저기서 패키지들이 오동작하거나 설치가 안되는 문제들이 많이 발생합니다. 오늘의 주인공인 파이썬 cryptography 패키지도 마찬가지입니다. 그나마 이 녀석은 에러가 나름 명확한 친구임에도 문제를 해결하기 위해 적지 않은 시간을 쏟아야 했습니다.

우선 에러 메세지 보고 가시겠습니다. :-) 이 에러 메세지는 pip install -r requirements.txt 를 수행하는 동안 발생헀고 requirements.txt 에는 cryptography가 기술되어 있었습니다. (드라마 24시에서 Following takes place... 하는 느낌이네요)

Building wheels for collected packages: cryptography
  Building wheel for cryptography (PEP 517) ... error
  ERROR: Command errored out with exit status 1:
...
...
 ...
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -I/Users/nopd/project/venv/include -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -c build/temp.macosx-10.14.6-arm64-3.8/_openssl.c -o build/temp.macosx-10.14.6-arm64-3.8/build/temp.macosx-10.14.6-arm64-3.8/_openssl.o -Wconversion -Wno-error=sign-conversion
  build/temp.macosx-10.14.6-arm64-3.8/_openssl.c:575:10: fatal error: 'openssl/opensslv.h' file not found
  #include <openssl/opensslv.h>
           ^~~~~~~~~~~~~~~~~~~~
  1 error generated.
  
      =============================DEBUG ASSISTANCE=============================
      If you are seeing a compilation error please try the following steps to
      successfully install cryptography:
      1) Upgrade to the latest pip and try again. This will fix errors for most
         users. See: https://pip.pypa.io/en/stable/installing/#upgrading-pip
      2) Read https://cryptography.io/en/latest/installation.html for specific
         instructions for your platform.
      3) Check our frequently asked questions for more information:
         https://cryptography.io/en/latest/faq.html
      =============================DEBUG ASSISTANCE=============================
  
  error: command 'clang' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for cryptography
Failed to build cryptography
ERROR: Could not build wheels for cryptography which use PEP 517 and cannot be installed directly

 

화려한 에러 메세지이지만 결론은 설치가 잘 안되었다 입니다. DEBUG ASSISTANCE의 내용은 평이합니다. PIP가 구버전이라 그럴 수 있으니 업데이터 해보고 cryptography 설치 가이드 문서를 읽어 본 다음, faq에 단서가 있을지도 모르니 찾아보라는 내용입니다. TL;DR 하자면 아래의 한줄이 모든 것을 이야기 해주고 있었습니다.

build/temp.macosx-10.14.6-arm64-3.8/_openssl.c:575:10: fatal error: 'openssl/opensslv.h' file not found

 

Trouble Shoot #1 - Install separate openssl with brew

구글 검색을 통해 살펴봤던 문서를 종합해보면 M1 silicon 기종에 선탑재되어 나오는 openssl이 cryptography 빌드에 필요한 버전과 상이하다고 합니다. 실제로 M1 silicon 탑재 기종에서 openssl version 명령으로 버전을 확인해보면 LibreSSL 이 설치되어 있는 것을 알 수 있습니다. 아래와 같이 brew 를 이용해 openssl 을 별도로 설치해 보도록 하겠습니다. 공식 가이드에서 안내하는 1.1 버전을 지정해서 설치 진행했습니다. 

cryptography 설치 가이드의 안내  (https://cryptography.io/en/latest/installation/)

% openssl version
LibreSSL 2.8.3
% brew install openssl@1.1 rust
Updating Homebrew...

 

Trouble Shoot #2 - Uninstall cffi then install cryptography again

별도의 openssl 설치와 혹시 몰라서 rust(cryptography는 rust를 사용합니다)를 설치하는 작업이 끝나면 이제 파이썬 패키지중에서 C언어를 호출할 때 사용되는 cffi 도 삭제후에 다시 설치해 주도록 하겠습니다. 아래의 명령을 순서대로 터미널에서 입력하면 cryptography 가 설치됩니다. 제 경우 cryptography를 3.3.1 버전으로 지정해서 사용할 일이 있어서 버전을 지정했습니다만 그렇지 않은 경우에는 버전 정보를 빼고 설치하면 되겠습니다. 

% pip uninstall cffi
% LDFLAGS=-L$(brew --prefix libffi)/lib CFLAGS=-I$(brew --prefix libffi)/include pip install cffi --no-binary :all:
% LDFLAGS="-L$(brew --prefix openssl@1.1)/lib" CFLAGS="-I$(brew --prefix openssl@1.1)/include" pip install cryptography==3.3.1

 

문제가 해결되었습니다. 비슷한 이슈를 겪는 분들은 정리된 내용대로 패키지 삭제, 재설치를 진행해 보시기 바랍니다. 영어로 된 자료도 흥미롭게 읽을 수 있다면 보다 깊은 내용과 관련 링크가 공유된 stackoverflow 의 아래 스레드를 읽어 보시면 도움이 되겠습니다!

 

Installing Cryptography on an Apple Silicon M1 Mac

Help! I'm trying to install cryptography on my m1. I know I can run terminal in rosetta mode, but I'm wondering if there is a way not to do that. Output: ERROR: Command errored out with exit st...

stackoverflow.com

 

Hope this helpful to someone who experiencing similar issue.

728x90

+ Recent posts