Setting environment variables in virtualenv (Python, Windows) - python

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.

Related

Django deployment and virtual environment

I am aware that there are many questions regarding Django and virtual environments, but I cannot wrap my head around the use of virtual environments with respect to deploying my Django app (locally) via uwsgi/nginx.
My setup includes a virtual environment (with Django and uwsgi), my Django app, nginx and PostgreSQL. The app was created before the virtual environment, and I applied only a single change to manage.py:
#!/Users/snafu/virtualdjango/bin/python3
When I start up the uwsgi located in the virtual environment (with the appropriate .ini file), everything works right away, but I wonder why. I did not need to fiddle around with the $PYTHONPATH, or append the site packages directory to the system path in manage.py, or activate the virtual environment at any point (apart from the initial installation of packages), although the boilerplate comment in manage.py explicitly mentions an inactive virtual environment as a possible reason for an import error.
Activating a virtual environment does nothing but prepend the virtual environment's bin/ to the $PATH thus making python and pip without explicit paths running from the virtual environment. Everything else related to virtual environments is implemented inside Python — it automatically changes sys.path and other paths (sys.prefix, sys.exec_prefix, etc).
This means that when you run python with an absolute path from a virtual environment Python automatically activates the virtual environment for this particular Python session. So you don't need to activate the virtual environment explicitly.
There is a minor warning sign on the road though: to run any Python script from a non-activated virtual environment you must set the shebang for all scripts to point to the virtual environment or use sys.executable. Do not use explicit python because that could be a different Python from the $PATH.

Set environment variables when activating python virtual environment in windows

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

How to get environment variables in Emacs Elpy Python shell using Pipenv?

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.

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

Prevent system-wide and virtualenv ipython from using the same directory for configuration

Recently, I started looking into virtualenv but ran into
problems with the configuration of ipython.
I created the virtual environment using
virtualenv --no-site-packages ENV
From within ENV (i.e. after source bin/activate),
I installed ipython.
pip install ipython
However, after having used ipython from within ENV,
I got a problem using the ``normal'' version.
Basically, I get the following warnings:
WARNING: Configuration file ipythonrc not found. Ignoring request.
and
WARNING: Problems loading configuration file 'ipythonrc'
Starting with default -bare bones- configuration.
The system-wide ipython is version 0.10 (still using Kubuntu 10.04 LTS),
whereas the one inside ENV is 0.13.
Apparently, there has been a change in the configuration.
If I remove the folder $HOME/.ipython/ and start up the system-wide
installation, everything works fine again.
But as soon as I launch ipython from within ENV, the content of
$HOME/.ipython/ is replaced and in particular, the file ipythonrc
is removed.
So my question is, how can I prevent both ipythons from
using the same folder $HOME/.ipython/ for configuration purposes?
Set the environment variable $IPYTHONDIR to something in your virtual-env. The IPython config location is determined relative to that, so you just need to add export IPYTHONDIR="$VIRTUAL_ENV/.ipython" or similar to your activate (and appropriate inverse on deactivate).
I would suggest using a different configuration file for the virtualenv version.
One option to do this is by adding an alias in the bin/activate file.
Something like this should do the trick:
alias ipython="ipython -rcfile $VIRTUAL_ENV/.ipythonrc"
Alternatively, setting the IPYTHONDIR variable to $VIRTUAL_ENV/.ipython/ should also work. This option appears undocumented in the manpage but the code shows that it is using that.

Categories