Pipenv: guidance on how to use pipenv with ansible

Created on 17 May 2017  路  8Comments  路  Source: pypa/pipenv

I am trying to setup a server with ansible and use pipenv. I succeed in creating the virtual environment, but could you give examples of how to activate the environment and then run commands in it?

Most helpful comment

Oh, that wasn't quite right, since people check in the Pipfile.lock, here's a working one:

- name: Check if a virtualenv is already created
  ignore_errors: true
  command: "pipenv --venv"
  args:
    chdir: "{{ project_root }}"
  register: pipenv_venv_check_cmd
  changed_when:
    - ('No virtualenv' not in pipenv_venv_check_cmd.stderr)

- name: Run a pipenv install
  environment:
    LANG: "en_GB.UTF-8"
  command: "pipenv --bare install --dev"
  args:
    chdir: "{{ project_root }}"
  when:
    - ('No virtualenv' in pipenv_venv_check_cmd.stderr)

I'll stop with the noise now :)

All 8 comments

$ pipenv run <command>

Yeah, that I use. More in general I am a bit puzzled by what exactly determines which virtual environment is activated (part of that might be that I am not using pew so far). In my local setup it is the folder in which I run 'pipenv shell'.

For ansible then is this the way to go e.g.?:

  • name: run tileserver setup develop
    command: pipenv run python /home/usr/tileserver/tileserver/setup.py develop chdir={{ virtualenv_dir }}

Hey @musicformellons, the environment selected by pipenv shell is determined by the project you are in at the time. If you have a project my_project which has a Pipfile in it, you'll need to first navigate to that directory and then run pipenv shell. At this point anywhere you navigate to, unless you deactivate the environment, will run under that venv.

You can also perform the pipenv run command you've supplied above, it's just going to require you're in the project directory each time you run a command. Either approach will work depending on your needs.

Actually the above ansible task works! So that's cool.

The definition of 'project' is then probably a folder with Pipfile in it. My question now is: What is the best way to start a project, i.e. create a Pipfile? As I noticed that when I want to start a new project with 'pipenv install --three' it will create a new Pipfile only when it does not find an existing one closer to the root of the folder tree. As I already had a Pipfile closer at the root, it 'interferes' creating a new one... So you have to do it 'manually' then?

I noticed that you can set how deep pipenv looks for a Pipfile. So I set it to 1 folder deep, this works for me, but I am still curious as to what is 'best practice'.

@musicformellons, you're correct, by default pipenv treats a directory containing a Pipfile and all directories up to three levels deep as a "Project". This can be modified by the environment variable that you noted to better fit your needs.

pipenv install --three will create a project for you, although install isn't necessary for a new project, pipenv --three works too. Having nested projects is a paradigm we really haven't entertained yet, but I don't see anything necessarily wrong with it.

It sounds like what you're doing is pretty well thought out and in line with how we'd expect people to be doing. Let us know if you have any further questions, thanks!

Alright, I'm going to close this out, but let us know if there's more clarification needed. Thanks again for checking in @musicformellons!

Something that I was experimenting that seems to work is:

- name: Run a pipenv install
  command: "pipenv install --dev --deploy --python {{ python_version }}"
  args:
    creates: "{{ project_root }}/Pipfile.lock"

Where Ansible knows not to run the pipenv install because the Pipfile.lock exists.

Oh, that wasn't quite right, since people check in the Pipfile.lock, here's a working one:

- name: Check if a virtualenv is already created
  ignore_errors: true
  command: "pipenv --venv"
  args:
    chdir: "{{ project_root }}"
  register: pipenv_venv_check_cmd
  changed_when:
    - ('No virtualenv' not in pipenv_venv_check_cmd.stderr)

- name: Run a pipenv install
  environment:
    LANG: "en_GB.UTF-8"
  command: "pipenv --bare install --dev"
  args:
    chdir: "{{ project_root }}"
  when:
    - ('No virtualenv' in pipenv_venv_check_cmd.stderr)

I'll stop with the noise now :)

Was this page helpful?
0 / 5 - 0 ratings