Python3, venv and Ansible

Introduction

It's been almost a year since the support for Python2 has ended. On the server I use on a regular basis, the script is still running on the 2nd system without any problems ... I thought that it would be time to use the 3rd system as well, so I tried to summarize the contents I investigated this time. As the title suggests, `venv``` for multiple environments and `Ansible``` for building an infrastructure environment will be explained as examples.

Environmental composition

Start CentOS 7 on VirtualBox and try it.

** Host machine **

** Guest machine **

VM boot

Start with `` `Vagrantfilebelow.vagrant up``` !

Vagrantfile


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.define "centos" do |server|
    server.vm.box = "centos/7"
    server.vm.box_version = "2004.01"
    server.vm.hostname = "centos"
    
    server.vm.provider "virtualbox" do |vb|
      vb.customize [
        "modifyvm", :id,
        "--memory", "4096",
        "--cpus", "2"
      ]
    end
  end
end

Python3 installation

vagrant sshLog in immediately and install python3. Before that, check your existing python environment.

$ python -V
Python 2.7.5

It's pretty old ... Now let's install Python3.

$ sudo yum install python3

#The path doesn't change, so the python command remains system 2
$ python -V
Python 2.7.5

#python3 command for 3 series
$ python3 -V
Python 3.6.8

use venv

python3I felt uncomfortable when I typed it as a command, and I wanted an environment with multiple different packages as a development environment.venvTo use. venvCan prepare multiple environments with different packages. pipYou can prepare an environment where the package to be installed by the command and the version of the package are different, but the limitation is that the version of python is fixed in the environment.

Since it is available as a standard function from python 3.3, I will try it immediately.

#Create directory for testing
$ mkdir ansible-2.9
$ cd ansible-2.9

#Environment creation
#New environment (using the venv module.venv) created
$ python3 -m venv .venv

#A directory for environment information is created with the environment name
#This time.It is a hidden folder because it is a folder with
$ ls
$ ls -a
.  ..  .venv

#Run venv to turn on the virtual environment.
$ source .venv/bin/activate
(.venv) $

Or even below.
$ . .venv/bin/activate

#Confirm that it is 3 system by executing python
(.venv) $ python -V
Python 3.6.8

#Turn off the virtual environment.
(.venv) $ deactivate
$ python -V
Python 2.7.5

Install and launch Ansible (2.9)

venvNow that is available, I'll put the package in. This time, it is Ansible used in the environment configuration of the infrastructure. I will try to introduce the version of 2.9 / 2.10.

#Turn on virtual environment with venv
$ pwd
/home/vagrant/ansible-2.9

$ . .venv/bin/activate
(.venv) $ pip install ansible==2.9.16
・ ・ ・
(.venv) $ ansible --version
ansible 2.9.16
  config file = None
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vagrant/ansible-2.9/.venv/lib64/python3.6/site-packages/ansible
  executable location = /home/vagrant/ansible-2.9/.venv/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Install and launch Ansible (2.10)

Now that we have an environment for Ansible 2.9, we will create another environment and prepare an environment for Ansible 2.10.

#Turn off virtual environment
(.venv) $ deactivate

# Ansible 2.Create an environment for 10
$ cd ../
$ mkdir ansible-2.10
$ cd ansible-2.10
$ python3 -m venv .venv
$ . .venv/bin/activate

#Make sure ansible is not installed
(.venv) $ ansible --version
-bash: ansible: command not found

# ansible 2.10 installations
(.venv) $ pip install ansible==2.10.4
(.venv) $ ansible --version
ansible 2.10.4
  config file = None
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/vagrant/ansible-2.10/.venv/lib64/python3.6/site-packages/ansible
  executable location = /home/vagrant/ansible-2.10/.venv/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

We were able to prepare an environment with different packages for each directory.

Summary

This time I prepared the development environment around python. I hope it will be helpful for those who have been tired of coexisting with python2.

The following is an article that I have referred to (thanks).

venv: Python virtual environment management https://qiita.com/fiftystorm36/items/b2fd47cf32c7694adc2e

Package development with Python https://future-architect.github.io/articles/20200820/

Supplement 1

The support deadline for Python3 is as follows. Some versions have expired, and some are about to expire ... I didn't know. 3.6 will be next year ...

version Release date Support deadline
3.5 2015/09/13 2020/09
3.6 2016/12/23 2021/12
3.7 2018/06/27 2023/06
3.8 2019/10/14 2024/10

Supplement 2

venvHowever, please be careful about the amount of disk used because the package is retained in each environment. By the way, in this case, it is as follows.

#The unit is MB
$ du -ms *
356     ansible-2.10
144     ansible-2.9

$ du -ms ansible-2.10/.venv/lib/python3.6/site-packages/ansible*
10      ansible-2.10/.venv/lib/python3.6/site-packages/ansible
6       ansible-2.10/.venv/lib/python3.6/site-packages/ansible-2.10.4-py3.6.egg-info
1       ansible-2.10/.venv/lib/python3.6/site-packages/ansible_base-2.10.4-py3.6.egg-info
314     ansible-2.10/.venv/lib/python3.6/site-packages/ansible_collections
3       ansible-2.10/.venv/lib/python3.6/site-packages/ansible_test

Supplement 3

I tried to run `venv``` on the `` ubuntu 18.04``` Box used by Vagrant, but I couldn't. It may not be included depending on the distribution.

$ python3 -V
Python 3.6.9

$ python3 -m venv .venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/vagrant/test/.venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

Supplement 4

Currently (2020/12/26), it seems that `` `pip search``` cannot be used. Suddenly I made an error and thought that the environment became strange, but there are various things in the world ... https://github.com/pypa/pip/issues/5216

Recommended Posts

Python3, venv and Ansible
python with pyenv and venv
venv (Python)
[python] Compress and decompress
Python and numpy tips
[Python] pip and wheel
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
python input and output
Python and Ruby split
Python asyncio and ContextVar
Python: Creating a virtual environment (venv), starting and stopping
Building and enabling a python virtual environment, etc. (venv)
Programming with Python and Tkinter
Encryption and decryption with Python
Python: Class and instance variables
3-3, Python strings and character codes
Python 2 series and 3 series (Anaconda edition)
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Python indentation and string format
Install Python and Flask (Windows 10)
About python objects and classes
About Python variables and objects
Apache mod_auth_tkt and Python AuthTkt
Å (Ongustromu) and NFC @ Python
About the Python module venv
Understand Python packages and modules
# 2 [python3] Separation and comment out
Python shallow copy and deep copy
Python and ruby slice memo
Python installation and basic grammar
I compared Java and Python!
Python shallow and deep copy
About Python, len () and randint ()
About Python datetime and timezone
Install Python 3.7 and Django 3.0 (CentOS)
Python environment construction and TensorFlow
Ruby and Python syntax ~ branch ~
[Python] Python and security-① What is Python?
Stack and Queue in Python
python metaclass and sqlalchemy declareative
Fibonacci and prime implementations (python)
Python bitwise operator and OR
Python debug and test module
venv: Python virtual environment management
Python list and tuples and commas
Python list comprehensions and generators
Unittest and CI in Python
Maxout description and implementation (Python)
[python] Get quotient and remainder
Python 3 sorted and comparison functions
[Python] Depth-first search and breadth-first search
python standard virtual environment venv
Identity and equivalence Python is and ==
Source installation and installation of Python
Python or and and operator trap
Challenge Python3 and Selenium Webdriver
py, shebang, venv and me