How to install pyenv, pipenv

Why using pyenv and pipenv for Python?

Python has many versions of many environments. It has need one of them but too much hassle to choosing appropriate version of Python and modules. I want to operation easely.

pyenv : Easy to switch between some versions of Python.

pipenv : It can install modules separately of version.(use pyenv's version switch module inside.)

Memo

  • I using this settings on Raspberry Pi 4(64bit Kernel) and MacOS Catalina
  • Python 3(.8)

Contents

  • Install require components
  • Install pyenv
  • Install pipenv
  • How to use.

Install require components

(Raspberry Pi) >

sudo apt-get install -y 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 python-openssl git

< (Raspberry Pi)

(MacOS) >

brew install readline xz

< (MacOS)

Install pyenv

(Raspberry Pi)

First, install pyenv from CUI command.

$ curl https://pyenv.run | bash

When install finished, You'll see warnings like below.

WARNING: seems you still have not added 'pyenv' to the load path.

# Load pyenv automatically by adding
# the following to ~/.bashrc:

export PATH="/home/tmya/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Edit ~/.bashrc file and add three rows.

$ micro ~/.bashrc
export PATH="/home/tmya/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Reload .bashrc file.

$ source ~/.bashrc

< (Raspberry Pi)

(MacOS)

$ brew install pyenv

< (MacOS)

Install pipenv.

$ python -m pip install pipenv

or

$ python3 -m pip install pipenv

How to use

Install Python 3.8.2 or what you need.

$ pyenv install 3.8.2

Create a local environment.

First check the default Python version

$ python --version
Python 3.7.3
$ mkdir mypy
$ cd mypy
$ pipenv install --python 3.8.2

Creating a virtualenv for this project…
Pipfile: /home/tmya/mypy/Pipfile
Using /home/tmya/.pyenv/versions/3.8.2/bin/python3.8 (3.8.2) to create virtualenv…
⠦ Creating virtual environment...created virtual environment CPython3.8.2.final.0-32 in 1015ms
  creator CPython3Posix(dest=/home/tmya/.local/share/virtualenvs/mypy-udRPw8Ka, clear=False, global=False)
  seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/home/tmya/.local/share/virtualenv/seed-app-data/v1.0.1)
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

✔ Successfully created virtual environment! 
Virtualenv location: /home/tmya/.local/share/virtualenvs/mypy-udRPw8Ka
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (db4242)!
Installing dependencies from Pipfile.lock (db4242)  🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0  00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

You can see your specified version of Python.

$ pipenv shell
~~~
$ python --version
Python 3.8.2

If you want to exit local environment, type 'exit' command.

$ exit

Install library

You can not use 'pip' command. You should use 'pipenv' command instead.

$ pipenv install beautifulsoup4

Optional settings

How to create a virtual environment in the project directory. This setting is recommended for git and other version-control software.

$ micro ~/.bashrc

export PIPENV_VENV_IN_PROJECT=1

$ source ~/.bashrc
By @Akio Tomita in
Tags :