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.
Start CentOS 7 on VirtualBox and try it.
** Host machine **
** Guest machine **
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
vagrant ssh
Log 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
python3
I felt uncomfortable when I typed it as a command, and I wanted an environment with multiple different packages as a development environment.venv
To use.
venv
Can prepare multiple environments with different packages.
pip
You 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
venv
Now 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)]
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.
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/
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 |
venv
However, 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
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']
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