ERROR: test_can_get_email_link_to_log_in (functional_tests.test_login.LoginTest)
----------------------------------------------------------------------
回溯(最近一次呼叫)。
檔案 ".../Django/python-tdd-book/functional_tests/test_login. py", line 36, in test_can_get_email_link_to_login
body = self.wait_for_email(test_email, SUBJECT)
檔案 ".../Django/python-tdd-book/functional_tests/test_login. py", line 72, in wait_for_email
inbox.pass_(os.environ['GMAIL_PASSWORD'])
檔案 "/opt/homebrew/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", 行 679, 在 __getitem__
raise KeyError(key) from None
KeyError。'GMAIL_PASSWORD'
我正在按照obeythetestinggoat的書來做,這本書非常棒,但是我無法通過這個錯誤,我在這里發現了另一個未解決的問題,與此完全一樣,但是,沒有解決,我試著去研究gmail的訪問檔案,例如 "允許不安全的應用程式",反復檢查我已經加載了環境變數,系統實際上在作業,即。我注意到這個州的python版本是系統的python3,但是當我從我的virtualenv中運行python3時,它顯示的是virtualenv版本,我甚至嘗試重新創建我的virtualenv,任何幫助都將是巨大的,我已經為這個錯誤花了一天時間。
from django.core import mail
from selenium.webdriver.common.keys import Keys
import re
import os
import time
import poplib
def wait_for_email(self, test_email, subject)。
if not self.staging_server:
email = mail.outbox[0]
self.assertIn(test_email, email.to)
self.assertEqual(email.subject, subject)
return email.body
email_id = None'pop.gmail.com')
try:
inbox.user(test_email)
inbox.pass_(os.environ['GMAIL_PASSWORD'] )
while time.time() - start < 60:
# get 10 newest messages.
count, _ = inbox.stat()
for i in reversed(range(max(1, count - 10), count 1)。
print('getting msg'/span>, i)
_, lines, __ = inbox.retr(i)
lines = [l.decode('utf8') for l in lines ]
print(lines)
if f'Subject: {subject}' in lines:
email_id = i
body = '
'.join(lines)
return body
time.sleep(10)
finally:
if email_id:
inbox.dele(email_id)
inbox.quit()
uj5u.com熱心網友回復:
如果os.environ['GMAIL_PASSWORD']引發一個KeyError,這意味著GMAIL_PASSWORD環境變數沒有在當前環境中設定。
仔細檢查你的shell環境變數。
env命令列出了所有的env變數。 檢查是否有錯別字,大寫/小寫等等。不太可能,但你也可以檢查你的 python 腳本 -- 是否有什么東西從環境中洗掉,用
del os.environ[...]?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318669.html
標籤:
上一篇:你怎么能測驗axios呢?
下一篇:在Django中用認證進行測驗
