[Permanent preservation version] Put pyenv + venv in ubuntu [Don't hesitate anymore]

Introduction

Every time I set up a VM instance, I initialize it every time I die. I put pyenv + venv every time to keep the environment clean. ** Incorrect information ** will always appear in Qiita, so make a new note here.

What to do when you launch a VM

You have sudo rights to run pyenv and you need to run it first. (Source: here)

Initial execution


sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev

Put pyenv

pyenv is not a system-based management of python It is for managing packages for each personal directory.

The specific mechanism is to install a python related system under ~ / .pyenv and install it. Besides, it is a system that makes it read properly when you type a python command.

By the way, please note that it does not support the ** python2 ** command.

Install pyenv

** For Ubuntu **, just copy and enter the following.

input


git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bashrc
exec "$SHELL"

Neither yyuu / pyenv nor ~ / .bash_profile. ** ubuntu is this. ** ** I don't know Debian or CentOS. It is also written in Official. By the way, if you want to use Zsh, write it in ~ / .zshenv.

Put a new Python environment in pyenv

Put python


pyenv install 3.6.9
pyenv global 3.6.9
pip install --upgrade pip

This completes the individual environment python. You can love pip or anything.

Insert venv

venv is the official Python ** package management tool ** It manages modules to be inserted with pip for each folder.

venv


python -m venv ~/env/hoge

When you add something new with pip, it is clean and easy to manage if you put it in the venv state.

venv_activate


. ~/env/hoge/bin/activate

By doing so, you can build a venv environment. Now you can build each folder in a completely independent pip environment.

Difference between pyenv and venv

When using venv with a version of python made with pyenv, You will have a python version of the virtual environment created with pyenv.

So, if you do which python in the ʻactivate` environment, The folder specified by venv will appear.

In other words, pyenv is ** specialized in putting in your favorite python version **, venv is ** specialized in putting your favorite packages ** in the current Python version.

This is a big difference.

in conclusion

It's a shit scribble memo, but if you build on Ubuntu every time, it's convenient to have a bookmark or stock If you like it, I think it will be easier to convey the correct information, so I hope you can sympathize with me. Thank you very much.

Recommended Posts

[Permanent preservation version] Put pyenv + venv in ubuntu [Don't hesitate anymore]
(Not yet) Don't put Ubuntu 20.04 in WSL1