仅在 py.test 文件中,使用嵌套(单词?)导入 variable/function 失败并出现 NameError
Only in py.test file, usage of a nested(word?) imported variable/function fails with NameError
我有这样的 python 结构:
mymodule/
globalconfig.py # variables to set environment, etc
work.py # has: from mymodule.globalconfig import *
__init__.py
tests/
testspart1/
test_work.py # has: from mymodule.work import *
在 work.py 内部,一切正常,我可以访问我的全局配置变量和函数。
从 test_work.py 内部,我无法访问这些变量,即使我添加了第二个导入,
from mymodule.globalconfig import *
这是为什么?我想使用与我的模块中使用的语法相同的语法。
谢谢!
我正在使用 py2.7,为了获得漂亮的 rspec 风格的输出和详细的差异,
pytest --spec -vv
参考;
1.This 回答提醒我可以使用另一种导入格式。如果没有其他答案,我将 post 我的解决方法。 how to share a variable across modules for all tests in py.test
除了导入被测文件外,对我有用的导入语法是直接导入嵌套的 python 文件。
from mymodule.work import *
import mymodule.globalconfig as myconfigs
我认为这是名称冲突或导入循环问题,但我无法弄清楚问题出在哪里。我花了一些时间,所以我想确保 post 为未来的我和其他人提供解决方案。
我有这样的 python 结构:
mymodule/
globalconfig.py # variables to set environment, etc
work.py # has: from mymodule.globalconfig import *
__init__.py
tests/
testspart1/
test_work.py # has: from mymodule.work import *
在 work.py 内部,一切正常,我可以访问我的全局配置变量和函数。 从 test_work.py 内部,我无法访问这些变量,即使我添加了第二个导入,
from mymodule.globalconfig import *
这是为什么?我想使用与我的模块中使用的语法相同的语法。 谢谢!
我正在使用 py2.7,为了获得漂亮的 rspec 风格的输出和详细的差异,
pytest --spec -vv
参考; 1.This 回答提醒我可以使用另一种导入格式。如果没有其他答案,我将 post 我的解决方法。 how to share a variable across modules for all tests in py.test
除了导入被测文件外,对我有用的导入语法是直接导入嵌套的 python 文件。
from mymodule.work import *
import mymodule.globalconfig as myconfigs
我认为这是名称冲突或导入循环问题,但我无法弄清楚问题出在哪里。我花了一些时间,所以我想确保 post 为未来的我和其他人提供解决方案。