raise NoRegionError() - 您必须指定一个区域

时间:2016-12-12 23:44:58

标签: python python-2.7 amazon-dynamodb

我正在创建一个类来处理DynamoDB数据库的操作。它将是一个只有一个表的简单数据库。

我以NoRegionError()身份运行脚本时收到python dynamo.db。但是,当我只是在jupyter notebookqtconsole环境中调用该类或者在python shell中单独运行脚本中的每个命令时,我都没有得到这样的错误。

我已将我的凭据添加到本地.aws文件夹中,所以我不确定为什么会出现此错误以及如何调试它。这是我正在上课的课程:

import boto3

class Dynamo(object):

    def __init__(self, **kwargs):

        self._db_api = 'dynamodb'
        self._session = None
        self._client = None
        self._database = None
        self._region_name = kwargs.get('region_name', 'us-west-2')
        self._endpoint_url = kwargs.get('endpoint_url', 'http://localhost:8000')

        self.cached_table = {}
        self.table = None

    def connect(self):
        self._session = boto3.Session()
        print(self._region_name)
        print(self._db_api)

        if self._session:
            self._database = self._session.resource(self._db_api)


def main():

    dynamo = Dynamo()
    dynamo.connect()

    return dynamo

if __name__ == '__main__':
    dynodb = main()

这就是我得到的错误:

Traceback (most recent call last):
  File "test.py", line 39, in <module>
    dynodb = main()
  File "test.py", line 34, in main
    dynamo.connect()
  File "test.py", line 28, in connect
    self._database = self._session.resource(self._db_api)
  File "python2.7/site-packages/boto3/session.py", line 389, in resource
    aws_session_token=aws_session_token, config=config)
  File "python2.7/site-packages/boto3/session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
  File "python2.7/site-packages/botocore/session.py", line 824, in create_client
client_config=config, api_version=api_version)
  File "python2.7/site-packages/botocore/client.py", line 69, in create_client
verify, credentials, scoped_config, client_config, endpoint_bridge)
  File "python2.7/site-packages/botocore/client.py", line 222, in _get_client_args
verify, credentials, scoped_config, client_config, endpoint_bridge)
  File "python2.7/site-packages/botocore/args.py", line 44, in get_client_args
endpoint_url, is_secure, scoped_config)
  File "python2.7/site-packages/botocore/args.py", line 101, in compute_client_args
service_name, region_name, endpoint_url, is_secure)
  File "python2.7/site-packages/botocore/client.py", line 295, in resolve
service_name, region_name)
  File "python2.7/site-packages/botocore/regions.py", line 122, in construct_endpoint
partition, service_name, region_name)
  File "python2.7/site-packages/botocore/regions.py", line 135, in _endpoint_for_partition
raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.

3 个答案:

答案 0 :(得分:0)

你应该create a DynamoDB client使用boto3模板:

boto3.client('dynamodb', region_name='us-west-2', endpoint_url='http://localhost:8000')

另见this question and answer

答案 1 :(得分:0)

可能有多个原因导致此问题。最可能的问题是运行Python程序的用户没有执行.aws目录的权限。

以运行Python程序的用户身份运行以下两个命令:

cd ~
ls -lhd .aws

运行该程序的用户需要在此目录上具有“ x”功能。您可以设置.aws目录的权限,以确保用户具有执行权限。常见且简单的方法是使用此代码:

chmod u+x .aws

另一种解决方案是重新安装和重新配置AWS CLI。有关解决此问题的更多方法,请参阅this link

答案 2 :(得分:0)

from openpyxl import load_workbook

from openpyxl import load_workbook

wb = load_workbook('Stocks.xlsx')

ws = wb["IBM"]

for row in ws.iter_rows():
    for cell in row:
        if cell.value == 2010:
            print(ws.cell)

这可能对您有帮助。