Python 2.7 获取错误类型错误:“模块”对象没有属性“\uuuu getitem\uuuuuu”
我正在尝试编写基于py.test的测试用例。我的test.py是Python 2.7 获取错误类型错误:“模块”对象没有属性“\uuuu getitem\uuuuuu”,python-2.7,sqlalchemy,pytest,Python 2.7,Sqlalchemy,Pytest,我正在尝试编写基于py.test的测试用例。我的test.py是 !flask/bin/python import pytest import config @pytest.fixture def app(request): SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app' config[SQLALCHEMY_DATABASE_URI] db.create_all() def fin
!flask/bin/python
import pytest
import config
@pytest.fixture
def app(request):
SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'
config[SQLALCHEMY_DATABASE_URI]
db.create_all()
def fin():
db.session.remove()
db.drop_all()
request.addfinalizer(fin)
def test_foo(app):
pass
我的config.py文件看起来像
import os
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
basedir = os.path.abspath(os.path.dirname(__file__))
CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'
SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
但是我得到了错误TypeError:“module”对象没有属性“getitem”
下面是错误日志跟踪
ERROR at setup of test_foo
request = <SubRequest 'app' for <Function 'test_foo'>>
@pytest.fixture
def app(request):
SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'
config[SQLALCHEMY_DATABASE_URI]
TypeError: 'module' object has no attribute '__getitem__'
test.py:20: TypeError
模块配置已导入。因此,您不能像访问列表一样访问它:
>>> import math
>>> math[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object has no attribute '__getitem__'
>>>
运行:
传递:
myfile.py:
运行:
config.py:
测试上述各项:
我的虚拟文件config.py:
我的虚拟文件test.py:
运行:应该打印[6,7,8]
模块配置已导入。因此,您不能像访问列表一样访问它:
>>> import math
>>> math[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object has no attribute '__getitem__'
>>>
运行:
传递:
myfile.py:
运行:
config.py:
测试上述各项:
我的虚拟文件config.py:
我的虚拟文件test.py:
运行:应该打印[6,7,8]
你能分享确切的堆栈跟踪吗?它可能是指代码中的特定表达式和行。添加了日志跟踪。我正在尝试使用py.test编写安装和拆卸方法。您可以共享确切的堆栈跟踪吗?它可能是指代码中的特定表达式和行。添加了日志跟踪。我正在尝试使用py.test编写安装和拆卸方法。当您在myfile.py中传递它时,它再次显示错误。不是吗?我这样做是故意的,只是想告诉你不能调用myfile[56]。我希望这不会让你感到困惑,但是把它传递到工作中:对不起,我没有得到它。您能告诉我如何使用config.py和test.py文件通过它吗。我想使用py.test编写安装和拆卸函数。我正在使用py.test编写测试用例。我正在使用命令flask/bin/py.test.py来运行测试。您的解决方案可能适用于python,但我在py.test中遇到了相同的错误。当您在myfile.py中传递它时,它再次显示错误。不是吗?我这样做是故意的,只是想告诉你不能调用myfile[56]。我希望这不会让你感到困惑,但是把它传递到工作中:对不起,我没有得到它。您能告诉我如何使用config.py和test.py文件通过它吗。我想使用py.test编写安装和拆卸函数。我正在使用py.test编写测试用例。我正在使用命令flask/bin/py.test.py来运行测试。您的解决方案可能适用于python,但我在py.test中遇到了相同的错误。
bash-3.2$ python myfile.py Hello World!
['Hello', 'World!']
bash-3.2$
def test(variable):
print variable
>>> import myfile
>>> myfile[56]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object has no attribute '__getitem__'
>>> myfile.test(56)
56
>>>
!flask/bin/python
import pytest
import config
@pytest.fixture
def app(request):
SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'
config.take(SQLALCHEMY_DATABASE_URI)
db.create_all()
def fin():
db.session.remove()
db.drop_all()
request.addfinalizer(fin)
def test_foo(app):
pass
import os
from sqlalchemy.ext.declarative import declarative_base
global myvar
def take(variable):
global myvar
myvar = variable
print myvar #Just a test to see if it works
Base = declarative_base()
basedir = os.path.abspath(os.path.dirname(__file__))
CSRF_ENABLED = True
SECRET_KEY = 'you-will-never-guess'
SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
global myvar
def take(variable):
global myvar
myvar = variable
def show():
global myvar
print myvar
import config
variable = [6, 7, 8]
config.take(variable)
config.show()
bash-3.2$ python test.py
[6, 7, 8]
bash-3.2$