Python 3.6 Migration and Development Techniques

Upgrading Python from 2.7 to 3.6

Legacy virtual machines often default to Python 2.7. Verify the current installation:

which python
ls -lah /usr/bin/python

Install Python 3.6:

yum install epel-release
yum install python36
cd /usr/bin/
rm python
ln -s python3.6 python
python --version

After installation, Python 3.6 will be available at /usr/bin/python.

Array Operations

Printing All Array Elements

items = [1, 2, 3, 4, 5]
print(*items, sep='\n')

Converting 1D to 2D Arrays

Using NumPy to reshape arrays:

import numpy

flat_array = [1, 2, 3, 4, 5, 6]
columns = 2
rows = len(flat_array) // columns
two_dimensional = numpy.array(flat_array).reshape(columns, rows)

Multiprocessing Implementation

import time
import random
from multiprocessing import Process

class TaskExecutor(Process):
    def __init__(self, identifier):
        super().__init__()
        self.identifier = identifier
    
    def run(self):
        print(f'Process {self.identifier} starting')
        time.sleep(random.randint(1, 5))
        print(f'Process {self.identifier} completed')

# Create and start processes
processes = [
    TaskExecutor('worker1'),
    TaskExecutor('worker2'), 
    TaskExecutor('worker3'),
    TaskExecutor('worker4')
]

for proc in processes:
    proc.start()

for proc in processes:
    proc.join()

print('Main process finished')

Ansible Output Processing

Capture and parse Ansible command output:

import os

reference_hash = "b2 63 72 db 92 21 2d 32 5d 8f 20"

os.system("ansible windows -m win_shell -a 'certutil -hashfile D:\\\\app\\\\file.exe MD5' > output.txt")

line_count = 1
with open("output.txt", 'rb') as file:
    for content_line in file.readlines():
        if line_count % 5 == 1:
            components = content_line.split("|")
        if line_count % 5 == 3:
            if content_line.strip() != reference_hash:
                print(content_line)
                print(reference_hash)
                print(components[0])
        line_count += 1

Development Environment Setup

MySQL and MongoDB Dependencies

yum install mysql-devel gcc gcc-devel python3-devel
pip3 install mysql-connector
pip install pymongo==3.4.0

Version-Specific Package Installation

pip install package-name==specific-version

Python Enviroment Isolation

Python packages are user-specfiic. If user A can run Ansible but user B cannot, user B must install required packages:

pip install pywinrm

Useful Python Libraries

File Operations with shutil

import shutil
shutil.rmtree('/path/to/directory')  # Recursively removes directory

Git Integration

pip install gitpython

Configuration File Handling

Use configparser for INI file processing:

import configparser
config = configparser.ConfigParser()
config.read('config.ini')

Tags: python System Administration multiprocessing Ansible Database Development

Posted on Mon, 22 Jun 2026 16:29:24 +0000 by tskweb