Getting error TypeError: 'module' object has no attribute '__getitem__'

一笑奈何 提交于 2019-12-11 10:03:24

问题


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

回答1:


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$ 


来源:https://stackoverflow.com/questions/23400092/getting-error-typeerror-module-object-has-no-attribute-getitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!