How Do You Install Python 3.x on Any Type of Linux?

Problem scenario
You want a script to install Python 3.x that will work on any type of Linux. What should you do?

Solution
Prerequisite

Install make. If you need assistance, see this posting.

Install the zlib development tools. If you have a Red Hat derivative (e.g., CentOS or Fedora), you this command: sudo yum -y install zlib-devel

If you have a Debian/Ubuntu system,

How Do You Read a JSON File to Extract the Value of a Given Key in a JSON File?

Problem scenario
You want to print the value of a given non-nested key in a JSON value. How can Python help you achieve this?

Solution
Here is an illustrative example. Create a file called reader.py with the following lines of code:

import json
with open(“first.json”, “r”) as reading_content:
i = reading_content.read()
j = json.loads(i)
print(j[“bird”])

Have the JSON file look like this:

cat first.json

{“bird”: {“name”: “singy”, …

How Do You Create a Class That Is an Iterator in Python?

Problem scenario
You want to implement an object that is an iterator in Python. What do you do to implement iterator behavior in the class?

Solution
Run this program as an example:

class Computation:

def __init__(self, max = 0):
self.max = max

def __iter__(self):
self.n = 0
return self

def __next__(self):
if self.n <= self.max:
product = 2 * self.n
self.n += 1
return product

objecta = Computation(4) # The number must be 3 or greater if you want to print something other than “None” with this program. …

How Do You Extract an Email Address from a CSV File?

Problem scenario
You have a CSV file with email addresses. You want to return only the email address — not lines that have an email address. What do you do?

Solution
Assuming the email addresses are in a file named email.txt, use this Python 3 program:

with open(’email.txt’, ‘r’) as a:
for b in a:
if ‘@’ in b: #operate only on lines with “@”
c = b.split() #split up words on line
for d in c: #iterate through characters of words
if ‘@’ in d: #if email address, …

How Do You Solve “env: ‘python’: Permission denied”?

Problem scenario
You are running a script and you receive env: “‘python’: Permission denied”

Solution
Run this: sudo ls -lhd $(which python)

Look at the permissions, file owner and group associated with the results. Consider the user that brings about the original error. You may need to change the permissions of the file.

Is the file link pointing to a directory?

How Do You Troubleshoot the Hadoop Message “Exception in thread “main” java.nio.file.AccessDeniedException: /home/jdoe/./mapper.py”?

Problem scenario
You are trying to run a hadoop command (to kick off a mapreduce job). But you get this error:
“Exception in thread “main” java.nio.file.AccessDeniedException: /home/jdoe/./mapper.py”

What should you do?

Solution (short version)
Change to a directory where the user can write files to. Retry the command.

Solution (long version)
Create a directory that is owned by the user and the group associated with the user that is running this command.

How Do You Change the Version of Python that Ansible Uses?

Problem scenario
You are using Ansible with -vvv to see what Python version it is using. (Or you use ansible –version.) You see an incorrect version of Python being used.

You tried ansible_python_interpreter=/usr/bin/python3 in your playbook and in the ansible.cfg file. Neither worked.

What should you do?

Solution
In the playbook, find the hosts stanza. Underneath it use this (where python3 is the version you want and “/usr/bin” is the path to it):

vars:
ansible_python_interpreter: /usr/bin/python3 …

How Do You Troubleshoot Ansible Errors about SELinux?

Problem scenario
You have Python 3 installed, but you do not have pip3 installed. One of the following also apply to your situation:

Problem scenario #1
You run an Ansible playbook. You receive the error message “Aborting, target uses selinux but python bindings (libselinux-python) aren’t installed.”

Problem scenario #2
You run an Ansible playbook.

How Do You Get the libselinux-python to Work with Python 3?

Problem scenario
You have Python 2 and Python 3 installed. When you run Python 3 programs, you get an error message about selinux. The message is consistent with libselinux-python not being installed.

When you enter the Python interpreter for Python 2, you can run this command without errors: import selinux

But when you enter the Python interpreter for Python 3 (e.g., python3), you get errors when you run this command: import selinux

Security of the server is not critical,