pythonamazon-web-servicespycharmboto3boto

PyCharm: Why "Python Console" is not accessing ~\.aws\credentials file? How to set it within "Python Console"


I am developing AWS DynamoDb tables in Pycharm. For this I have created a virtual environment with Python 3.6 and installed required libraries like boto3. I have also set my AWS credentials using AWS CLI tool in ~/.aws/credentials file.

Problem is when I simply run the code, it works like a charm and is able to read the credentials file. However, when I select to run the code in "Python console", I get the error that credentials have expired. It appears to me that somehow "Python console" is unable to access the ~/.aws/credentials file and is looking somewhere else for credentials. Or boto3 is not accessing the credentials file from ~/.aws/credentials when I select code to run in python console.

Can someone guide me as how to set up credentials in Python console so that I can run the code interactively.

Thanks,


Solution

  • I struggled with the same problem using Pycharm in Intellij.

    Boto3 couldn't locate the credentials file and I didn't have a default profile set.

    > os.environ["AWS_SHARED_CREDENTIALS_FILE"]
    None
    > os.environ["AWS_DEFAULT_PROFILE"]
    None
    

    Solution, I set the variables explicitly

    > import boto3
    > import os
    > os.environ["AWS_SHARED_CREDENTIALS_FILE"] = "<full-path>/.aws/credentials"
    > os.environ["AWS_DEFAULT_PROFILE"] = "<profile-name>"
    
    > boto3.client('sts').get_caller_identity().get('Account')
    1234567890