I am trying to use env variables in my rails app and set those variable values in ubuntu 14.04
I tried setting using export command
export mongodb_username="abc"
export mongodb_password="cde"
and also tried setting them in /etc/environment and in ~/.bashsrc
and printenv gives following results
>> printenv mongodb_username
=> abc
>> printenv mongodb_password
=> cde
BUT in RAILS APP or irb the output is following
>> ENV['mongodb_password']
=> nil
>> ENV['mongodb_username']
=> nil
I am missing something? Please help!!!
When setting an environment variable's value using export, that value is available only in the shell in which it was set, and its subshells. So you'll need to export those variables in every shell in which you need them.
However, you can automate this, of course.
If you have variables that you need frequently, one approach is to put their assignments in a shell script and then source the shell script in any shells you need them in (see http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x237.html for more on this).
If it's ok to have the variables be in effect in all your shells, then a simpler way is to export them from your startup script (probably ~/.bashrc).
Related
In my rails application, I exported my database variables in .profile using ansible. the variables are accessible by the command printenv. However, when I run the application or use rails c via ENV['NAME'] , the variables aren't there.
Does anyone have any idea why rails doesn't load variables from .profile?
The ~/.profile script is only meant to be read by your (presumably POSIX compatible) shell. And even then only if it is what is known as a "login" shell. For example, from the bash man page:
When invoked as an interactive login shell, or a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order.
If you're running your rails application from an interactive shell prompt it should have access to the env vars you're setting in ~/.profile. If you're starting your rails app some other way (e.g., from your window manager) then you'll need to find some other way to set the env vars that it inherits.
I have several environment variable from a text.txt file , i set them with export variable = value manually one by one myself in terminal Ubuntu 18.04 but now no one of them appear in printenv !
I need to set them somehow never remove again(unless i delete them myself).any idea?thank you
Add your variables in ~/.bashrc or in /etc/environment this way on every reboot they will be exported.
in bashrc should be like this
export VARIABLE='<value>'
in /etc/environment
VARIABLE=<value>
I switched to zsh from bash. I updated the shell in Preferences > Terminal settings inside RubyMine.
But, now environment variables are not being loaded inside my Rails application. I can still access them inside the terminal in RubyMine editor!
I tried printing the value of environment variable inside a yml file (where all the DB related environment variables are required). I could access the home variable but not custom variables set by me.
Database.yml file:
Output while starting Rails server in Rubymine:
Output inside Rubymine terminal:
My /etc/zshrc:
DB settings inside my vaibhavatul47_zsh_profile.sh file:
Automatic loading Environment variables from bash into IntelliJ works while reading and loading from zsh doesn't work for Intellij.
Starting IntelliJ from Terminal will load environment variables from zsh too, please try following:
open -a "IntelliJ IDEA"
Note: here IntelliJ IDEA is name of my application, in case you have renamed your IntelliJ application to something else please enter that.
Hope this helps!
Check that your env var are loaded correctly in the terminal and then open IDE from the terminal. Then check if the build configuration env vars contains the profile env vars.
Before opening the idea make sure your environment variables are actually loaded when running the terminal:
open your zsh profile (vim ~/.zshrc)
insert a test env var (something like TEST_1="mytest")
restart the terminal and check if you see TEST_1 value (echo $TEST_1)
In case you see TEST_1 value open intelij idea by entering "idea ."
Now, open your module build configuration and look for the env vars list, check if they contain your zsh env vars (or you can type "echo TEST_1" in the idea terminal)
gl:)
When I use vim to update my environmental variables (in ~/.bashrc), PyCharm does not get the updates right away. I have to shut down the program, source ~/.bashrc again, and re-open PyCharm.
Is there any way to have PyCharm source the changes automatically (or without shutting down)?
When any process get created it inherit the environment variables from it's parent process (the O.S. itself in your case). if you change the environment variables at the parent level, the child process is not aware of it.
PyCharm allows you to change the environment variables from the Run\Debug Configuration window.
Run > Edit Configurations > Environment Variables ->
In my case pycharm does not take env variables from bashrc even after restarting
Pycharm maintains it's own version of environment variables and those aren't sourced from the shell.
It seems that if pycharm is executed from a virtualenv or the shell containing said variables, it will load with them, however it is not dynamic.
the answer below has a settings.py script for the virtualenv to update and maintain settings. Whether this completely solves your question or not i'm not sure.
Pycharm: set environment variable for run manage.py Task
I recently discovered a workaround in windows. Close Pycharm, copy the command to run Pycharm directly from the shortcut, and rerun it in a new terminal window: cmd, cmder, etc.
C:\
λ "C:\Program Files\JetBrains\PyCharm 2017.2.1\bin\pycharm64.exe"
I know this is very late, but I encountered this issue as well and found the accepted answer tedious as I had a lot of saved configurations already.
The solution that a co-worker told me is to add the environment variables to ~/.profile instead. I then had to restart my linux machine and pycharm picked up the new values. (for OSX, I only needed to source ~/.profile and restart pycharm completely)
One thing to be aware is that another coworker said that pycharm would look at ~/.bash_profile so if you have that file, then you need the environment variables added there
In case you are using the "sudo python" technique, be aware that it does not by default convey the environment variables.
To correctly pass on the environment variables defined in the PyCharm launch configuration, use the -E switch:
sudo -E /path/to/python/executable "$#"
This is simply how environment variables work. If you change them you have to re-source your .bashrc (or whatever file the environment variables are located in).
from dotenv import load_dotenv
load_dotenv(override=True)
Python-dotenv can interpolate variables using POSIX variable expansion.
With load_dotenv(override=True) or dotenv_values(), the value of a variable is the first of the values defined in the following list:
Value of that variable in the .env file.
Value of that variable in the environment.
Default value, if provided.
Empty string.
With load_dotenv(override=False), the value of a variable is the first of the values defined in the following list:
Value of that variable in the environment
Value of that variable in the .env file.
Default value, if provided.
Empty string.
I am developing a Rails 4 app on cloud9 (c9.io). When I placed SECRET="geheim" in config file, it works fine. I tried setting an environment variable using
echo "export SECRET=geheim" >> ~/.profile
and then using ENV['SECRET'] in config file, but it doesn't work. When I type printenv SECRET in console, it returns nothing, meaning the variable is not set. How can I fix this? Thanks.
You can add environment variables on cloud9 only if you are using the run panel to run your application. In the run panel theres a ENV button at the far right side where you can set your environment variables.
Heres some documentation about setting up your run command:
https://docs.c9.io/v1.0/docs/run-an-application
Unfortunately, this doesnt work if you're running your app from the terminal as cloud9 doesnt seem to support environment variables directly from the terminal.
In the linux terminal:
% export <env-variable-name> = <env-variable-value>
For example, setting an AWS-S3 bucket:
% export PHOTOS_BUCKET='s3://edx-photo-lab/photos/'
source: https://docs.aws.amazon.com/cloud9/latest/user-guide/env-vars.html
The above solution has the problem that the variables are cleared when the Cloud9 EC2 is rebooted.
To persist the variable you must add the export statement to the ~/.bashrc. You could use vim for edit:
% sudo vim ~/.bashrc