'raise NoRegionError() - You must specify a region

I am making a class to handle operations for a DynamoDB database. It is going to be simple database with only one table.

I am getting a NoRegionError() when running my script as python dynamo.db. However, when I just call the class in a jupyter notebook or qtconsole environment or run each of the commands in the script separately in a python shell, I get no such error.

I have added my credentials to the local .aws folder so I'm not sure why this error is occurring and how to debug it. Here is the class I'm working on:

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

And this is the error I'm getting:

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.


Solution 1:[1]

You should create a DynamoDB client using boto3 templates:

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

Also see this question and answer.

Solution 2:[2]

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

This will may helpful for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 Hushen