[PYTHON] Pyenv + virtualenv on Mac

After a clean install of Marvericks, I revamped the python environment. In the past, virtualenv + virtualenvwrapper was enough. It's convenient because you can switch the python environment for each directory.

And although some of the same information was already written by my seniors, I decided to use it for myself.

environment

pyenv

Installation

pyenv only

$ brew install pyenv

This is the end.

If you also use virtualenv as a plugin for pyenv

$ brew install pyenv-virtualenv

Just do this from the beginning and pyenv will come in with you.

Setting

As a precaution during installation, the following will be prompted as appropriate.

To enable shims and autocompletion add to your profile:
  if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

To use Homebrew's directories rather than ~/.pyenv add to your profile:
  export PYENV_ROOT=/usr/local/opt/pyenv

How to use

Create an environment

$ pyenv install 2.7.6

The specified version of python is installed. Of course with pip.

To check the list of installable versions

$ pyenv install -l

If you want to check the list of installed environments

$ pyenv versions

global: Common use of environment

I feel that this is personal or not. If you don't, just go to see python on the system side.

$ python --version;which python
Python 2.7.5
/usr/bin/python

$ pyenv global 2.7.6
$ exec $SHELL -l

$ python --version;which python
Python 2.7.6
/usr/local/opt/pyenv/shims/python

local: Apply any environment in any directory

Introduced version 3.3.3 environment.

$ pyenv install 3.3.3

Move to an arbitrary directory and specify the environment.

$ cd ~/Projects/sandbox/
$ pyenv local 3.3.3
$ pyenv version
3.3.3 (set by /Users/xxx/Projects/sandbox/.python-version)

Confirm that you are looking at global except for the specified directory.

$ cd
$ pyenv version
2.7.6 (set by /usr/local/opt/pyenv/version)

virtualenv Based on the environment for each version specified at the time of pyenv install, virtualenv newly duplicates the environment. This is convenient when you want to have the same version but different package configurations.

$ pyenv virtualenv 2.7.6 sandbox276

The environment created here is also included in pyenv versions. If you use pyenv local sandbox276, you can use the environment in any directory as well.

Delete environment

The environment for each version is the same as the virtualenv environment.

$ pyenv uninstall 3.3.3
$ pyenv uninstall sandbox276
$ pyenv versions
  system
* 2.7.6 (set by /usr/local/opt/pyenv/version)

However, if you specify local and create an environment in an arbitrary directory, a file called .python-version will be created and the environment name will be written there, but please note that it will not disappear.

Impressions

Vertically flexible version! Flexible sideways with virtualenv! Surely convenient!

reference

Recommended Posts

Pyenv + virtualenv on Mac
Install pyenv on mac
Pyenv on Mac OSX Mavericks
Catalina on Mac and pyenv
(Beginner) Notes on using pyenv on Mac
Python environment construction on Mac (pyenv, virtualenv, anaconda, ipython notebook)
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
pyenv + virtualenv + Anaconda
python on mac
Put Anaconda on your Mac using Pyenv
Notes on building Python and pyenv on Mac
Install Tensorflow on Mac
Install Ansible on Mac
Install Python on Mac
Install Python 3 on Mac
numba installation on mac
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Run OpenMVG on Mac
Install Python 3.4 on Mac
Remove old pyenv environment on Mac and update
Installing pyenv on ubuntu 16.04
Install Caffe on Mac
Install pyenv on OSX
Install mecab on mac
Install mecab-python on Mac
Try deepdream on Mac
Build a Python environment on your Mac using pyenv
Install Python environment on local PC (pyenv, venv on Mac)
Minimum memo when using Python on Mac (pyenv edition)
Install Numpy on virtualenv on Windows
Set-enable Python virtualenv on Windows
Notes on installing dlib on mac
Pyramid + mongodb environment on Mac
Install module on Anaconda (Mac)
Install OpenPose on mac (Catalina)
Notes on pyenv and Atom
Install numba on your Mac
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Launch local server on mac
Notes on installing Python on Mac
Install Django on your Mac
Mac + fish + pyenv + VSCode (April 2020)
Install pillow on Mac OSX 10.9
[Mac] Tips: Install pyquery on Mac [pyquery]
Use matplot libwidget on mac
Memo on Mac OS X
Notes on installing pipenv on Mac
Introduced pyenv on Raspberry Pi
[Python] Anaconda, pyenv, virtualenv, .bash_profile
Build a machine learning environment on mac (pyenv, deeplearning, opencv)
If python on mac goes missing
Install anaconda on Mac → Add Library
How to rebuild python environment from pyenv on Mac environment (El Capitan)
MQTT on Raspberry Pi and Mac
How to install mysql-connector-python on mac
Implement TensorFlow Lite on Mac [2019 Edition]
Steps to install matplotlib on Mac
Anaconda environment construction on Mac (2018 version)
Introducing Python using pyenv on Ubuntu 20.04