Search code examples
pythonfabricpipenv

Running python script with fabric and pipenv


My project is built on python 3 and I want to use Fabric to automate several scripts. I'm using pipenv for deps management. But I can't manage to run Fabric command using python 3. Here is a simplified script with my issue.

from fabric.tasks import task

PIPENV = '/home/viktor/.local/bin/pipenv'
PROJECT_PATH = '/home/viktor/dev/fabric'

@task
def test1(c):        
    c.run('python --version')

@task
def test2(c):
    with c.cd(PROJECT_PATH):
        c.run(f'{PIPENV} run python --version')

Examples:

viktor@pro:~/dev/fabric$ pipenv run python --version
Python 3.7.5

viktor@pro:~/dev/fabric$ pipenv run fab test1
Python 2.7.17

viktor@pro:~/dev/fabric$ pipenv run fab test2
Warning: the which -a system utility is required for Pipenv to find Python installations properly.
  Please install it.
Error: the command python could not be found within PATH or Pipfile's [scripts].
viktor@pro:~/dev/fabric$ 

which utility is installed in the system

And I couldn't find any info about PATH settings in Pipfiles

Any ideas?

UPDATE

I was able to do it with this ugly workaround:

@task
def test3(c):
    c.run(f'source $({PIPENV} --venv)/bin/activate && python --version')


>> viktor@pro:~/dev/fabric$ pipenv run fab test3
>> Python 3.7.5

It works but I wonder if there is a better way to do it


Solution

  • I don't see you specifying the remote host to which you're connecting, but whatever that host is, it can't find which in the remote path. I'd check your shell environment on the remote side where you've got pipenv installed to make sure the PATH is correct.