Enviroment variables in pyenv-virtualenv
Asked Answered
C

3

7

I have created a virtual environment with pyenv virtualenv 3.5.9 projectname for developing a django project.

How can I set environment variables for my code to use?

I tried to add the environment variable DATABASE_USER in /Users/developer/.pyenv/versions/projectname/bin/activate like this:

export DATABASE_USER="dbuser"

When I tried to echo $DATABASE_USER an empty string gets printed.

Tried to install zsh-autoenv

And now I can echo $DATABASE_USER and get the value set in the .autoenv.zsh file.

But I can't seem to get the environment variable to be available to my django code:

If I try to os.getenv('DATABASE_USER', '') in the python shell inside the virtualenv, I get ''

What could be wrong? Is the zsh-autoenv variables just available for the zsh shell and not python manage.py shell ?

Congruency answered 7/9, 2020 at 14:11 Comment(0)
M
4

I was wondering a similar thing, and I stumbled across a reddit thread where someone else had asked the same question, and eventually followed up noting some interesting finds.

As you noticed, pyenv doesn't seem to actually use the bin/activate file. They didn't say what the activation method is, but like you, adding environment variables there yielded no results.

In the end, they wound up installing autoenv, which bills itself as directory-based environments. It allows you to create an .env file in your directory, and when you cd to that directory, it runs the .env file. You can use it for environment variables, or you could add anything else to it.

I noticed on the autoenv page that they say you should probably use direnv instead, as it has better features and is higher quality software. Neither of these are Python or pyenv specific, and if you call your python code from outside of the directory, they may not work. Since you're using pyenv, you're probably running your code from within the directory anyway, so I think there's a good chance either one could work.

Mazuma answered 21/4, 2021 at 1:46 Comment(1)
direnv helps me on working with pyEnv virtual environment.Tulatulip
W
0

As suggested by @Jeremy_W I installed direnv and configured on MacOS/zsh as:

brew install direnv
eval "$(direnv hook zsh)"
cd <your-project>
echo export FOO=foo > .envrc
direnv grant .
echo $FOO

(please see the docs for your specific OS or shell version)

It works like a charm.

Woodson answered 24/4, 2023 at 15:56 Comment(0)
H
0

Similar to @Jeremy W, I encountered the same Reddit thread but philgyford's response worked well for me with some tinkering. Here is a quick overview of my steps:

  1. Run pyenv versions to get the full venv paths one has created by pyenv (e.g., $HOME/.pyenv/versions/3.11.7/envs/my-venv.

  2. Run vim $HOME/.pyenv/versions/3.11.7/envs/my-venv.bin/activate to edit the file that instantiates venvs created by pyenv.

  3. Add one's custom environment variables

# ... earlier part of file redacted for simplicity

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

# PLACE VAR HERE! (I'm unsure if positioning actually matters within the file)
export ENV_VAR_NAME="xxyyxx"

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
    _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
    unset PYTHONHOME
fi
# ... rest of file redacted for simplicity
  1. Save the file and exit.

If you open a python session within the directory of the venv (which again, should automatically activate when set up with pyenv properly) and run:

import os

os.environ[`ENV_VAR_NAME`]

It should print the appropriate value (i.e., xxyyzz).

I utilize pyenv via Brew installation so following the repo's extensive documentation will get you up and running just fine, especially the bash_profile and bashrc instructions that allow for automatic environment activations.

Hurl answered 29/2 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.