Often when using conda environments, the environment variables set in my rc file are overwritten (experienced with both bash as zsh), particularly my $PATH variable. It does not always happen though; I just experienced this, then tried to reproduce it but the second time it did not happen.
After activating the conda environment (source activate py35) I can resource my rc file (source ~/.zshrc) to recover my environment variables but can I avoid this extra step?
Related
I've installed emsdk, following the steps described in the following document: https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install
Now, when I launch a Terminal under macOS, I have these lines inserted at the beginning:
Setting up EMSDK environment (suppress these messages with EMSDK_QUIET=1)
Adding directories to PATH:
PATH += [private]/emscripten/emsdk
PATH += [private]/emscripten/emsdk/upstream/emscripten
PATH += [private]/emscripten/emsdk/node/14.18.2_64bit/bin
Setting environment variables:
PATH = [private]/emscripten/emsdk:[private]/emscripten/emsdk/upstream/emscripten:[private]/emscripten/emsdk/node/14.18.2_64bit/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
EMSDK = [private]/emscripten/emsdk
EM_CONFIG = [private]/emscripten/emsdk/.emscripten
EMSDK_NODE = [private]/emscripten/emsdk/node/14.18.2_64bit/bin/node
EMSDK_PYTHON = [private]/emscripten/emsdk/python/3.9.2_64bit/bin/python3
SSL_CERT_FILE = [private]/emscripten/emsdk/python/3.9.2_64bit/lib/python3.9/site-packages/certifi/cacert.pem
I can't find from where this is launched. emsdk doesn't appear in .bash_profile, nor in .profile or .bashrc.
Where do I have to set EMSDK_QUIET=1 to avoid these lines?
A lot of times when you install a program that needs to alter the environment like compiler toolchains the install script will modify the file it expects to be sourced by your shell based on the default shell for the current user or sometimes by scanning $HOME. On MacOS you might see it add a line to (or create if it can't find):
$HOME/.bashrc or $HOME/.bash_profile for bash
$HOME/.zshrc or $HOME/.zprofile for zsh
$HOME/.config/fish/config.fish for fish
Note that recent versions of MacOS have changed the default shell from bash to zsh. Due to licensing issues they have to ship an archaic version of bash (3.2 vs 5.x current) so it was probably a good move but it means you may need to check the zsh files instead of the usual bash ones.
When you need to set an envar for a toolchain like EMSDK_QUIET=1 just look for the line where the environment is sourced, and export that envar above it.
I think I have seen others who have had this same issue, but perhaps it is due to where the Anaconda image is being run on the system ( i.e where the venv is that pip has installed)
Just a note that my processor- arch is x86, and with some older style bus and memory layout.
What's more likely is that CC environment variable is affecting pip/make/autotools.
You can view your environment variable's value by running:
env |grep ^CC=
The ~/.bashrc file itself isn't read unless an interactive shell is launched, so for those shell scripts and/or Make calls that pip uses to compile Python modules, it's not likely to be sourced, but any process spawned from a shell where the CC environment variable is set will inherit that environment variable, regardless of whether it reads ~/.bashrc.
Does anyone know how to automatically set environment variables when activating an env in conda?
I have tried editing */bin/activate, but that adds the new environment variables for every new env that is created. I want to set env variables that are specific to each env.
Use the files $CONDA_PREFIX/etc/conda/activate.d and $CONDA_PREFIX/etc/conda/deactivate.d, where $CONDA_PREFIX is the path to the environment.
See the section on managing environments in the official documentation for reference.
Environment Variables as Configuration Settings
Conda v4.8 introduced a new command-line interface in the conda-env tool for managing environment variables on a per-environment basis. The command is conda env config vars and here is the help description as of v4.8.3 for the command overall:
$ conda env config vars -h
usage: conda-env config vars [-h] {list,set,unset} ...
Interact with environment variables associated with Conda environments
Options:
positional arguments:
{list,set,unset}
list List environment variables for a conda environment
set Set environment variables for a conda environment
unset Unset environment variables for a conda environment
optional arguments:
-h, --help Show this help message and exit.
examples:
conda env config vars list -n my_env
conda env config vars set MY_VAR=something OTHER_THING=ohhhhya
conda env config vars unset MY_VAR
Perhaps a bit verbose, but it avoids having to manually manage files in etc/conda/(de|)activate.d.
YAML Specification
Added in Conda v4.9, there is now support for automatic defining of environment-specific variables as part of an environment YAML definition. For example,
name: foo
channels:
- defaults
dependencies:
- python
variables:
MY_VAR: something
OTHER_VAR: ohhhhya
which would set up the environment variables MY_VAR and OTHER_VAR to be set and unset on environment activation and deactivation, respectively.
The accepted answer (conda/activate.d and conda/deactivate.d) works well enough, but it is inconvenient if you want the environment variables to be version controlled without putting the entire environment into version control too. Generally you'd want to store only the environment.yml file in version control.
(I understand that this does not apply to all projects - sometimes the entire reason for using environment variables is to prevent that particular configuration getting stored in version control.)
My preference (on Windows, but the same principle would apply on Linux) is to create a (version-controlled) activate.cmd file in the root of the project directory that sets the environemnt variable(s) and then calls conda's own activate.bat script.
Example (a per-project pylint configuration):
set PYLINTRC=%cd%\pylintrc
#activate.bat %cd%\env
Note that on Windows at least you have to set the environment variables before calling activate.bat because the call to activate.bat never returns to the calling batch file. You also have to name your own script something other than activate.bat to avoid recursion, which is why I chose the cmd extension (which is treated by Windows as a batch file in this context).
So for virtualenv on Ubuntu I did the below where my virtual environement names is my_env and my environmental variables I want to persist were VAR_A and VAR_B:
virtualenv my_env
vim my_env/bin/activate
This opens the file and you can append your env variables to the end of the file like the below:
# This is me env variables to persist
export VAR_A=/home/developer/my_workspace/var_a
export VAR_B=/home/developer/my_workspace/var_b
Then exit the file.
Activate your virtualenv with
source my_env/bin/activate
Then your env variables should be good. Can verify like the below:
printenv | grep VAR_
VAR_B=/home/developer/my_workspace/var_b
VAR_A=/home/developer/my_workspace/var_a
How I can EXPORT environment variable not just for one tab but for all system?
If i will use export JAVA_HOME=/Library/Java/Home it will set JAVA_HOME only for current terminal tab and after system restart I will need do it one more time.
How I can set environment variable globally to make by default?
How I can edit variables in $ env list?
Add an entry to ~/.bash_profile:
export JAVA_HOME=/Library/Java/Home
save it, (or create it if it doesn't exist)
quit Terminal.app
re-launch and you're in business.
This is the best place to add the entry in my opinion, although for the distinct differences on OS X of where to add environment variables specifically for one reason or another see:
https://apple.stackexchange.com/a/13019
And for a more generalized UNIX overview:
What's the difference between .bashrc, .bash_profile, and .environment?
You can set environment variables by adding the export commands to various configuration files. E.g. ~/.profile
You can find out more about what files can be used to modify your bash environment by reading the bash manual (man bash). If you're using a different shell then it's probably similar and its man page should contain the same info. You can also read this answer on unix.stackexchange.com, which has some of these details.
If you want to set environment variables for your entire user environment, including GUI applications, I believe you can do that using ~/.MacOSX/environment.plist.
I know that csh has a lot of start-up files (.login, .cshrc, etc.). The problem is that I'm starting a new csh terminal and I'm seeing a lot of non-standard (not the standard ones like $HOME, $SHELL, etc.) environment variables set at start-up that I didn't set in any of my start-up scripts. Is there a way to figure out the sequence of files that get sourced at start-up in my current session. If not, is there even a facility that tells which script sets some environment variable given the variable's name?
You need to check the system-wide start-up files. Start with:
/etc/profile
which calls:
/etc/{the system-wide shell setup you are using} e.g. /etc/csh
Then your own profile is called
~/.profile
which in turn calls your personal shell setup, e.g. ~/.csh