Skip to content Skip to sidebar Skip to footer

Getting Error Typeerror: 'module' Object Has No Attribute '__getitem__'

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 = 'postgre

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

deftest(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.fixturedefapp(request):

SQLALCHEMY_DATABASE_URI = 'postgresql://sanjeev:sanjeev@localhost:5432/app'

config.take(SQLALCHEMY_DATABASE_URI)
db.create_all()
deffin():
    db.session.remove()
    db.drop_all()
request.addfinalizer(fin)

deftest_foo(app):
    pass

config.py:

import os
from sqlalchemy.ext.declarative import declarative_base

global myvar

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

deftake(variable):
        global myvar
        myvar = variable

defshow():
        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$ 

Post a Comment for "Getting Error Typeerror: 'module' Object Has No Attribute '__getitem__'"