top of page

Integrating AWS Environment Variables in PyCharm: A Comprehensive Guide


To use environment variables like `AWS_SECRET_ACCESS_KEY`, `AWS_ACCESS_KEY_ID`, and `AWS_SESSION_TOKEN` in PyCharm, you need to set them up within the PyCharm environment. Here's how you can do it:

1. Setting Environment Variables in a Run Configuration:

- Open your project in PyCharm.

- Go to the 'Run' menu and select 'Edit Configurations.'

- In the 'Run/Debug Configurations' window, select your script.

- Find the 'Environment variables' field. You can either manually enter your variables in the format `KEY=VALUE` or click the browse button (three dots) to open the 'Environment Variables' dialog where you can add new variables.

- Add your `AWS_SECRET_ACCESS_KEY`, `AWS_ACCESS_KEY_ID`, and `AWS_SESSION_TOKEN` variables here.

- Apply the changes and close the dialog.

2. Loading Environment Variables from a File:

- Instead of manually adding environment variables in each run configuration, you can create a file (e.g., `.env`) in your project directory with all the necessary environment variables.

- You can then use a package like `python-dotenv` to load these variables in your script.

- Install `python-dotenv` using PyCharm's terminal or external terminal: `pip install python-dotenv`.

- At the beginning of your script, add:

   from dotenv import load_dotenv

     load_dotenv()  # This loads the variables from .env

3. Global Environment Variables in PyCharm:

- PyCharm also allows setting global environment variables which will be available for all projects.

- Go to 'File' -> 'Settings' (or 'PyCharm' -> 'Preferences' on macOS).

- Navigate to 'Build, Execution, Deployment' -> 'Console' -> 'Python Console'.

- Here, you can add environment variables that will be used every time you run a script.

4. Using Terminal's Environment Variables in PyCharm:

- PyCharm usually does not automatically inherit environment variables set in your system’s terminal.

- However, you can start PyCharm from the terminal where these variables are set, which might make PyCharm inherit them. This is less reliable and varies depending on the operating system and PyCharm version.

Remember, it’s crucial to keep your AWS credentials secure. Avoid hardcoding them directly in your scripts or storing them in files that might get pushed to version control. Always use environment variables or other secure methods for handling sensitive information like AWS keys.

7 views0 comments
bottom of page