Build a python environment for each directory with pyenv-virtualenv

I've been using Python 2.7 for a long time, but since the language processing script I got from a person used Python 3, I tried to switch to the 3.X system.

Before switching, [Check the difference between 2.X series and 3.X series](http://postd.cc/the-key-differences-between-python-2-7-x-and-python- 3-x-with-examples /), different from what I expected.

Which one should I continue to use after all? I thought, and when I tried various things, there was a description on the Official site that "Which version to use depends almost on what you want to do". Lol.

Therefore, I decided to use pyenv, which I hated without eating.

In such a situation, I borrowed the wisdom of the pioneers and left a memo when I introduced pyenv-virtualenv in my environment.

pyenv and virtualenv

environment

Installation

Install pyenv and pyenv-virtualenv using Homebrew. Install Homebrew from here.

#Install pyenv
$ brew install pyenv

# pyenv-Install virtualenv
$ brew install pyenv-virtualenv

After installation, add the following to .bash_profile.

.bash_profile


$ vim ~/.bash_profile

## Set path for pyenv
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
    export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
fi

Restart the terminal to reflect the settings, or type the following command.

$ source ~/.bash_profile

Installing python3 with pyenv

First check the current python version

$ python -V
Python 2.7.10

As mentioned at the beginning, it seems better to install it according to the purpose. This time, install 3.4.3 as well as the reference material.

#Create a folder and move it
$ mkdir ~/work/pyenv_test
$ cd ~/work/pyenv_test

# python3.4.Installation of 3
pyenv_test $ pyenv install 3.4.3
Downloading Python-3.4.3.tgz...
-> https://yyuu.github.io/pythons/4281ff86778db65892c05151d5de738d
Installing Python-3.4.3...
Installed Python-3.4.3 to /Users/XXXX/.pyenv/versions/3.4.3

# python3.4.Check if 3 has been introduced
pyenv_test $ pyenv versions
* system (set by /Users/XXXX/.pyenv/version)
  3.4.3
pyenv_test $ ls ~/.pyenv/versions
3.4.3

Python version setting by pyenv

At this rate, the original version of python is reflected.

$ pwd
/Users/XXXX/work/pyenv_test
$ python -V
Python 2.7.10

Therefore, use pyenv local NAME to reflect the downloaded python version in pyenv.

$ pwd
/Users/XXXX/work/pyenv_test
$ pyenv local 3.4.3
$ pyenv versions
  system
* 3.4.3 (set by /Users/XXXX/work/pyenv_test/.python-version)

Now, python3.4.3 installed by pyenv can be reflected in the pyenv_test directory.

Building a virtual environment with pyenv-virtualenv

In pyenv-virtualenv, create an environment to isolate the package to be installed.

pyenv_test $ pwd
/Users/XXXX/work/pyenv_test

#Execute the following command under the directory where you want to build the above environment
pyenv_test $ pyenv virtualenv 3.4.3 test
pyenv_test $ pyenv local test
pyenv-virtualenv: activate test

(test)$    # <-Set pyenv-virtualenv()Displayed in

(test)$ pyenv versions
  system
  3.4.3
* test (set by /Users/XXXX/work/pyenv_test/.python-version)

Now, every time you move to this directory, the test environment you built will be used automatically.

Package installation

Manage packages using pip. pip is installed at the same time as pyenv install NAME. I haven't done anything right now, so only the wheel is included.

(test) pyenv_test $ pip freeze
wheel==0.26.0

Install packages that you often use personally.

pip install numpy
pip install scipy
pip install pandas
pip install scikit-learn
pip install nltk
pip install matplotlib
pip install seaborn
pip install ipython
pip install jupyter

Some people seem to get an error with matplotlib or jupyter, but the installation is completed without any error.

Save the installed package

You can save the installed packages as a list. Note that you can enjoy it when installing the same package in another project.

#A command to list installed packages
$ pip freeze > mypckg.list

#Command to install the listed packages
$ pip install -r mypckg.list

Delete the built environment

The built environment can be deleted with pyenv uninstall NAME. This deletion method does not delete the .python-version file that contains the environment name generated when local is specified.

(test) pyenv_test $ pyenv versions
  system
  3.4.3
* test (set by /Users/XXXX/work/pyenv_test/.python-version)

(test) pyenv_test $ pyenv uninstall 3.4.3
(test) pyenv_test $ pyenv uninstall test
pyenv: remove /Users/XXXX/.pyenv/versions/test? [[Enter y]]
pyenv-virtualenv: deactivate test
pyenv: version `test' is not installed (set by /Users/XXXX/work/pyenv_test/.python-version)

pyenv_test $     # <- ()Lose
pyenv_test $ pyenv versions
 system

Summary

When building the environment from the next time onward, follow the flow below.

#python installation
$ pyenv install 3.4.3

#Environment
$ pyenv virtualenv 3.4.3 NAME

#Environment selection
$ pyenv local NAME
(NAME)$ pyenv versions
  system
  3.4.3
* NAME (set by /Users/XXXX/work/pyenv_test/.python-version)

#Package installation
(NAME)$ pip install -r mypckg.list

#When deleting the built environment
(NAME)$ pyenv uninstall NAME

reference

-Building multiple Python environments on the same system -Python version control in Mavericks and use pyenv Python instead of system -Python version control -Build pyenv and virtualenv environment on Mac OSX yosemite -Pyenv + virtualenv on Mac -Notes on pyenv virtualenv -How to use pyenv and virtualenv -A story about using pyenv, which I hated without eating, was too convenient to sit down -Building Python local development environment 2 (pyenv-virtualenv, pip usage)

Recommended Posts

Build a python environment for each directory with pyenv-virtualenv
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Build a python environment with ansible on centos6
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
Commands for creating a python3 environment with virtualenv
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
Build python3 environment with ubuntu 16.04
Build python environment with direnv
Build a Python environment offline
Create a directory with python
Building a Python environment for pyenv, pyenv-virtualenv, Anaconda (Miniconda)
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a machine learning application development environment with Python
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Django environment for Win10 (with virtual space)
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Build python virtual environment with virtualenv
Build Mysql + Python environment with docker
Create a virtual environment with Python!
Building a virtual environment with Python 3
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Build a python3 environment on CentOS7
[Memo] Build a development environment for Django + Nuxt.js with Docker
How to build a python2.7 series development environment with Vagrant
Build a python environment with pyenv (OS X El Capitan 10.11.3)
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
Build a python environment on CentOS 7.7 for your home server
[Pyenv] Building a python environment with ubuntu 16.04
Create a Python execution environment for Windows with VScode + Remote WSL
Building a Python3 environment with Amazon Linux2
Build an environment for Blender built-in Python
Easily build a development environment with Laragon
Build Jupyter Lab (Python) environment with Docker
Build and test a CI environment for multiple versions of Python
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a local development environment for Lambda + Python using Serverless Framework
Build a python environment on MacOS (Catallina)
Building a Windows 7 environment for getting started with machine learning with Python
Build a Tensorflow environment with Raspberry Pi [2020]
Build a Python environment on your Mac with Anaconda and PyCharm
Let's create a virtual environment for Python
Try using virtualenv, which can build a virtual environment for Python
Building a Python 3.6 environment with Windows + PowerShell
I want to build a Python environment
Build a Fast API environment with docker-compose
[Mac] Building a virtual environment for Python
Build Python environment with Anaconda on Mac
[Linux] Build a jenkins environment with Docker
Build a Python + OpenCV environment on Cloud9
Build a Python execution environment using GPU with GCP Compute engine
Building a Python development environment for AI development
Build AI / machine learning environment with Python
Create a USB boot Ubuntu with a Python environment for data analysis
[Linux] Build a Docker environment with Amazon Linux 2
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Prepare a Python virtual environment for your project with venv with VS Code