256👍
You can set environmental variables in Pycharm’s run configurations menu.
- Open the Run Configuration selector in the top-right and click
Edit Configurations...
- Select the correct file from the menu, find
Environmental variables
and click...
- Add or change variables, then click
OK
You can access your environmental variables with os.environ
import os
print(os.environ['SOME_VAR'])
171👍
I was able to figure out this using a PyCharm plugin called EnvFile. This plugin, basically allows setting environment variables to run configurations from one or multiple files.
The installation is pretty simple:
Preferences > Plugins > Browse repositories… > Search for “Env File” > Install Plugin.
Then, I created a file, in my project root, called environment.env
which contains:
DATABASE_URL=postgres://127.0.0.1:5432/my_db_name
DEBUG=1
Then I went to Run->Edit Configurations, and I followed the steps in the next image:
In 3, I chose the file environment.env
, and then I could just click the play button in PyCharm, and everything worked like a charm.
- [Django]-How to put comments in Django templates?
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Why won't Django use IPython?
32👍
The original question is:
How to set environment variables in PyCharm?
The two most-upvoted answers tell you how to set environment variables for PyCharm Run/Debug Configurations – manually enter them in "Environment variables" or use EnvFile plugin.
After using PyCharm for many years now, I’ve learned there are other key areas you can set PyCharm environment variables. EnvFile won’t work for these other areas!
Here’s where ELSE to look (in Settings):
- Tools > Terminal > Environment variables
- Languages & Frameworks > Django > Environment variables
- Build, Execution, Deployment > Console > Python Console > Environment variables
- Build, Execution, Deployment > Console > Django Console > Environment variables
and of course, your run/debug configurations that was already mentioned.
- [Django]-How to mix queryset results?
- [Django]-Django: Display Choice Value
- [Django]-Django.contrib.auth.logout in Django
19👍
This functionality has been added to the IDE now (working Pycharm 2018.3)
Just click the EnvFile
tab in the run configuration, click Enable EnvFile
and click the + icon to add an env file
Update: Essentially the same as the answer by @imguelvargasf but the the plugin was enabled by default for me.
- [Django]-How to reset Django admin password?
- [Django]-How to filter objects for count annotation in Django?
- [Django]-Django queries – id vs pk
7👍
This is what you can do to source an .env (and .flaskenv) file in the pycharm flask/django console. It would also work for a normal python console of course.
-
Do
pip install python-dotenv
in your environment (the same as being pointed to by pycharm). -
Go to: Settings > Build ,Execution, Deployment > Console > Flask/django Console
-
In "starting script" include something like this near the top:
from dotenv import load_dotenv
load_dotenv(verbose=True)
The .env file can look like this:
export KEY=VALUE
It doesn’t matter if one includes export
or not for dotenv to read it.
As an alternative you could also source the .env file in the activate shell script for the respective virtual environement.
- [Django]-How to implement FirebaseDB with a Django Web Application
- [Django]-Checking for empty queryset in Django
- [Django]-Django – filtering on foreign key properties
4👍
Let’s say the environment variables are in a file and store like below.
Copy all of them to clipboard Ctrl+A
-> Ctrl+C
Then click on Edit Configurations
-> Environment Variables
.
Now click on the small paste icon, and all the environment variables, will be available for usage in current environment.
- [Django]-How to assign items inside a Model object with Django?
- [Django]-Why is __init__ module in django project loaded twice
- [Django]-Django values_list vs values
2👍
This method helped me a lot for calling environmental variable from a .env file and its really easy to implement too.
-
Create a .env file in the root folder of the project
-
Install a package called "python-dotenv"
-
Import and load the package in the .py file where you want to use the environment variable as follows
import os from dotenv import load_dotenv, find_dotenv load_dotenv(find_dotenv()) any_secret_var = os.get_env("SECRET_KEY")
and BOOM it will work like magic
And the .env file should look like,
don’t place any qoutes around the values in the .env file
SECRET_KEY=any_secret_value_you_need_to_place_in
- [Django]-What does error mean? : "Forbidden (Referer checking failed – no Referer.):"
- [Django]-How to check Django version
- [Django]-Uwsgi installation error in windows 7
1👍
None of the above methods worked for me. If you are on Windows, try this on PyCharm terminal:
setx YOUR_VAR "VALUE"
You can access it in your scripts using os.environ['YOUR_VAR']
.
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
- [Django]-Visual Editor for Django Templates?
- [Django]-Setting Django up to use MySQL
1👍
In case anyone want to set environment variables for all their Run/Debug Configuration, I list my solution here.
- Go to the Edit Configurations as pointed by other answers.
- Click edit the configuration templates in the panel.
- Edit environment variables there.
By doing so, the variables will be there every time you create a new configuration.
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
- [Django]-Django template includes slow?
- [Django]-Django: Redirect to previous page after login
0👍
Solution tested with virtual environment.
Create an script that define and export or source the env vars. Then define the script as the python interpreter of an existing virtual env. The solution works for any task like run, debug, console …
Example script:
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export VAR1="TEST1"
source $DIR/../../.env
$DIR/python "$@"
- [Django]-How to test "render to template" functions in django? (TDD)
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-Django limit_choices_to for multiple fields with "or" condition
0👍
This is the best way to set environment variable in pycharm terminal. Just add the following lines at the end of venv/bin/activate
script if you are using a virtual environment:
export $(grep -v '^#' .env | xargs)
- [Django]-Group by Foreign Key and show related items – Django
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-Django Framework – Is there a shutdown event that can be subscribed to?
-1👍
In the Pycharm Python console just type
ENV_VAR_NAME="STRING VARIABLE"
INT_VAR_NAME=5
Unlike other Python text editors, you do not add export
in Pycharm
- [Django]-What is actually assertEquals in Python?
- [Django]-What's the best solution for OpenID with Django?
- [Django]-Django rest framework, use different serializers in the same ModelViewSet