question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot use environment variables defined in .env in `pipenv run` and scripts in Pipfile

See original GitHub issue

Issue description

I want to use environment variables defined in .env in pipenv run and scripts in Pipfile. For example, when FOO=SPAM is in .env and show-foo = "echo $FOO" is in Pipfile as a script, I want to get SPAM by running pipenv run echo '$FOO' or pipenv run show-foo.

Expected result

Running pipenv run echo '$FOO' or pipenv run show-foo returns SPAM with the following Pipfile and .env:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.10"

[scripts]
show-foo = "echo $FOO"
FOO=SPAM

Actual result

Running pipenv run echo '$FOO' or pipenv run show-foo behave as follows:

$ pipenv run echo '$FOO'
Loading .env environment variables...
$FOO
$ pipenv run show-foo
Loading .env environment variables...
$FOO

$FOO is not evaluated even if FOO=SPAM in .env.

Steps to replicate

  1. create the following Pipfile

    [[source]]
    url = "https://pypi.org/simple"
    verify_ssl = true
    name = "pypi"
    
    [packages]
    
    [dev-packages]
    
    [requires]
    python_version = "3.10"
    
    [scripts]
    show-foo = "echo $FOO"
    
  2. create .env

    FOO=SPAM
    
  3. run pipenv run echo '$FOO' or pipenv run show-foo

    $ pipenv run echo '$FOO'
    Loading .env environment variables...
    $FOO
    $ pipenv run show-foo
    Loading .env environment variables...
    $FOO
    

$ pipenv --support

Pipenv version: '2022.1.8'

Pipenv location: '/home/hmasu/.pyenv/versions/3.10.2/lib/python3.10/site-packages/pipenv'

Python location: '/home/hmasu/.pyenv/versions/3.10.2/bin/python3.10'

Python installations found:

  • 3.10.2: /home/hmasu/.pyenv/versions/3.10.2/bin/python3
  • 3.10.2: /home/hmasu/.pyenv/versions/3.10.2/bin/python
  • 3.10.2: /home/hmasu/.pyenv/versions/3.10.2/bin/python3
  • 3.9.8: /home/hmasu/.pyenv/versions/3.9.8/bin/python3.9
  • 3.8.5: /usr/bin/python3.8
  • 3.8.5: /usr/bin/python3
  • 3.8.5: /bin/python3.8
  • 3.8.5: /bin/python3
  • 3.7.12: /home/hmasu/.pyenv/versions/3.7.12/bin/python3.7
  • 2.7.18: /usr/bin/python2
  • 2.7.18: /usr/bin/python2.7
  • 2.7.18: /bin/python2
  • 2.7.18: /bin/python2.7

PEP 508 Information:

{'implementation_name': 'cpython',
 'implementation_version': '3.10.2',
 'os_name': 'posix',
 'platform_machine': 'x86_64',
 'platform_python_implementation': 'CPython',
 'platform_release': '5.4.72-microsoft-standard-WSL2',
 'platform_system': 'Linux',
 'platform_version': '#1 SMP Wed Oct 28 23:40:43 UTC 2020',
 'python_full_version': '3.10.2',
 'python_version': '3.10',
 'sys_platform': 'linux'}

System environment variables:

  • SHELL
  • PYENV_HOOK_PATH
  • PYENV_SHELL
  • WSL_DISTRO_NAME
  • PYENV_VERSION
  • NAME
  • PWD
  • LOGNAME
  • HOME
  • LANG
  • WSL_INTEROP
  • PYENV_DIR
  • TERM
  • USER
  • DISPLAY
  • SHLVL
  • PYENV_ROOT
  • WSLENV
  • XDG_DATA_DIRS
  • PATH
  • HOSTTYPE
  • PIP_SHIMS_BASE_MODULE
  • PIP_DISABLE_PIP_VERSION_CHECK
  • PYTHONDONTWRITEBYTECODE
  • PIP_PYTHON_PATH
  • PYTHONFINDER_IGNORE_UNSUPPORTED

Pipenv–specific environment variables:

Debug–specific environment variables:

  • PATH: /home/hmasu/.pyenv/versions/3.10.2/bin:/home/hmasu/.pyenv/libexec:/home/hmasu/.pyenv/plugins/python-build/bin:/home/hmasu/.pyenv/shims:/home/hmasu/.pyenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/WindowsApps/CanonicalGroupLimited.Ubuntu20.04onWindows_2004.2022.8.0_x64__79rhkp1fndgsc:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.1/libnvvp:/mnt/c/windows/system32:/mnt/c/windows:/mnt/c/windows/System32/Wbem:/mnt/c/windows/System32/WindowsPowerShell/v1.0:/mnt/c/windows/System32/OpenSSH:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files/dotnet:/mnt/c/Program Files/NVIDIA Corporation/Nsight Compute 2019.4.0:/mnt/c/ProgramData/chocolatey/bin:/mnt/c/Program Files/nodejs:/mnt/c/Program Files/Microsoft SQL Server/130/Tools/Binn:/mnt/c/Program Files/Microsoft SQL Server/Client SDK/ODBC/170/Tools/Binn:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/mnt/c/Program Files/Azure Data Studio/bin:/mnt/c/WINDOWS/system32:/mnt/c/WINDOWS:/mnt/c/WINDOWS/System32/Wbem:/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0:/mnt/c/WINDOWS/System32/OpenSSH:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/ProgramData/DockerDesktop/version-bin:/mnt/c/Users/hmasu/.windows-build-tools/python27:/mnt/c/Users/hmasu/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/hmasu/bat:/mnt/c/tools/cuda/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/bin:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/extras/CUPTI/libx64:/mnt/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0/include:/mnt/c/Users/hmasu/AppData/Roaming/npm:/mnt/c/Users/hmasu/AppData/Local/Programs/Microsoft VS Code/bin:/mnt/c/Program Files/LLVM/bin:/mnt/c/Users/hmasu/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/hmasu/AppData/Local/Programs/Julia-1.6.0/bin:/snap/bin
  • SHELL: /bin/bash
  • LANG: C.UTF-8
  • PWD: /mnt/d/workspace/pipenv-.env-exp

Contents of Pipfile (‘/mnt/d/workspace/pipenv-.env-exp/Pipfile’):

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.10"

[scripts]
show-foo = "echo $FOO"

Contents of Pipfile.lock (‘/mnt/d/workspace/pipenv-.env-exp/Pipfile.lock’):

{
  "_meta": {
    "hash": {
      "sha256": "fedbd2ab7afd84cf16f128af0619749267b62277b4cb6989ef16d4bef6e4eef2"
    },
    "pipfile-spec": 6,
    "requires": {
      "python_version": "3.10"
    },
    "sources": [
      {
        "name": "pypi",
        "url": "https://pypi.org/simple",
        "verify_ssl": true
      }
    ]
  },
  "default": {},
  "develop": {}
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
matteiuscommented, Mar 23, 2022

2022.3.23 has been released!

1reaction
matteiuscommented, Mar 11, 2022

By the way, is the following result what you intended on issue-4975-env-variables-not-expanded?

Not really, I think the master branch worked without the quotes for the run example … that is kind of odd really. I’ll have to think about it more. EDIT: Actually @hmasdev funny story, I just tried it on main and it also required the quotes, ~~it is on my other branch that upgrades some other vendor’d dependencies that it seems to not require the quotes. So I think this is fine as is and the other issue you mentioned should be fixed when we eventually release: https://github.com/pypa/pipenv/pull/4969~~ Double EDIT: Actually my branch it also needs quotes, and git shell on windows needs quotes … I am not sure what I did at 3am.

Read more comments on GitHub >

github_iconTop Results From Across the Web

pipenv - Is it possible to use environment variables from “.env ...
env is only read during pipenv run and pipenv shell . It's not run during pipenv sync or pipenv install . I say...
Read more >
Advanced Usage of Pipenv - Python Packaging Authority
Pipenv will expand environment variables (if defined) in your Pipfile. ... To use Pipenv with a third-party Python distribution (e.g. Anaconda), ...
Read more >
Frequently Encountered Pipenv Problems - Read the Docs
Here are some common questions people have using Pipenv. ... you can set the environment variable PIPENV_PYTHON to $PYENV_ROOT/shims/python .
Read more >
pipenv Documentation - Read the Docs
Then you can run this script using pipenv run: $ pipenv run python main.py ... Pipenv will expand environment variables (if defined) in...
Read more >
Configure a Pipenv environment | PyCharm Documentation
Install Pipenv · Run the following command: $ py -m site --user-site. A sample output can be: · Replace site-packages with Scripts in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found