Python of 3.5.3
is installed by default on Raspberry Pi,
I wanted to use Python with 3.6
or higher, so I'll use pyenv to switch versions.
$ uname -a
# Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux
$ lsb_release -a
# No LSB modules are available.
# Distributor ID: Raspbian
# Description: Raspbian GNU/Linux 9.11 (stretch)
# Release: 9.11
# Codename: stretch
First, install the required packages.
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install -y git openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev
Clone pyenv from github.
$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Add the following to .bash_profile
so that you can hit the command.
$ sudo vi ~/.bash_profile
.bash_profile
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Reload .bash_profile
.
$ source ~/.bash_profile
Installation is complete when the version is displayed with the following command.
$ pyenv --version
#pyenv 1.2.16-5-g7097f820
Let's display a list of versions that can be installed.
$ pyenv install --list
#Available versions:
# 2.1.3
# 2.2.3
# ...
# 3.6.0
# 3.6-dev
# ...
# stackless-3.4.7
# stackless-3.5.4
This time I will install 3.6.0
.
$ pyenv install 3.6.0
Let's actually change the version after the installation is completed.
First, check the version before the change.
$ python --version
#Python 2.7.13
$ python3 --version
#Python 3.5.3
Change the version.
$ pyenv global 3.6.0
The version has changed as below!
$ python --version
#Python 3.6.0
$ python3 --version
#Python 3.6.0
Set pyenv with the following command.
$ pyenv global x.x.x
By the way, you can set the scope by changing this global
part.
Scope of application | Use applications | |
---|---|---|
shell | Applies only to the shell you are currently using | Temporary use |
local | Apply to current directory | I want to set for each folder |
global | Applies to the whole | I want to change the overall default environment |
You can restore it by changing the x.x.x
part to system
.
Example
$ pyenv local 3.6.4
$ pyenv shell 2.7.6
$ pyenv global system
You can also see the list of installed versions with the following command.
$ pyenv versions
When I tried to install 3.7 series, I could not install it with the following error.
$ pyenv install 3.7.0
Downloading Python-3.7.0.tar.xz...
-> https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
Installing Python-3.7.0...
BUILD FAILED (Raspbian 9.11 using python-build 1.2.16-5-g7097f820)
Inspect or clean up the working tree at /tmp/python-build.20200213174102.2652
Results logged to /tmp/python-build.20200213174102.2652.log
Last 10 log lines:
File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/__init__.py", line 42, in <module>
File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/cmdoptions.py", line 16, in <module>
File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/index.py", line 25, in <module>
File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/download.py", line 39, in <module>
File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module>
File "/tmp/python-build.20200213174102.2652/Python-3.7.0/Lib/ctypes/__init__.py", line 7, in <module>
from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
Makefile:1122: recipe for target 'install' failed
make: *** [install] Error 1
By installing libffi-dev
, I was able to install 3.7 series as well.
$ sudo apt install libffi-dev
$ pyenv install 3.7.0
Today is up to here!
-Building an environment with pyenv and pyenv-virtualenv
Recommended Posts