Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

guidance on how to use pipenv with ansible #363

Closed
musicformellons opened this issue May 17, 2017 · 8 comments
Closed

guidance on how to use pipenv with ansible #363

musicformellons opened this issue May 17, 2017 · 8 comments

Comments

@musicformellons
Copy link

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?

@kennethreitz
Copy link
Contributor

$ pipenv run <command>

@musicformellons
Copy link
Author

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 }}

@kennethreitz kennethreitz reopened this May 17, 2017
@nateprewitt
Copy link
Sponsor Member

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.

@musicformellons
Copy link
Author

musicformellons commented May 18, 2017

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'.

@nateprewitt
Copy link
Sponsor Member

@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!

@nateprewitt
Copy link
Sponsor Member

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

@decentral1se
Copy link

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.

@decentral1se
Copy link

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants