简体   繁体   English

获取NameError:名称'bot_token'未定义py.test

[英]Getting NameError: name 'bot_token' is not defined py.test

I am getting hit with a NameError: name 'bot_token' is not defined when I try to run pytest, even though I am importing the all the necessary files (or clearly maybe not).我遇到了 NameError: name 'bot_token' is not defined 当我尝试运行 pytest 时,即使我正在导入所有必要的文件(或者显然可能没有)。

Directory structure:目录结构:

bot/
-src/
--__init__.py
--my_bot.py
--bot_token.py
-tests/
--test-sample.py

test-sample.py测试样本.py

import src.my_bot
import src.bot_token

def test
...

my_bot.py我的机器人.py

import src.bot_token

client.run(bot_token.bot_token)

...

bot_token.py bot_token.py

bot_token = 'blahblahblah'

When I try to run python3 -m pytest , I get hit with the following error:当我尝试运行python3 -m pytest时,我遇到以下错误:

_________________________________________________ ERROR collecting tests/test_sample.py _________________________________________________
tests/test_sample.py:2: in <module>
    import src.my_bot
src/__init__.py:1: in <module>
    from .my_bot import my_bot
src/my_bot.py:278: in <module>
    client.run(bot_token.bot_token)
E   NameError: name 'bot_token' is not defined
======================================================== short test summary info ========================================================
ERROR tests/test_sample.py - NameError: name 'bot_token' is not defined

How do I manage this?我该如何管理?

You are importing src.bot_token and then referencing bot_token.bot_token .您正在导入src.bot_token然后引用bot_token.bot_token That name does not exist.那个名字不存在。 Try using src.bot_token or maybe src.bot_token.bot_token on line 3.尝试在第 3 行使用src.bot_tokensrc.bot_token.bot_token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM