Install Python3 + OCI CLI on 7-series Linux & Python version supported by OCI CLI will be 3.5+ after 2020/2/13

1.First of all

** I updated the CLI thinking "I haven't updated the OCI CLI recently" **. When you run the CLI, a surprising message is displayed in red!

$ oci iam region list WARNING: OCI CLI now requires Python 3.5+. Please install or upgrade your version of Python to 3.5+ before February 13, 2020 to avoid interruption to CLI usage.

** If you think calmly, the support deadline for Python 2 is until the end of March 2020. ** **

** Python of Oracle Linux 7 is 2.x series. That's why I investigated the countermeasures. ** **

$ python -V Python 2.7.5

1-1. TL;DR

1-2. Target audience

1-3. Reference materials

** Japanese manual **

** English manual **

2. Consider building an OCI CLI environment

Python 3 is required to use the OCI CLI. However, Python's environment construction is diverse, and it is a difficult situation for users. Look back at the big picture and think about the installation policy.

2-1. How to install OCI CLI

Many instructions on how to install the OCI CLI are introduced in the above manual and the next Qiita article. There are some subtle differences, but they are about the same.

2-2. Python environment construction problem

The problem is building a Python environment. There are multiple ways to use Python 3 on Oracle Linux 7 / CentOS 7 in the first place, and there are also multiple ways to manage Python packages. As you can see in the next article, the situation is extremely complicated.

This time, let's think simply and narrow down under the following conditions.

Then I can think of the following method.

** Python 3.x is not included in the standard repository, so if you think "Software Collections" or "EPEL", according to the following news source, 7.7 or later (2019 / 8-) will be included in the standard repository. ** **

After checking, it is included in the standard repository (ol7_latest) as follows. In the next chapter, we will try to install using this.

$ sudo yum list available python3
Loaded plugins: langpacks, ulninfo
Available Packages
python3.i686                 3.6.8-10.0.1.el7                ol7_optional_latest
python3.x86_64               3.6.8-10.0.1.el7                ol7_latest ★ here

3. Python 3 environment construction

Install Python 3 on Oracle Linux 7. In addition, ** venv **, which is convenient for building a Python environment, will be explained.

The OCI CLI internally uses ** venv / virtulenv ** to manage the Python virtual environment. Therefore, the user does not need to use ** venv / virtulenv ** during installation. </ font>

3-1. Installation of Python 3

  1. Install Python 3.
$ sudo yum install -y python3
  1. When I check the version, it remains 2.7.5.
$ python -V
Python 2.7.5
  1. The reason is that the command is python3 as follows.
$ python3 -V
Python 3.6.8
  1. Check the symbolic link as follows.
$ ls -l /usr/bin/py*
-rwxr-xr-x.1 root root 78 December 5 10:34 /usr/bin/pydoc
lrwxrwxrwx.1 root root 8 December 24 16:08 /usr/bin/pydoc3 -> pydoc3.6
-rwxr-xr-x.1 root root 78 August 7 17:04 /usr/bin/pydoc3.6
lrwxrwxrwx.1 root root 7 December 24 13:08 /usr/bin/python -> python2
lrwxrwxrwx.1 root root 9 December 24 13:08 /usr/bin/python2 -> python2.7
-rwxr-xr-x.1 root root 7144 December 5 10:34 /usr/bin/python2.7
lrwxrwxrwx.1 root root 9 December 24 16:08 /usr/bin/python3 -> python3.6
-rwxr-xr-x.2 root root 11336 August 7 17:04 /usr/bin/python3.6
-rwxr-xr-x.2 root root 11336 August 7 17:04 /usr/bin/python3.6m
lrwxrwxrwx.1 root root 10 December 24 16:08 /usr/bin/pyvenv -> pyvenv-3.6
-rwxr-xr-x.1 root root 435 August 7 17:04 /usr/bin/pyvenv-3.6

** Caution ** Do not change the link destination of / usr / bin / python to python3 here. That's because many commands, including yum, depend on Python 2.x. Please be careful as troubles will continue if you replace the symbolic link. </ font>

  1. Check the contents of the rpm package as follows.
