[PYTHON] Environment construction with pyenv and pyenv-virtualenv

A note on how to build your own environment

Make a note of it as you need it when working remotely The referenced site may be easier to understand The environment is mac OSX: Yosemite.

pyenv pyenv manages the python environment. You can create a python environment that is different from the standard environment.

Reference URL: How to use pyenv and virtualenv How to manage your Python environment with pyenv Try using pyenv and virtualenv

People who do not want to read explanations (knowledgeable people)

https://github.com/KodairaTomonori/Qiita/blob/master/shell/construct_pyenv.sh Copy this and run it to build the environment at home If you want to build other than home, you can do it by rewriting ~ / part to any path.

Make pyenv available

First, git clone

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv



 Then write the following in `~ / .bash_profile`. (About .bashrc, .bash_profile: [Really correct use of .bashrc and .bash_profile](http://qiita.com/magicant/items/d3bb7ea1192e63fba850)

export PYENV_ROOT="HOME/.pyenv" export PATH="PYENV_ROOT/bin:PATH" eval "(pyenv init -)"


 \ * 2020/03/17 Fixed: Double quart added

## Installation of each version of python
 You can install it with `pyenv install 3.5.0`. The version is optional.


#### **`install-version.`**

$ pyenv install 3.5.0 $ pyenv install 2.7.9

 If you make a mistake, change ʻinstall` to ʻuninstall` and execute.

## Switching python version
 You can switch the version with `pyenv global x.x.x` or` pyenv local x.x.x`.
 If you set it to `global`, it will be reflected in the whole, and if you set it to` local`, it will be reflected in the current directory. This reflection will continue even if you exit terminal, so it is very convenient to set it for each folder with `local`.

 Changes in that shell can be made with `shell`. If you use this, the environment will only be reflected in the shell you are currently using. Therefore, if you close the shell and then reopen it, or if you open multiple shells, the environment change will not be affected by others.

 Is it `shell` if you want to use it temporarily,` local` if you want to set it for each folder, or `global` if you want to change the overall default environment?

 You can restore it by changing the `x.x.x` part to` system`.


#### **`swich-python-version.`**

python --version Python 2.7.3

$ pyenv shell 2.7.9 $ python --version Python 2.7.9

$ pyenv shell 3.5.0 $ python --version Python 3.5.0

$ pyenv shell system $ python --version Python 2.7.3


## module installation
 The installed python (2.7.9 and 3.5.0) contains only the default module (pip freeze does not contain anything), so install it.
 To install a module, just install any module with `pip install` as usual.

```pip install xxxxx```

pyenv-virtualenv
 `pyenv` alone is convenient enough, but more convenient.
 `pyenv-virtualenv` is a plugin for pyenv to create different environments with the same (python) version.

## Installation method

#### **`$  git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv`**

Then write the settings to .bash_profile

eval "$(pyenv virtualenv-init -)"

Creating a new environment

To create a new python environment, set ``` pyenv virtualenv [version] `` to create a new environment calledvirtualenv-name. For [version]` here, specify the existing environment.

make_new.


pyenv virtualenv 3.5.0 new_3.5.0

Here, in the newly created environment, the modules installed by pip etc. in the environment created by pyenv cannot be used. If you want to use the one used in [version], add the option --system-site-package. Then, pass the path to the site-packages of the one built with pyenv.

In the example below, if you add --system-site-packages, ~ / .pyenv / versions / 3.5.0 / lib / python3.5 / site-packages will be added to the path.

sample_site-packages.


$ pyenv virtualenv 3.5.0 inde-3.5.0
$ pyenv virtualenv --system-site-packages 3.5.0 co-3.5.0
$ pyenv virtualenv --system-site-packages inde-3.5.0 co-inde-3.5.0

# sys.path Some parts are omitted.
$ python -c 'import sys; print(sys.path)'    #Here is 3.5.0
['', '~/.pyenv/versions/3.5.0/lib/python35.zip',..., '~/.pyenv/versions/3.5.0/lib/python3.5/site-packages']

(inde-3.5.0) $ python -c 'import sys; print(sys.path)'
['', '~/.pyenv/versions/3.5.0/lib/python35.zip', .... , '~/.pyenv/versions/inde-3.5.0/lib/python3.5/site-packages']

(co-3.5.0) $ python -c 'import sys; print(sys.path)'
['', '~/.pyenv/versions/3.5.0/lib/python35.zip', ... , '~/.pyenv/versions/co-3.5.0/lib/python3.5/site-packages', '~/.pyenv/versions/3.5.0/lib/python3.5/site-packages']

(co-inde-3.5.0)kodaira@fomalhaut:~$ python -c 'import sys; print(sys.path)'
['', '~/.pyenv/versions/co-inde-3.5.0/lib/python35.zip',  ... , '~/.pyenv/versions/co-inde-3.5.0/lib/python3.5/site-packages', '~/.pyenv/versions/3.5.0/lib/python3.5/site-packages']

If you want to install a module in another environment, you can write pip freeze somewhere and install it with pip install -r. (There seemed to be a way to copy it when doing pyenv virtualenv, but I couldn't find it, so someone ...)

pip_install.


---In the environment you want to copy----
$ pip freeze > pyp_list.txt
---After changing the environment----
$ pip install -r pyp_list.txt

Save and rebuild virtual environment

I think this is the best advantage of using pyenv-virtualenv. It's about saving the environment and recreating the same environment elsewhere.

First, install what you need (wheel)

install_wheel.


$ pyenv exec pip install wheel

Save information about modules installed with pip using wheel

Save to any directory with --wheel-dir = [dir] Above, I used the output of pip freeze as it is, but it seems better to use wheel.

save_module_info.


$ pyenv exec pip freeze > pyp_list.txt
$ pyenv exec pip wheel --wheel-dir=~/tmp/wheelhouse -r pyp_list.txt

Install the same thing using the wheel you made earlier when you created a new environment

By wheel, you can save the pre-built package, so you don't have to compile it every time. Reference URL: I'll write it somehow.

install_using_wheel.


$ pyenv virtualenv 3.5.0 tes-3.5
$ pyenv global tes-3.5
$ pip install -r pyp_list.txt --use-wheel --no-index --find-links=tmp/wheelhouse
$ pip freeze

Summary

You can build an environment by using pyenv and pyenv-virtualenv. You can save the environment with'wheel'and reproduce the same environment elsewhere.

Please let me know if there is something wrong or something more convenient

Recommended Posts

Environment construction with pyenv and pyenv-virtualenv
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
Install pyenv and pyenv-virtualenv
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Build a virtual environment with pyenv and venv
Python virtual environment construction (2017 version) pyenv and pyenv-virtualenv and virtualenv and virtualenv wrapper and pyvenv and venv
ML environment construction with Miniconda
Python environment construction and TensorFlow
NumPy and matplotlib environment construction
Environment construction with anyenv + pyenv (migrate from pyenv only (Mac))
[Python] Django environment construction (pyenv + pyenv-virtualenv + Anaconda) for macOS
Build a numerical calculation environment with pyenv and miniconda3
Get started with Python! ~ ① Environment construction ~
ruby environment construction with aws EC2
Environment construction memo of pyenv + conda
Python environment construction (pyenv + poetry + pipx)
Automate environment construction with Shell Script
Using Chainer with CentOS7 [Environment construction]
Image recognition environment construction and basics
pytorch @ python3.8 environment construction with pipenv
Data science environment construction with Docker
Construction of Python local development environment Part 1 (pyenv, pyenv-virtualenv, pip installation)
Environment construction of Tensorflow and Chainer by Window with CUDA (with GPU)
LaTeX and R (a little Python) environment construction with SublimeText3 (Windows)
[Pyenv] Building a python environment with ubuntu 16.04
Data pipeline construction with Python and Luigi
Use smbus with python3 under pyenv environment
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
Python and machine learning environment construction (macOS)
Build a python virtual environment with pyenv
A memo packed with RADEX environment construction
Let's get along with Python # 0 (Environment construction)
DeepIE3D environment construction
Emacs-based environment construction
Linux environment construction
Python environment construction
Environment construction (python)
Realize environment construction for "Deep Learning from scratch" with docker and Vagrant
django environment construction
Build an environment with pyenv, pyenv-virtualenv, jupyter on OS X El Capitan
CodeIgniter environment construction
python environment construction
Python --Environment construction
Python environment construction
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Golang environment construction
python environment construction
Collecting information from Twitter with Python (Environment construction)
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
Recommended environment and usage when developing with Python
Building a python environment with virtualenv and direnv
Building an Anaconda environment for Python with pyenv
Declaratively manage your environment with Nix and home-manager
Installation of Python3 and Flask [Environment construction summary]
[0] TensorFlow-GPU environment construction built with Anaconda on Ubuntu
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
python development environment -use of pyenv and virtualenv-
[Django3] Environment construction and various settings summary [Python3]
Run OpenVino on macOS and pyenv and pipenv environment
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
Remove old pyenv environment on Mac and update