[Ubuntu 18.04] Python environment construction with pyenv + pipenv

Introduction

Create a virtual environment management environment (?) Using pyenv and pipenv. Version control of Python itself is performed by pyenv, and virtual environment construction and package management for each project are performed by pipenv.

environment

Installation

install.sh


#!/bin/bash

# Install dependencies
echo "User password" | sudo apt update && sudo apt install -y --no-install-recommends \
        build-essential \
        libffi-dev \
        libssl-dev \
        zlib1g-dev \
        libbz2-dev \
        libreadline-dev \
        libsqlite3-dev \
        git

# Download pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# Update .bash_profile
touch ~/.bash_profile
echo -e "# pyenv paths" >> ~/.bash_profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile
pyenv -v

# Install Python and set default
pyenv install 3.7.4
pyenv global 3.7.4

# Install pipenv
pip install pipenv

By executing this shell script as follows, pyenv / python / pipenv will be installed.

./install.sh

If you get angry that you don't have permission, use chmod + rx install.sh to give read / execute permission. Of course, there is no problem even if you hit the terminal line by line and execute it.

How to use

Create a project called hoge and start up a virtual environment of Python 3.6.

First install 3.6 version of Python with pyenv.

$ pyenv install 3.6.9

Next, create a virtual environment with pipenv.

$ mkdir hoge
$ cd hoge
$ pipenv install --python 3.6.9
Creating a virtualenv for this project…
Pipfile: /home/**/hoge/Pipfile
Using /home/**/.pyenv/versions/3.6.9/bin/python3 (3.6.9) to create virtualenv…

Python 3.6.9 installed with pyenv is used.

Success if there are Pipfile and Pipfile.lock under hoge /.

--Enable virtual environment

$ pipenv shell
(hoge) user:~/hoge$ 

--Install library in virtual environment

$ pipenv install numpy

Use pipenv install instead of pip install. If you accidentally pip install, it won't pollute the outside of the virtual environment, but be aware that the library is not managed by Pipfile / Pipfile.lock. ..

--Get out of the virtual environment

$ exit

--Delete virtual environment

$ pipenv --rm

Good point

--Easy to install & easy to configure --When you install the library with pipenv, Pipfile / Pipfile.lock is also updated automatically, so unlike requirements.txt, you will not forget to update it. ――It goes well with git --You can take over package management from requirements.txt with pipenv install -r requirements.txt

Bad place

--It is troublesome to enable / disable the virtual environment. The usability may be similar to virtualenv. --It looks like you're going to pip install by mistake. --It takes time to create Pipfile.lock

Bonus: Where to put the virtual environment

By default, virtual environments created with pipenv are placed under ~ / .local / share / virtualenvs /. pipenv --rm deletes this virtual environment, but forget about it. If you delete only the project directory, the virtual environment itself will remain.

To create a virtual environment directly under the project, it is necessary to define the environment variable $ PIPENV_VENV_IN_PROJECT.

echo -e "# pipenv property" >> ~/.bash_profile
echo 'export PIPENV_VENV_IN_PROJECT=1' >> ~/.bash_profile
source ~/.bash_profile

By doing this, the virtual environment .venv / will be placed directly under the project, and if you delete the project directory, the virtual environment will also be deleted.

Referenced site

--The story of installing pyenv on ubuntu 18.04 --Qiita --Use virtual environment anywhere with pipenv --Qiita

Recommended Posts

[Ubuntu 18.04] Python environment construction with pyenv + pipenv
pytorch @ python3.8 environment construction with pipenv
[Pyenv] Building a python environment with ubuntu 16.04
Build python environment with pyenv on EC2 (ubuntu)
Build python3 environment with ubuntu 16.04
Python 3.x environment construction by Pyenv (CentOS, Ubuntu)
Get started with Python! ~ ① Environment construction ~
Python environment construction (pyenv, anaconda, tensorflow)
Python environment construction (pyenv + poetry + pipx)
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
OpenCV3 & Python3 environment construction on Ubuntu
Environment construction with pyenv and pyenv-virtualenv
Python environment construction
Environment construction (python)
python environment construction
Python --Environment construction
Python environment construction
python environment construction
Use smbus with python3 under pyenv environment
Environment construction procedure: Ubuntu + Apache2 + Python + Pyramid
Build a python virtual environment with pyenv
Let's get along with Python # 0 (Environment construction)
python windows environment construction
homebrew python environment construction
Python development environment construction
python virtual environment Pipenv
Python environment with docker-compose
pyenv + fish environment construction
python2.7 development environment construction
Install python with pyenv
Mac environment construction Python
Virtual environment with Python 3.6
Python environment construction @ Win7
Python3 TensorFlow environment construction (Mac and pyenv virtualenv)
Collecting information from Twitter with Python (Environment construction)
Building an Anaconda environment for Python with pyenv
Use multiple versions of python environment with pyenv
[0] TensorFlow-GPU environment construction built with Anaconda on Ubuntu
Poetry-virtualenv environment construction with python of centos-sclo-rh ~ Notes
First python ① Environment construction with pythonbrew & Hello World !!
From Python environment construction to virtual environment construction with anaconda
[Ubuntu 18.04] Tensorflow 2.0.0-GPU environment construction
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Install Python environment with Anaconda
Manage python environment with virtualenv
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Python environment construction (Windows10 + Emacs)
Use Python installed with pyenv for PL / Python execution environment
CI environment construction ~ Python edition ~
Data analysis environment construction with Python (IPython notebook + Pandas)
ML environment construction with Miniconda
I made a Python3 environment on Ubuntu with direnv.
Python environment construction For Mac
Anaconda3 python environment construction procedure
Prepare python3 environment with Docker
Ubuntu 16.04 LTS, beginner memorandum of environment construction to switch anaconda version with pyenv
Build python environment with direnv
Python3 environment construction (for beginners)
Python environment construction and TensorFlow
Web application made with Python3.4 + Django (Part.1 Environment construction)