$ rpm -ql python3
/usr/bin/pydoc3
/usr/bin/pydoc3.6
/usr/bin/python3
/usr/bin/python3.6
/usr/bin/python3.6m
/usr/bin/pyvenv
/usr/bin/pyvenv-3.6
/usr/share/doc/python3-3.6.8
/usr/share/doc/python3-3.6.8/README.rst
/usr/share/licenses/python3-3.6.8
/usr/share/licenses/python3-3.6.8/LICENSE
/usr/share/man/man1/python3.1.gz
/usr/share/man/man1/python3.6.1.gz

3-2. Understanding Python 3 Virtual Environment

Python has many ways to manage multiple Pythons and Python packages on one machine. This time, I will introduce ** venv **, which is the most standard and also uses the OCI CLI installer. ** venv ** is a standard feature of the traditional ** virtualenv ** from Python 3.3.

I'm confused by the mixture of ** venv ** and ** pyvenv **, but both are the same ** venv **. pyvenv is a script that operates venv and is currently deprecated. https://docs.python.org/3.6/library/venv.html

The pyvenv script has been deprecated as of Python 3.6 in favor of using python3 -m venv to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on.

Here's how to use ** venv **:

** Creating a virtual environment **

python3 -m venv <environment path name>

** Enable virtual environment **

source <environment path name>/bin/activate

** Disable virtual environment **

deactivate

3-3. Python 3 Building a virtual environment

