[PYTHON] pyenv + pyenv-Automatically load the virtualenv environment in the virtualenv environment

Thing you want to do

For those who use python in the pyenv + pyenv-virtualenv environment. I want to automatically load .python-version and ʻactivate` when I move the directory.

Reason,

cd ~/foo pyenv activate foo

cd ~ pyenv deactivate

Because it is troublesome to activate and deactivate one by one.

If you are using direnv, use ʻeval" $ (direnv hook bash) " If it is described as follows in~ / .bashrc etc., every time you cd It seems that it hooks me, so I should be able to do it without usingdirenv. It was easy to do after a little research, so I posted it after a long time as a reminder (Maybe I should use direnv`, but I personally didn't get used to it)

correction: eval "$(pyenv virtualenv-init -)" And, if it is described in ~ / .bash_profile, it will switch automatically ... I intended to have described it above, but there was a case where the environment did not switch because the description position was wrong ...

Setting

It is assumed that the pyenv and pyenv-virtualenv environments have been set. Then add the following to .bash_profile etc. to .bash_profile etc.

cd(){
    now_path=`pwd`
    now_pyenv_filepath=$now_path/.python-version
    if [ -e ${now_pyenv_filepath} ];then
        now_pyenv=`cat ${now_pyenv_filepath}`
        pyenv deactivate
    fi

    new_pyenv_filepath=$1/.python-version

    if [ -e ${new_pyenv_filepath} ];then
        local_env=`cat ${new_pyenv_filepath}`
        pyenv activate ${local_env}
    fi

    builtin cd "$1"
}

If you write the above and reload it as source .bash_profile It will automatically deactivate and ʻactivate`.

First read the current location with pwd If there is .python-version there, the first half is deactivate

Furthermore, if there is .python-version in the destination folder Load it ʻactivate and finally load the built-in cd` I'm moving.

I couldn't call the built-in command with bulitin hoge.

reference

I referred to the following that I went through with shell hook.

Recommended Posts

pyenv + pyenv-Automatically load the virtualenv environment in the virtualenv environment
Use the latest pip in a virtualenv environment
Use Anaconda in pyenv environment
Byobu crashes in pyenv environment
pyenv, virtualenv Use docker to manage the environment without using
Django cannot be installed in the development environment of pipenv + pyenv
Load the remote Python SDK in IntelliJ
I tried putting virtualenv in Cygwin environment
[mac] Install R in pyenv + Jupyter-Lab environment
Install the package in an offline environment
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
How to pass the path to the library built with pyenv and virtualenv in PyCharm
Load the network modeled with Rhinoceros in Python ③
I want to use Python in the environment of pyenv + pipenv on Windows 10
I installed Pygame with Python 3.5.1 in the environment of pyenv on OS X
Install the python package in an offline environment
pyenv + virtualenv + Anaconda
Precautions when installing packages in the conda environment
Set a fixed IP in the Linux environment
python development environment -use of pyenv and virtualenv-
Load the network modeled with Rhinoceros in Python ②
Work in a virtual environment with Python virtualenv.
python in virtualenv
Matplotlib etc. cannot be imported in VirtualEnv environment
Load the network modeled with Rhinoceros in Python ①
Install LightGBM in an OS X virtualenv environment
Used from the introduction of Node.js in WSL environment
The story of building the fastest Linux environment in the world
The story of sharing the pyenv environment with multiple users
[For beginners] Install the package in the Anaconda environment (Janome)
Use the CASA Toolkit in your own Python environment
Load the module with the same name in another location
python memo (for myself): About the development environment virtualenv
Completely disable mouse acceleration in the Linux desktop environment
Pyenv + virtualenv on Mac
virtualenvwrapper in windows environment
virtual environment in python
pyenv + fish environment construction
Reflect the virtual environment created by virtualenv or conda in the IntelliJ IDEA (PyCharm) project (memo)
Development environment in Python
Dynamically load the module.
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
Display "Hello World" created in the local environment on the web
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
I tried the super-resolution algorithm "PULSE" in a Windows environment
Enable the virtualenv Python virtual environment for Visual Studio Code
Check the operation of Python for .NET in each environment
Django + MongoDB development environment maintenance (in the middle of writing)
Commands often used in the development environment during Python implementation
Minimum load test knowledge gained in the first API server load test
Reflect the virtual environment created with Miniconda in Jupyter notebook
A note on how to load a virtual environment in PyCharm
After enabling the python virtual environment in the batch file, run the python file