I am using Elpy on Emacs and using pipenv. When I start an Elpy Python shell or send the current buffer to the shell, I cannot get the environment variables that I set in the terminal. But I would very much like to be able to send my code and env vars over to the Elpy shell, how can I do that?
The virtual environment is works fine because all my packages are there; also, if I use the pipenv-shell, I can access my env vars.
I checked on os.environ and it shows that I'm using the correct virtual environment, and everything else looks just about right there, just none of the variables I set myself. Is there a way to set the environment variables from Elpy? I looked at the documentation, but I am still unclear on this. I would rather use the OS environment variables than "project variables" or anything like that because I need to be able to edit and run the code outside of Emacs as well.
Edit: OK, I realized that I can use M-x setenv to set the environment variable, which does what I need, but I would still like to know how to get Elpy to recognize already set environment variables.
Related
I am using VSCodes terminal pane. I activate a conda environment. For some reason, the python command is still set to /usr/bin/python, instead of the correct path to the conda environment.
% conda activate myenv
% which python
/usr/bin/python
The correct anaconda environment directory does seem to be in the $PATH variable, but /usr/bin seems above it in priority.
When I open a standard terminal through the OS, the behavior is as I expect.
% conda activate myenv
% which python
/Users/cpl/anaconda3/envs/myenv/bin/python
Please note: I have already set the VSCode preferences key python.pythonPath to /Users/cpl/anaconda3/envs/myenv/bin/python, and I think that it works correctly. When I run a file through right-clicking and selecting Run Python File In Terminal, the correct python (from the conda environment) is executed. My problem is using the VSCode terminal directly to execute python.
My shell is zsh, and I am using OSX. Any advice?
This behavior is explained in the VSCode docs:
Why are there duplicate paths in the terminal's $PATH environment variable and/or why are they reversed?#
It sounds like VSCode will run your .zshrc twice in MacOS, conflicting with the conda-generated PATH variable definitions.
There are two solutions listed in the link above. The one that works for me is to set the VSCode setting "terminal.integrated.inheritEnv": false. The documentation warns that all of your environmental variables will be stripped if you do this. However, I find I still have my custom variables defined in the .zshrc file.
It is worth noting that recent versions of VSCode will prompt you when it detects you are using a conda environment, and suggests making this change.
I don't use zsh, but I've run into this issue in bash and I believe the cause is the same.
Conda has recently changed the "official" method of activating environments, as described in this issue: https://github.com/Microsoft/vscode-python/issues/1882
Before, you needed to modify your .bashrc/.zshrc to prepend PATH with the directory of conda's activate script, and then activate specific environments by typing source activate name_of_env. VSCode-Python activates conda terminals by sending this command to the shell — with visible echo, like you typed it yourself.
The new method is to source $HOME/anaconda3/etc/profile.d/conda.sh in .bashrc and then activate environments with conda activate name_of_env, which is the behavior you're seeing work correctly in a dedicated terminal. VSCode-Python does not yet support this, and there appear to be issues with cross-platform support on Windows that are complicating the transition.
The best solution for now is to ignore the "correct" method of conda activate and consistently use the older source activate name_of_env, which still works (if your PATH is set to include $HOME/anaconda3/bin).
A dark magic below may work:-
In my Big Sur, I added the following empty entry to my settings.json -- can be found at File(Windows)/Code(Mac)>Preferences>Settings -- click on any link stated "Edit in settings.json"
"terminal.integrated.env.osx": {
"PATH": ""
}
All the best!
If anyone else in the future ends up scratching their heads over this particular problem, I’ve found another culprit:
Terminal>Integrated>Env: Enable Persistent Sessions
I suspect what happens is that after you update system paths, VScode caches the old path in the terminal and persists it. In this case it persists the old python path rather than the new conda one.
Toggling this option off and restarting VSCode clears that cache, and the new path is loaded in. You can also toggle the option back on after you’re done.
I have package atom-python-virtualenv and other package termination as terminal.
I am looking for solution which allow me to use automatically virtual enviroment inside terminal.
It should work like this:
I choose virtual environment in atom-python-virtualenv paskage . ( I do not want to talk about how to create env.)
When i open terminal in termiantion package, I want to have the same virtual environment in terminal.
If no virtual environment selected, then terminal in normal mode.
Does Termination works automatically under virtualenv? I don't think so, because it do not show me.
I create solution not exactly what i expect but is working.
Inside termination settings have we auto run command and there you have to write 3 commands.
1. Jump to env directory.
2. Start env
3. Come back to previous dir.
Look on print-screen:
I want to be able to set up environment variables in my virtual environment so that they are available in my code when I activate the virtual environment. I make my virtual enviornments with venv. I'm working on a Windows machine with VS-code.
What I already tried, but didn't work.
Adding the vars to end of the activate.bat file like this:
set CLIENT_SECRET="MYSECRET"
Adding the vars to the end of the Activate.ps1 file like this:
$CLIENT_SECRET="MYSECRET"
Adding the vars to the end of the activate file like this:
export CLIENT_SECRET="MYSECRET"
I found a lot related to my topic, but none working for me. What to do?
If you want to setup your development environment in VSCode you can simply add .env file with all secrets defined in project root directory. More details in docs
Your first solution
set CLIENT_SECRET=MYSECRET
in activate.batshould work, when using Command Prompt in the terminal as Default Shell.
You can omit the quotes unless they are part of your envirionment variable.
You can verify, if the environment variable is set with:
echo %CLIENT_SECRET% in the terminal in VS-Code.
go to endowment variable folder enter Script folder now activate with cmd
use set in CMD teminal
use env: in powershell
I defined my environment variables inside env/bin/activate and the following came out from the output when i activated them
debug="True"
secret="RI4MgORxGb4c6zhotTHSNXc54lz1IWGbIoETfmj2VB99sPNlk9YMcg4b6qyX"
using echo in zsh. Now that tells me that the environment variables are set properly. I am getting the same inside django settings using
DEBUG = True if os.environ.get("debug") == "True" else False
The problem comes when i run
python manage.py runserver
which is unable to find those variables as opposed to
env debug='True' python manage.py run server
which works fine in debug mode and production depending on environment variables. What am i missing over here ? is this something because of using zsh shell or am i getting the variables wrong way somehow ?
In many shells, setting a variable only does so for the shell process itself. If you want to turn a shell variable into an environment variable then you need to export the variable, usually with the export command.
As the title suggests, I'm trying to use a environment variable in a config file for a Flask project (in windows 10).
I'm using a virtual env and this far i have tried to add set "DATABASE_URL=sqlite:///models.db" to /Scripts/activate.bat in the virtualenv folder.
But it does not seem to work. Any suggestions?
Flask does not automatically take configuration variables from environment variables. You have to set them manually like so:
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
More info regarding configuration can be found here.
To set an environment variable in Windows, you should do as described here:
setx DATABASE_URL=sqlite://something.something
The problem was that PyCharm does not activate the virtualenvironment when pressing the run button. It only uses the virtualenv python.exe.