Django

[Pytest] - django-dotenv 와 pytest-dotenv 오류

r잡초처럼 2022. 12. 19. 17:47

1. 문제 상황

테스트 코드를 짜 보려고 pytest를 설치했다. 근데 해당 테스트 코드를 실행하는 도중 예기치 못한 문제를 맞닥뜨렸다. django-dotenvpytest-dotenv의 naming conflict가 발생했다.

해당 글에서 댓글에는 이렇게 적혀있다.

Warning to Django Users: If you're using django-dotenv to call dotenv.read_dotenv() from your manage.py module, this library will break it. This library requires python-dotenv, which also uses the 'dotenv' namespace, and you'll end up a ModuleNotFoundError when manage.py is run.
 
– GlenVaughan

django-dotenv에 이슈로 올라왔는데 해당 문제가 해결이 안 됐나 보다 - issue

 

Why? · Issue #42 · jpadilla/django-dotenv

I looked at the source code for this project -- none of it appears to be django specific. Why is it branded with django?

github.com

2. 해결

난 그냥 django-dotenv를 삭제하고 코드를 수정했다.

# manage.py


# 기존 코드
...
if __name__ == '__main__':
    dotenv.read_dotenv()

# 변경 후 코드
...
if __name__ == '__main__':
    dotenv.load_dotenv()