Skip to content

How does the auto activation/ deactivation works? #52

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

Closed
DanyC97 opened this issue Oct 23, 2014 · 16 comments
Closed

How does the auto activation/ deactivation works? #52

DanyC97 opened this issue Oct 23, 2014 · 16 comments

Comments

@DanyC97
Copy link

DanyC97 commented Oct 23, 2014

I'm running Mac 10.9.5 and i did the installation as below:

brew install pyenv
eval "$(pyenv init -)" >> ~/.bashrc
brew install pyenv-virtualenv
eval "$(pyenv virtualenv-init -)" >> ~/.bashrc

The installation home is all under

pyenv versions
* system (set by /usr/local/opt/pyenv/version)
  3.4.2
  ra1

[dani@localhost in ~]# ll /usr/local/opt/pyenv*
lrwxr-xr-x  1 dani  admin    24B Oct 22 16:27 /usr/local/opt/pyenv -> ../Cellar/pyenv/20141012
lrwxr-xr-x  1 dani  admin    35B Oct 22 16:38 /usr/local/opt/pyenv-virtualenv -> ../Cellar/pyenv-virtualenv/20141012

I read the other issues - #47 , #48, #32 , #33 and i've followed the same steps as in #32 (i get this are the manual steps)

[dani@localhost in ~]# pyenv versions
* system (set by /usr/local/opt/pyenv/version)
  3.4.2
  ra1
[dani@localhost in ~]# python -V
Python 2.7.6
[dani@localhost in ~]# echo PYENV_ACTIVATED
PYENV_ACTIVATED
[dani@localhost in ~]# echo $PYENV_ACTIVATED

[dani@localhost in ~]# echo $PYENV_DEACTIVATED

[dani@localhost in ~]# echo $PYENV_VIRTUALENV_INIT
1
[dani@localhost in ~]# pyenv activate ra1
(ra1) [dani@localhost in ~]# python -V
Python 3.4.2
(ra1) [dani@localhost in ~]# echo $PYENV_ACTIVATED

(ra1) [dani@localhost in ~]# echo $PYENV_DEACTIVATED

(ra1) [dani@localhost in ~]# echo $PYENV_VERSION
ra1
(ra1) [dani@localhost in ~]# pyenv deactivate
[dani@localhost in ~]# echo $PYENV_VERSION
ra1
[dani@localhost in ~]# echo $PYENV_DEACTIVATED

[dani@localhost in ~]# echo $PYENV_ACTIVATED

[dani@localhost in ~]# pyenv version
ra1 (set by PYENV_VERSION environment variable)
[dani@localhost in ~]# pyenv versions
  system
  3.4.2
* ra1 (set by PYENV_VERSION environment variable)
[dani@localhost in ~]#

BUT because the above behavior, when i'm trying to uninstall the virtual env i get this error

