获取错误类型错误:“模块"对象没有属性“__getitem__" [英] Getting error TypeError: 'module' object has no attribute '__getitem__'

查看:49
本文介绍了获取错误类型错误:“模块"对象没有属性“__getitem__"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写基于 py.test 的测试用例.我的 test.py 是

!flask/bin/python导入pytest导入配置@pytest.fixture定义应用程序(请求):SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'配置[SQLALCHEMY_DATABASE_URI]db.create_all()定义鳍():db.session.remove()db.drop_all()request.addfinalizer(鳍)def test_foo(app):经过

我的 config.py 文件看起来像

导入操作系统从 sqlalchemy.ext.declarative 导入 declarative_baseBase = declarative_base()basedir = os.path.abspath(os.path.dirname(__file__))CSRF_ENABLED = 真SECRET_KEY = '你永远猜不到'SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')

但我收到错误 TypeError: 'module' object has no attribute 'getitem'

下面是错误日志跟踪

 设置 test_foo 时出错request = <SubRequest 'app' for <Function 'test_foo'>>@pytest.fixture定义应用程序(请求):SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'配置[SQLALCHEMY_DATABASE_URI]类型错误:模块"对象没有属性__getitem__"test.py:20: 类型错误

解决方案

导入模块 config.因此,您不能像列表一样访问它:

<预><代码>>>>导入数学>>>数学[0]回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:模块"对象没有属性__getitem__">>>

相反,如果你也想从 shell 运行它,你可以使用 sys.argv,或者将它作为函数传入:

sys.argv:

myfile.py:

导入系统打印 sys.argv[1:]

跑步:

bash-3.2$ python myfile.py Hello World!['你好,世界!']bash-3.2$

传入:

myfile.py:

def 测试(变量):打印变量

跑步:

<预><代码>>>>导入我的文件>>>我的文件[56]回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:模块"对象没有属性__getitem__">>>myfile.test(56)56>>>

您更新的文件test.py:

!flask/bin/python导入pytest导入配置@pytest.fixture定义应用程序(请求):SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'config.take(SQLALCHEMY_DATABASE_URI)db.create_all()定义鳍():db.session.remove()db.drop_all()request.addfinalizer(鳍)def test_foo(app):经过

config.py:

导入操作系统从 sqlalchemy.ext.declarative 导入 declarative_base全球myvardef take(变量):全球myvarmyvar = 变量print myvar #只是一个测试,看看它是否有效Base = declarative_base()basedir = os.path.abspath(os.path.dirname(__file__))CSRF_ENABLED = 真SECRET_KEY = '你永远猜不到'SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')

测试上述内容:

我的虚拟文件config.py:

全局 myvardef take(变量):全球myvarmyvar = 变量定义显示():全球myvar打印 myvar

我的虚拟文件test.py:

导入配置变量 = [6, 7, 8]config.take(变量)config.show()

运行: 这应该打印 [6, 7, 8]

bash-3.2$ python test.py[6, 7, 8]bash-3.2$

I am trying to write py.test based test cases. my test.py is

!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 

My config.py file looks like

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

But I am getting error TypeError: 'module' object has no attribute 'getitem'

below is error log trace

                  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

解决方案

The module config is imported. Therefore, you cannot access it like a list:

>>> import math
>>> math[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object has no attribute '__getitem__'
>>> 

Instead, you could use sys.argv if you want to run it from the shell too, or pass it in as a function:

sys.argv:

myfile.py:

import sys
print sys.argv[1:]

Running:

bash-3.2$ python myfile.py Hello World!
['Hello', 'World!']
bash-3.2$ 

Passing it in:

myfile.py:

def test(variable):
        print variable

Running:

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

Edit:

Your updated file test.py:

!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 

config.py:

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

Testing the above:

My dummy file config.py:

global myvar

def take(variable):
        global myvar
        myvar = variable

def show():
        global myvar
        print myvar

My dummy file test.py:

import config
variable = [6, 7, 8]

config.take(variable)
config.show()

Running: This should print [6, 7, 8]

bash-3.2$ python test.py
[6, 7, 8]
bash-3.2$ 

这篇关于获取错误类型错误:“模块"对象没有属性“__getitem__"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