This work is not necessary for installing the OCI CLI, but it will be used in the investigation described later. </ font>

  1. Create a virtual environment for the OCI CLI. ʻOci_cli` is the name of the directory that builds the virtual environment, and may be another character string.
$ python3 -m venv oci_cli
  1. After checking, the following directories and files are created. You don't need to understand the contents, but if you want to know more details, please refer to Official Manual.
$ ls -d  oci_cli/*
oci_cli/bin  oci_cli/include  oci_cli/lib  oci_cli/lib64  oci_cli/pyvenv.cfg
  1. Enable the virtual environment. When the virtual environment is enabled, the virtual environment name is displayed at the beginning of the prompt. The part is the relative path from the current directory.
$ source oci_cli/bin/activate
(oci_cli) [opc@hostname ~]$
  1. When I check the Python version, it is 3.6.8.
(oci_cli) [opc@hostname ~]$ python -V
Python 3.6.8
  1. Enter deactivate to deactivate. You can see that disabling the virtual environment reverts to the original version.
(oci_cli) [opc@hostname ~]$ deactivate
[opc@hostname ~]$ python -V
Python 2.7.5

This is the basic knowledge of Python's virtual environment. Then install / update the OCI CLI.

4. OCI CLI setup

Then install the OCI CLI. You must have internet access to run the OCI CLI installer.

4-1. Installing the OCI CLI

Install the OCI CLI. Even if you are already using the OCI CLI with Python 2.x, you will need to reinstall the Python library because it is installed in a different location. However, the configuration file under $ HOME / .oci can be used as it is.

  1. Run the OCI CLI installer. In addition, execute it as an OS user who executes the OCI CLI, such as opc or oracle user.
$ bash -c "$(curl -L https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh)"
  1. When updating from an existing one, the following will be displayed. Enter "y" to overwrite it.
===> In what directory would you like to place the install? (leave blank to use '/home/opc/lib/oracle-cli'):
-- Install directory '/home/opc/lib/oracle-cli' is not empty and may contain a previous installation.

===> Remove this directory? (y/N):★ Enter y
  1. After that, you will be asked to enter some information, but you can use the "all return key".

  2. When finished, the output will be as follows.

-- ** Run `exec -l $SHELL` to restart your shell. **
--
-- Installation successful.
-- Run the CLI with /home/opc/bin/oci --help
  1. Restart the shell for the content added to $ HOME / .bashrc to take effect.
$ exec -l $SHELL
  1. After updating from Python 2, there are still lines that reference 2.7, like this: However, since it is not executed, there is no problem even if it is left.

$HOME/.bashrc


[[ -e "/home/opc/lib/oracle-cli/lib/python2.7/site-packages/oci_cli/bin/oci_auto
complete.sh" ]] && source "/home/opc/lib/oracle-cli/lib/python2.7/site-packages/
oci_cli/bin/oci_autocomplete.sh"

[[ -e "/home/opc/lib/oracle-cli/lib/python3.6/site-packages/oci_cli/bin/oci_auto
complete.sh" ]] && source "/home/opc/lib/oracle-cli/lib/python3.6/site-packages/
oci_cli/bin/oci_autocomplete.sh"
  1. Check the version of OCI CLI and it will be as follows.
$ oci --version
2.7.0

4-2. Configuration file settings

You don't have to do anything when updating from Python 2. The config file under $ HOME / .oci can be used as it is. If the installation is successful, the warning message introduced at the beginning will disappear even if you execute the OCI CLI.

For new installation, refer to the following page for setting.

5. Survey of installation environment

I explained the virtual environment of Python, so let's check it. The OCI CLI is installed in $ HOME / lib / oracle-cli.

  1. The OCI CLI is not installed in the default Python 3 environment.
$ pip3 list --format=columns
Package    Version
---------- -------
pip        9.0.3
setuptools 39.2.0
  1. Enable the virtual environment.
$ source lib/oracle-cli/bin/activate
(oracle-cli) [opc@hostname ~]$
  1. Check the packages installed in your virtual environment and you will see the OCI Python SDK and OCI CLI.
(oracle-cli) [opc@hostname ~]$ pip3 list
Package         Version
--------------- ----------
arrow           0.10.0
asn1crypto      1.2.0
certifi         2019.11.28
cffi            1.13.2
click           6.7
configparser    3.5.0
cryptography    2.4.2
idna            2.6
jmespath        0.9.3
oci             2.8.0 ★OCI Python SDK 
oci-cli         2.7.0 ★OCI CLI
pip             19.3.1
pycparser       2.19
pyOpenSSL       18.0.0
python-dateutil 2.7.3
pytz            2016.10
PyYAML          5.1.2
retrying        1.3.3
setuptools      42.0.2
six             1.11.0
terminaltables  3.1.0
wheel           0.33.6

** Bonus: ** You can also use pip instead of pip3 in a virtual environment. Both have the same output, but when you activate the virtual environment, you can use the pip installed in the virtual environment. There is no pip in / usr / bin.

$ find $HOME /usr/bin -name "pip*"
/home/opc/.cache/pip
/home/opc/lib/oracle-cli/lib/python3.6/site-packages/pip
/home/opc/lib/oracle-cli/lib/python3.6/site-packages/pip-19.3.1.dist-info
/home/opc/lib/oracle-cli/bin/pip
/home/opc/lib/oracle-cli/bin/pip3
/home/opc/lib/oracle-cli/bin/pip3.6
/home/opc/lib/oracle-cli/pip-selfcheck.json
/usr/bin/pip3 ★ There is no pip
/usr/bin/pip-3
/usr/bin/pip-3.6
/usr/bin/pip3.6
  1. Check if there are any packages that have been updated. If I just set up the OCI CLI, I don't see oci and oci-cli as shown below. Other packages have dependencies, so don't worry if they are old.
(oracle-cli) [opc@hostname ~]$ pip3 list -o
Package         Version Latest Type
--------------- ------- ------ -----
arrow           0.10.0  0.15.4 wheel
click           6.7     7.0    wheel
configparser    3.5.0   4.0.2  wheel
cryptography    2.4.2   2.8    wheel
idna            2.6     2.8    wheel
jmespath        0.9.3   0.9.4  wheel
pyOpenSSL       18.0.0  19.1.0 wheel
python-dateutil 2.7.3   2.8.1  wheel
pytz            2016.10 2019.3 wheel
PyYAML          5.1.2   5.2    sdist
six             1.11.0  1.13.0 wheel
  1. When a new version is released, you can update it with the following command.
(oracle-cli) [opc@hostname ~]$ pip3 install oci-cli --upgrade
  1. Disable the virtual environment and finish.
(oracle-cli) [opc@hostname ~]$ deactivate
[opc@hostname ~]$

6. OCI CLI update

Earlier I showed you how to update the OCI CLI with pip3 install oci-cli --upgrade. Actually, there are the following two update methods.

  1. Reinstall with the OCI CLI installer
  2. Update with pip / pip3 command

Since reinstallation is a single command, I will introduce the pip / pip3 method including the actual output.

  1. Change to the directory where the OCI CLI is installed.
$cd <installation directory>
  1. Enable the virtual environment and check if there is a new version. In this example, the new version is displayed.
source lib/oracle-cli/bin/activate
pip3 list -o
---Output here or below---
Package         Version Latest Type
--------------- ------- ------ -----
arrow           0.10.0  0.15.5 wheel
cffi            1.13.2  1.14.0 wheel
click           6.7     7.1.1  wheel
configparser    3.5.0   4.0.2  wheel
idna            2.6     2.9    wheel
jmespath        0.9.3   0.9.5  wheel
oci             2.10.2  2.12.1 wheel ★ There is a new version
oci-cli         2.9.0   2.9.7 wheel ★ There is a new version
pycparser       2.19    2.20   wheel
pyOpenSSL       18.0.0  19.1.0 wheel
python-dateutil 2.7.3   2.8.1  wheel
pytz            2016.10 2019.3 wheel
PyYAML          5.1.2   5.3.1  sdist
setuptools      45.1.0  46.1.1 wheel
six             1.11.0  1.14.0 wheel
  1. Update the package. Finally, when "Successfully installed" is displayed, the update is successful.
pip3 install oci-cli --upgrade

Part of the output


Collecting oci-cli
  Downloading oci_cli-2.9.7-py2.py3-none-any.whl (7.5 MB)
     |████████████████████████████████| 7.5 MB 6.1 MB/s
Collecting configparser>=4.0.2
  Downloading configparser-4.0.2-py2.py3-none-any.whl (22 kB)
Requirement already satisfied, skipping upgrade: terminaltables==3.1.0 in ./lib/oracle-cli/lib/python3.6/site-packages (from oci-cli) (3.1.0)
Requirement already satisfied, skipping upgrade: certifi in ./lib/oracle-cli/lib/python3.6/site-packages (from oci-cli) (2019.11.28)
Requirement already satisfied, skipping upgrade: PyYAML==5.1.2 in ./lib/oracle-cli/lib/python3.6/site-packages (from oci-cli) (5.1.2)
Requirement already satisfied, skipping upgrade: retrying==1.3.3 in ./lib/oracle-cli/lib/python3.6/site-packages (from oci-cli) (1.3.3)
Requirement already satisfied, skipping upgrade: idna<2.7,>=2.5 in ./lib/oracle-cli/lib/python3.6/site-packages (from oci-cli) (2.6)
Requirement already satisfied, skipping upgrade: pytz>=2016.10 in ./lib/oracle-cli/lib/python3.6/site-packages (from oci-cli) (2016.10)
Collecting oci==2.12.0
  Downloading oci-2.12.0-py2.py3-none-any.whl (3.6 MB)
     |████████████████████████████████| 3.6 MB 2.3 MB/s
★ Omitted
      Successfully uninstalled oci-cli-2.9.0
Successfully installed arrow-0.14.7 configparser-4.0.2 jmespath-0.9.4 oci-2.12.0 oci-cli-2.9.7 six-1.14.0
  1. Finally, deactivate the virtual environment and you're done.
(oracle-cli) [opc@hostname ~]$ deactivate
[opc@hostname ~]$

7. Summary

  • Starting February 13, 2020, the Python version supported by the OCI CLI will be 3.5+.
  • To use Python 3 on Oracle Linux / CentOS 7, the standard repository python3 is recommended.
  • You can both install and upgrade using the OCI CLI installer
  • OCI CLI is installed in Python virtual environment

Recommended Posts