How do I fix the `botocore.exceptions.NoCredentialsError: Unable to locate credentials` error in Boto3?

I’m trying to create an S3 bucket using Boto3 in Python, but I keep running into this error:

botocore.exceptions.NoCredentialsError: Unable to locate credentials

Here’s a simplified version of my code:

import boto3
import uuid

s3 = boto3.resource('s3')
bucket_name = "python-sdk-sample-%s" % uuid.uuid4()
print("Creating new bucket with name:", bucket_name)
s3.create_bucket(Bucket=bucket_name)

I’ve already saved my credentials in:

C:\Users\myname\.aws\credentials

and I expected Boto3 to read them automatically. I also enabled debug logging:

boto3.set_stream_logger('botocore', level='DEBUG')

The log shows that Boto3 is checking all usual credential sources (environment variables, shared credentials file, config file, IAM roles, etc.) but still cannot find my credentials.

I’m looking for guidance on why botocore.exceptions.nocredentialserror: unable to locate credentials occurs in this situation and what the proper way is to make Boto3 recognize my credentials on Windows.

Any step-by-step advice or common pitfalls would be really helpful.

This usually happens if Boto3 can’t find your credentials in the expected locations. On Windows, make sure your file is at:

C:\Users\<username>\.aws\credentials

with this format:

[default]
aws_access_key_id = YOUR_KEY
aws_secret_access_key = YOUR_SECRET ```

I’ve found using environment variables helps, especially for scripts or Docker containers:

setx AWS_ACCESS_KEY_ID "YOUR_KEY"
setx AWS_SECRET_ACCESS_KEY "YOUR_SECRET"

Boto3 will pick these up automatically.

If you’re running this on an EC2 instance or AWS environment, Boto3 can use IAM roles instead of credentials files. In local setups, double-check your profile name in boto3.Session(profile_name=“default”) if you have multiple profiles.