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.

Fabric does not pass the environment to a local shell like Invoke does

See original GitHub issue

Consider this task definition, saved as both fabfile.py and tasks.py:

from invoke import task

@task
def make(c):
    c.run('env')

fab make outputs:

PWD=/tmp
SHLVL=1
_=/usr/bin/env

inv make outputs:

LC_ALL=en_US.UTF-8
NVM_DIR=/home/justinas/.nvm
LC_MEASUREMENT=en_US.UTF-8
LC_PAPER=en_US.UTF-8
LC_MONETARY=en_US.UTF-8
<...> (lots of stuff in my environment)

I initially found this when trying to use fpm which checks the PATH variable to check that the executables it requires to function exist:

from invoke import task

@task
def make(c):
    c.run('fpm -s dir -t rpm -C dist --name somename .')

This works with invoke, but not with fab.

justinas@js:/tmp$ fab make
{:timestamp=>"2018-05-11T13:11:26.218874+0300", :message=>"Need executable 'rpmbuild' to convert dir to rpm", :level=>:error}
justinas@js:/tmp$ inv make
{:timestamp=>"2018-05-11T13:11:29.967762+0300", :message=>"Created package", :path=>"somename-1.0-1.x86_64.rpm"}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
timonwebcommented, May 14, 2018

@justinas I ended up using run from invoke for local tasks, I import it like this:

from invoke import run as local

@task
def test(c):
    local('ls')
6reactions
aviddivinercommented, Oct 3, 2018

I just ran into this as well. I was doing c.run('make build') and none of my ENV pulled through (specifically GOPATH which caused the build to fail). Adding the replace_env=False fixed it.

Edit: I see according to the docs:

run.replace_env: True, instead of False, so that remote commands run with a ‘clean’, empty environment instead of inheriting a copy of the current process’ environment.

It was just a bit confusing to me since I was trying to run a local command.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set environment variables using Fabric - Stack Overflow
You can use fabric.context_managers.shell_env to export a variable to bash for your shell and all subshells spawned from it - but those ...
Read more >
The environment dictionary, env - Fabric documentation
Most of Fabric's behavior is controllable by modifying env variables, such as env.hosts (as seen in the tutorial). Other commonly-modified env vars include:....
Read more >
Fabric - Read the Docs
Invoke implements CLI parsing, task organization, and shell command execution (a generic framework plus specific implementation for local ...
Read more >
Fabric Automation - Peterlog
The Fabric library and executable are used for remote execution of tasks, just like the invoke lib are used for local tasks. Installation....
Read more >
Shell Variables - Learning the bash Shell, Second Edition [Book]
Some environment variables are predefined by the shell when you log in. ... whose value is the name of the script (i.e., the...
Read more >

github_iconTop Related Medium Post

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