[dani@localhost in ~]# pyenv uninstall ra1
pyenv: remove /usr/local/opt/pyenv/versions/ra1? y
[dani@localhost in ~]# pyenv versions
pyenv: version `ra1' is not installed
  system
  3.4.2

In #47 you mentioned,

As you mentioned, PYENV_DEACTIVATED should not persist after automatically triggered deactivation. I'll try to fix it in #47.

hence my question: _what is the working flow for the auto activation/ deactivation? Do i need to be inside the PYENV_ROOT in order for auto activation to kick in?_

In addition to this issue, according to the README file, the pyenv-virtualenv should be registered as a plugin inside pyenv, is this a bug in the brew formula's installation?

[dani@localhost in ~]# ll /usr/local/opt/pyenv/plugins/
total 8
lrwxr-xr-x  1 dani  admin    61B Oct 22 16:27 python-build -> /usr/local/Cellar/pyenv/20141012/default-plugins/python-build
@yyuu
Copy link
Collaborator

yyuu commented Oct 25, 2014

There are any references to ${PYENV_ACTIVATED} and ${PYENV_DEACTIVATED} in pyenv-virtualenv. It uses ${PYENV_ACTIVATE} and ${PYENV_DEACTIVATE} to store the state of activation. Please see also the relating source to understand the behaviour of the auto-activation.

https://github.com/yyuu/pyenv-virtualenv/blob/a02eb0a396f03b70bb6ed9d215cf298f471d8a49/bin/pyenv-virtualenv-init#L114
https://github.com/yyuu/pyenv-virtualenv/blob/a02eb0a396f03b70bb6ed9d215cf298f471d8a49/bin/pyenv-sh-activate#L112
https://github.com/yyuu/pyenv-virtualenv/blob/a02eb0a396f03b70bb6ed9d215cf298f471d8a49/bin/pyenv-sh-deactivate#L62

@yyuu
Copy link
Collaborator

yyuu commented Oct 25, 2014

Actually, brew's pyenv-virtualenv doesn't install as a plugin of pyenv, but it isn't matter. It is just simply installed into /usr/local (brew --prefix) and pyenv can find the plugin's executables if it is in $PATH. It is working expectedly for now.

@yyuu
Copy link
Collaborator

yyuu commented Oct 25, 2014

Here's some example how auto-activation works.

Let's say if you have 3 directories named foo, bar and baz. The foo uses CPython 2.7.8 as the local version inside the directory. The bar uses a virtual environment named venv27 as the local version. And, the baz uses a virtual environment named venv34 as the local version.

With this configuration, when you cd to the directory bar or baz, the pyenv will automatically activate the specified virtual environment into your shell.

% pyenv versions
  system
  2.7.8
* 3.4.2 (set by /home/yyuu/.pyenv/version)
  venv27
  venv34
% mkdir foo bar baz
% echo 2.7.8 > foo/.python-version
% echo venv27 > bar/.python-version
% echo venv34 > baz/.python-version
% cd foo
% python --version
Python 2.7.8
% cd -
~/
% cd bar
pyenv-virtualenv: activate venv27                                                                                                                                            
(venv27)% python --version
Python 2.7.8
(venv27)% cd -
~/
pyenv-virtualenv: deactivate venv27                                                                                                                                          
% cd baz
~/
pyenv-virtualenv: activate venv34                                                                                                                                            
(venv34)% python --version
Python 3.4.2
(venv34)% cd -
~/
pyenv-virtualenv: deactivate venv34

@DanyC97
Copy link
Author

DanyC97 commented Oct 25, 2014

Thanks for your reply. Now i get it - the notes are not clear at all with regards to how auto-activation works - i.e there is no mention that you can / should run pyenv local <version | virtualenv name > or the echo above.

Now i followed the below steps, which match yours but for some reason i can't get the line which indicate what steps took place

[dani@localhost in ~]# pyenv versions
* system (set by /usr/local/opt/pyenv/version)
  3.4.2
[dani@localhost in ~]# mkdir dani
[dani@localhost in ~]# cd dani/
[dani@localhost in ~/dani]# pyenv virtualenv 3.4.2 ra4
Ignoring indexes: https://pypi.python.org/simple/
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/var/lib/pyenv/versions/ra4/lib/python3.4/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /usr/local/var/lib/pyenv/versions/ra4/lib/python3.4/site-packages
Cleaning up...
[dani@localhost in ~/dani]# pyenv local ra4
[dani@localhost in ~/dani]# cat .python-version
ra4
[dani@localhost in ~/dani]# cd
[dani@localhost in ~]# pyenv versions
* system (set by /usr/local/opt/pyenv/version)
  3.4.2
  ra4
[dani@localhost in ~]# cd dani/
[dani@localhost in ~/dani]# pyenv versions
  system
  3.4.2
* ra4 (set by /Users/dani/dani/.python-version)
[dani@localhost in ~/dani]# python -V
Python 3.4.2
[dani@localhost in ~/dani]# cd
[dani@localhost in ~]# python -V
Python 2.7.6
[dani@localhost in ~]# pyenv versions
* system (set by /usr/local/opt/pyenv/version)
  3.4.2
  ra4
[dani@localhost in ~]#

As you can see when i go _inside_ dani directory, it gets auto activated but there is no line output as below to indicate if something happen

pyenv-virtualenv: activate ra4

nor having something like

(ra4)[dani@localhost in ~/dani]#

Any idea?

Thx,
Dani

@DanyC97
Copy link
Author

DanyC97 commented Oct 29, 2014

@yyuu - can you please help on the above question?

Maybe you can paste:

  • echo $PATH
  • if you using BASH, maybe you can say which version
  • is the PROMPT_COMMAND controlled by ${virtualenv}/bin/activate ? If yes, how it it called in the auto activation scenario?

Many thanks in advance

@yyuu
Copy link
Collaborator

yyuu commented Nov 2, 2014

I'm using GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu) on Debian GNU/Linux sid. pyenv and pyenv-virtualenv are installed from latest master on GH. Actually, my default shell is zsh (zsh 5.0.7 (x86_64-pc-linux-gnu)) and PATH is /usr/local/heroku/bin:/home/yyuu/.rbenv/shims:/home/yyuu/.rbenv/bin:/home/yyuu/.pyenv/shims:/home/yyuu/.pyenv/bin:/usr/lib/jvm/default-java/bin:/home/yyuu/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/local/sbin:/usr/sbin:/sbin.

pyenv / pyenv-virtualenv will do nothing about shell's prompt. During the auto-activation scenario, ${virtualenv}/bin/activate is called via pyenv-sh-activate and it will be modify user's shell prompt.

https://github.com/yyuu/pyenv-virtualenv/blob/58e93f9e0d7fa45e44bb2f13caff49668e1cad47/bin/pyenv-virtualenv-init#L123
https://github.com/yyuu/pyenv-virtualenv/blob/58e93f9e0d7fa45e44bb2f13caff49668e1cad47/bin/pyenv-sh-activate#L115

@yyuu
Copy link
Collaborator

yyuu commented Nov 3, 2014

FWIW, I pushed a runnable docker image yyuu/pyenv-virtualenv:issue52 at https://registry.hub.docker.com/u/yyuu/pyenv-virtualenv/ . You can try running pyenv-virtualenv with auto-activation feature from the image.

% docker run -it yyuu/pyenv-virtualenv:issue52 bash
root@d35f7bb54946:/# cat /dani/.python-version 
ra4
root@d35f7bb54946:/# pyenv versions
  3.4.2
  ra4
root@d35f7bb54946:/# cd /dani
pyenv-virtualenv: activate ra4
(ra4) root@d35f7bb54946:/dani# pyenv versions
  system
  3.4.2
* ra4 (set by /dani/.python-version)
(ra4) root@d35f7bb54946:/dani# python -V
Python 3.4.2
(ra4) root@d35f7bb54946:/dani# cd /
pyenv-virtualenv: deactivate ra4
root@d35f7bb54946:/# pyenv versions
  3.4.2
  ra4

The following is the Dockerfile I used to create the yyuu/pyenv-virtualenv:issue52.

FROM ubuntu:14.04
MAINTAINER Yamashita, Yuu <[email protected]>

ENV PATH /root/.pyenv/shims:/root/.pyenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get --quiet --yes update
RUN apt-get --quiet --yes upgrade
RUN apt-get --quiet --yes install build-essential curl git libbz2-dev libreadline-dev libsqlite3-dev libssl-dev patch zlib1g-dev

RUN git clone --quiet https://github.com/yyuu/pyenv.git /root/.pyenv
RUN cd /root/.pyenv && git reset --hard 35aed21
RUN git clone --quiet https://github.com/yyuu/pyenv-virtualenv.git /root/.pyenv/plugins/pyenv-virtualenv
RUN cd /root/.pyenv/plugins/pyenv-virtualenv && git reset --hard 58e93f9
RUN echo 'eval "$(pyenv init -)"' >> /root/.profile
RUN echo 'eval "$(pyenv virtualenv-init -)"' >> /root/.profile

RUN pyenv install 3.4.2
RUN pyenv rehash
RUN pyenv virtualenv 3.4.2 ra4
RUN mkdir /dani && cd /dani && pyenv local ra4

RUN echo 'exec /bin/bash -l "$@"' > /start && chmod 755 /start
ENTRYPOINT /start

@DanyC97
Copy link
Author

DanyC97 commented Nov 23, 2014

Thanks for the dockerfile. I don't get it how it doesn't work on my Mac. Anyway, for now i can live with the manual activation/ deactivation + shell unset.

Dani

@yyuu
Copy link
Collaborator

yyuu commented Nov 29, 2014

I'm using docker on OS X with boot2docker 😃 (honestly, usually I'm living on Debian GNU/Linux on VMware Fusion, though)

I suspect that there might be some configuration issue in your installation. If you found something, please let me know.

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

DanyC97 commented Nov 8, 2015

FYI before i upgraded to latest version (using git pull), the prompt was working when the virtualenv was activated.

After the upgrade to latest/ greatest my prompt is no longer shown after activation.

@yyuu
Copy link
Collaborator

yyuu commented Nov 8, 2015

It is expected behaviour after #104. Currently we're not depending activate script created by virtualenv and it will not change the users' shell prompt anymore.

@DanyC97
Copy link
Author

DanyC97 commented Nov 9, 2015

@yyuu while i do understand the work done to get anaconda env work better i'd appreciate if you add back the prompt to signal is active - otherwise is a massive killer from user experience pov.

@yyuu
Copy link
Collaborator

yyuu commented Nov 9, 2015

Hm, okay. I understood your request. I might revert them at least for non-fish. (I don't want to revert it for fish since the original code had some problems with oh-my-fish, as far as I remember)

@DanyC97
Copy link
Author

DanyC97 commented Nov 9, 2015

@yyuu or if you want you can add a flag which could be set in the .bashrc (same way you set auto activation) to behave as of now or the old way - i.e change prompt to reflect the virtualenv name

Thanks!

P.S - virtualenv project behavior does show/ change the prompt to reflect the activation

yyuu added a commit that referenced this issue Nov 9, 2015
@yyuu
Copy link
Collaborator

yyuu commented Nov 9, 2015

Pushed 3152718 to restore prompt for non-fish.

@DanyC97
Copy link
Author

DanyC97 commented Nov 9, 2015

Awesome, it works as expected, thank you!

P.S - suggestion: if in the future you planning to remove the prompt (w/o any flag to enable) please make a big note in the readme, in this way we can hold the upgrade ;)

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

2 participants