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

Post install package for every virtualenv created #56

Closed
DanyC97 opened this issue Nov 30, 2014 · 22 comments
Closed

Post install package for every virtualenv created #56

DanyC97 opened this issue Nov 30, 2014 · 22 comments

Comments

@DanyC97
Copy link

DanyC97 commented Nov 30, 2014

Is it possible to have a post install hook so that every time a new virtualenv is created a package is installed too?

The use case is simple: i'd like - in the same way pip is installed - to have iPython installed automatically with every pyenv virtualenv created.

@yyuu
Copy link
Collaborator

yyuu commented Nov 30, 2014

There are before_virtualenv and after_virtualenv hooks in pyenv-virtualenv. pyenv will call those hooks if you setup them. See details for Authoring plugins.

https://github.com/yyuu/pyenv-virtualenv/blob/ad644e83b586b5c922f93adeb77197ef13108fb6/bin/pyenv-virtualenv#L362

@yyuu yyuu closed this as completed Nov 30, 2014
@DanyC97
Copy link
Author

DanyC97 commented Dec 1, 2014

Thanks for reply.

I've setup them but i think something else is missing or i'm doing something wrong.

The setup i done is:
created the path $PYENV_ROOT/plugins/*/etc/pyenv.d

cat /usr/local/opt/pyenv/plugins/pyenv-virtualenv/etc/pyenv.d/exec/virtualenv.bash
after_virtualenv 'echo after: Dani'

However when i create a virtualenv i get the expected error

pyenv virtualenv 2.7.8 dani2
[pyenv-exec:39] for script in '"${scripts[@]}"'
+ [pyenv-exec:40] source /usr/local/opt/pyenv/plugins/pyenv-virtualenv/etc/pyenv.d/exec/virtualenv.bash
++ [virtualenv.bash:1] after_virtualenv 'echo after: Dani'
/usr/local/opt/pyenv/plugins/pyenv-virtualenv/etc/pyenv.d/exec/virtualenv.bash: line 1: after_virtualenv: command not found

Any idea what am i missing?

I've looked up at your test and i can't see how your tests succeed.

@test "pyenv-virtualenv hooks" {
cat > "${HOOK_PATH}/virtualenv.bash" <<OUT
before_virtualenv 'echo before: \$VIRTUALENV_PATH'
after_virtualenv 'echo after: \$STATUS'
OUT
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
stub pyenv-prefix "echo '${PYENV_ROOT}/versions/3.2.1'"
stub pyenv-hooks "virtualenv : echo '$HOOK_PATH'/virtualenv.bash"
stub pyenv-exec "echo PYENV_VERSION=3.2.1 \"\$@\""
stub pyenv-rehash "echo rehashed"

Have you loaded any other plugin apart from default IFS=$'\n' scripts=(``pyenv-hooks virtualenv``) ?

Cheers,
Dani

@yyuu
Copy link
Collaborator

yyuu commented Dec 2, 2014

/usr/local/opt/pyenv/plugins/pyenv-virtualenv/etc/pyenv.d/exec/virtualenv.bash isn't the right place for hooks for pyenv-virtualenv. It'll be used as hooks for pyenv-exec. Please rename the path enclosing virtualenv.bash to virtualenv.

Here's an example.

% mkdir -p "$(pyenv root)/pyenv.d/virtualenv"
% cat <<EOS > "$(pyenv root)/pyenv.d/virtualenv/after.bash"
after_virtualenv 'echo after: Dani'
EOS
% pyenv virtualenv foo
Ignoring indexes: https://pypi.python.org/simple/
Requirement already satisfied (use --upgrade to upgrade): setuptools in /home/yyuu/.pyenv/versions/foo/lib/python3.4/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /home/yyuu/.pyenv/versions/foo/lib/python3.4/site-packages
Cleaning up...
after: Dani

@DanyC97
Copy link
Author

DanyC97 commented Dec 2, 2014

Okay however in this case the notes are not up to date, correct? Because i set it up for step 6).

Hooks are bash scripts named like HOOK_NAME/*.bash, where "HOOK_NAME" is one of:

    exec
    rehash
    which

Hooks are looked for in $PYENV_HOOK_PATH, which is composed of:

    1. $PYENV_HOOK_PATH (external value)
    2. $PYENV_ROOT/pyenv.d
    3. /usr/local/etc/pyenv.d
    4. /etc/pyenv.d
    5. /usr/lib/pyenv/hooks
    6. $PYENV_ROOT/plugins/*/etc/pyenv.d

@yyuu
Copy link
Collaborator

yyuu commented Dec 2, 2014

Umm. Sorry for complication.

By default, pyenv itself provides hooks for exec, rehash and which. In pyenv-virtualenv, it also provides hooks for virtualenv in addition to pyenv's hooks. Because pyenv-virtualenv is an individual project from pyenv, the hook of pyenv-virtualenv was not described in the wiki of pyenv.

I don't think it's outdated. But complicated :(

@DanyC97
Copy link
Author

DanyC97 commented Dec 2, 2014

Fyi i confirm following your suggestion everything works as expected.

Many thanks!

P.S - will be good if we can can make the notes a bit more clear

@robcowie
Copy link

robcowie commented Mar 1, 2015

@DanyC97 I'd also like to install a standard set of packages in new virtualenvs. Any chance you could post your hook file? I'm having issues getting it to work (specifically, how do I reference pip in the newly created virtualenv from the script?). Cheers.

@nayefc
Copy link

nayefc commented Mar 26, 2015

I am trying to do the same thing. Doing an after_virtualenv 'echo hi' works. However, when I try to pip install it doesn't really install anything. Basically, I want a set of packages to be installed with every virtualenv created. Is that possible?

@jawshooah
Copy link
Contributor

@robcowie @nayefc Are you looking for something similar to rbenv-default-gems, but for pyenv? If so, I'm thinking about writing a pyenv-default-pips plugin that should take care of this.

@robcowie
Copy link

I'm unfamiliar with that project but it certainly looks like what I want. If you start the project, let me know. Cheers.

@nayefc
Copy link

nayefc commented Mar 26, 2015

@jawshooah Yup exactly. For example, I need pylint and Jedi and jediepcserver installed for each virtualenv and I was hoping to automate that as part of the pyenv mode hooks.

@jawshooah
Copy link
Contributor

Just forked rbenv-default-gems, working on it now.

@nayefc
Copy link

nayefc commented Mar 26, 2015

@jawshooah That would be amazing please keep me posted!

@jawshooah
Copy link
Contributor

Boom.

@robcowie
Copy link

You're a legend. I'll give it a go shortly and report back.

@nayefc
Copy link

nayefc commented Mar 26, 2015

@jawshooah I'm trying to modify this to do it as I create a virtualenv, not when I install a version. I changed after_install to after_virtualenv but I still can't see the packages when I do pip freeze. Any ideas how to do such a modification?

@jawshooah
Copy link
Contributor

Ah, seems I misunderstood the problem. The plugin is meant to install default packages when you install a new Python version; I assumed that if you wanted default packages for virtualenvs, the simplest way to get them was to have default packages for the Python base.

The reason after_virtualenv doesn't work here is that the plugin itself runs as an after_install hook. I could add a duplicate of default-packages.bash to etc/pyenv.d/virtualenv and sed 's/after_install/after_virtualenv/', but it may be better to just create a separate pyenv-virtualenv-default-packages plugin.

@jawshooah
Copy link
Contributor

Turns out pyenv-virtualenv does not include packages in the base Python when creating a virtualenv, which is probably expected behavior, so my initial thinking was flawed from the start. I'll make the change.

@nayefc
Copy link

nayefc commented Mar 26, 2015

@jawshooah That's fine I can do it myself. How do I create a plugin that runs after a virtualenv is created? I don't see where in your code you explicitly make an "after_install" package except for saying "after_install" in the bash script.

@jawshooah
Copy link
Contributor

Until I make the change upstream, you can just copy etc/pyenv.d/install/default-packages.bash to etc/pyenv.d/virtualenv/ and change after_install to after_virtualenv and PYENV_VERSION to VIRTUALENV_NAME in the new file. Haven't tried it yet myself, but it should work.

Edit: Sorry, change VERSION_NAME to VIRTUALENV_NAME.

@jawshooah
Copy link
Contributor

I created an issue for this on my repo if you'd like to continue the conversation over there.

@DonDebonair
Copy link

For those coming here from Google that want to install and/or upgrade packages after creation of a new virtualenv, you can do so as follows:

Run the following commands:

mkdir -p "$(pyenv root)/pyenv.d/virtualenv" # this will create a directory to hold "hooks" for pyenv virtualenv
$EDITOR "$(pyenv root)/pyenv.d/virtualenv/after.bash" # open your favorite editor to edit a new hook script

In your newly opened editor add the following contents to after.bash:

after_virtualenv 'PYENV_VERSION="$VIRTUALENV_NAME" pyenv-exec pip install --upgrade pip setuptools wheel'

And you're done! Now any time you create a new virtualenv, it will upgrade pip, setuptools and wheel and/or install them.
If you want to install (for example) jupyter to each new virtualenv, you can just add it at the end of the above command

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

6 participants