GithubHelp home page GithubHelp logo

Comments (22)

yyuu avatar yyuu commented on May 28, 2024 1

/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

from pyenv-virtualenv.

yyuu avatar yyuu commented on May 28, 2024

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

from pyenv-virtualenv.

DanyC97 avatar DanyC97 commented on May 28, 2024

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

from pyenv-virtualenv.

DanyC97 avatar DanyC97 commented on May 28, 2024

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

from pyenv-virtualenv.

yyuu avatar yyuu commented on May 28, 2024

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

from pyenv-virtualenv.

DanyC97 avatar DanyC97 commented on May 28, 2024

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

from pyenv-virtualenv.

robcowie avatar robcowie commented on May 28, 2024

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

from pyenv-virtualenv.

nayefc avatar nayefc commented on May 28, 2024

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?

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

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

from pyenv-virtualenv.

robcowie avatar robcowie commented on May 28, 2024

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

from pyenv-virtualenv.

nayefc avatar nayefc commented on May 28, 2024

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

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

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

from pyenv-virtualenv.

nayefc avatar nayefc commented on May 28, 2024

@jawshooah That would be amazing please keep me posted!

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

Boom.

from pyenv-virtualenv.

robcowie avatar robcowie commented on May 28, 2024

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

from pyenv-virtualenv.

nayefc avatar nayefc commented on May 28, 2024

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

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

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.

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

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.

from pyenv-virtualenv.

nayefc avatar nayefc commented on May 28, 2024

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

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

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.

from pyenv-virtualenv.

jawshooah avatar jawshooah commented on May 28, 2024

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

from pyenv-virtualenv.

DonDebonair avatar DonDebonair commented on May 28, 2024

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

from pyenv-virtualenv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.