Set environment variables when activating python virtual environment in windows - python

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

Related

Atom, termination, virtual enviroment

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:

Setting environment variables in virtualenv (Python, Windows)

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.

Change default environment in anaconda

By default, anaconda is using my root environment. However, I'm always using another environment called py34 that I activate manually. Is it possible to change the default environment from root to another one?
Edit: I forgot to mention that I'm working on Windows. On Unix systems adding a line to .bashrc is a solution of course.
You could add the following line to the end of your ~/.bashrc or ~/.bash_profile file:
source activate py34
Unfortunately, I'm not familiar with developing on Windows, but this might help? Another possibility is to use an IDE such as pycharm, which I can really recommend for developing python. There, you can change your project settings to your py34 environment.
On Windows:
Search for the Activate.bat file, Usually it is in
"C:\Users\Your_UserName\AppData\Local\Continuum\anaconda3\Scripts"
Open Activate.bat file in any Text Editor and Append
activate Your_Environment_Name
Save it and after that whenever you will open Anaconda Prompt Your_Environment will get Activated by default instead of base environment.
You can use this link to create a new env - https://stackoverflow.com/a/60820687/11383333
Once your env is created, you can use the answer mentioned by Jayesh to update the activate.bat file -
Let's say the new env name is python39, then the original line in activate.bat looks like this -
#CALL "%~dp0..\condabin\conda.bat" activate %*
Update it to this -
#CALL "%~dp0..\condabin\conda.bat" activate python38
Save it and restart your anaconda prompt

How to configure virtual environment hooks before or after activating the virtual environment, and before or after deactivating it in Windows

I am new to Django. I have created the virtual environment. With virtualenvwrapper, there is a way to configure different hooks that are sourced before or after activating the virtual environment, and before or after deactivating it, but I am not sure how to do this. I am using Windows for this. There is a lot of help for this in Linux but I can't find anything for Windows.
Since you are using windows you should read this thread before.
Once finished and decided to use tools such as powershell, you can do as follow:
in your virtualenv directory you will find your app folders, under virtualenv_path/app/bin you find all the hooks:
Example: You need to set DJANGO_SETTINGS_MODULE to tell django use my local setting, you need to set it in each activation of the env and unset it when you deactive the env:
in postactive file add : export DJANGO_SETTINGS_MODULE="myproject.settings_local"
and in predeactivate you add: unset DJANGO_SETTINGS_MODULE
So the variable will be set every time you activate your env and unset every tine you deactivate.
To check if every thing is working correct when you activate your env try in shell:
echo $DJANGO_SETTINGS_MODULE

I can't remember the name of the virtual environment - how can I see a list of all virtual environments?

I installed a virtual environment as per: http://docs.python-guide.org/en/latest/dev/virtualenvs/
I stopped working on the project for a few days and can't remember the name of the environment. How can I find out the name? I've done some searchign and came across this exact question here:
Forgot virtualenv name - how to find the name of the virtual env?
When I try the suggestions the terminal just says the command doesn't exist.
If you are using conda try this at the command prompt:
conda env list
This should give you a summary of all accessible environment which you have created.
Using the terminal cd here /Users/${USER}/Library/Jupyter/kernels and then do an ls to see all your names.
got this answer from this post
From my bash terminal, I type ls and it lists all of my directories from there, including my virtual environments. If you do not see your virtual environments listed, try changing directory into the folder that it is contained in.
To find the lost Django virtual Environments.
in the terminal type the command : lsvirtualenv
or in Windows Operating System you are able to see it in File Explorer
C:\Users<User Name>\Envs\

Categories