简体   繁体   中英

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).

Directory structure:

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

test-sample.py

import src.my_bot
import src.bot_token

def test
...

my_bot.py

import src.bot_token

client.run(bot_token.bot_token)

...

bot_token.py

bot_token = 'blahblahblah'

When I try to run python3 -m pytest , I get hit with the following error:

_________________________________________________ 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 . That name does not exist. Try using src.bot_token or maybe src.bot_token.bot_token on line 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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