How Do You Troubleshoot “/bin/python3.6: bad interpreter: No such file or directory”?

Problem scenario
You are trying to run a pip command. But you get “/bin/python3.7: bad interpreter: Permission denied”. What should you do about these “bad interpreter” errors in response to having run pip commands?

Solution
Use “sudo” before the pip command. It should allow you to run the pip command. You may need to be a different user or obtain sudoer rights.

Why Would Boto3 Not Show a Peering Connection That the AWS CLI Shows?

Problem scenario
You have found an AWS CLI command that shows you output consistent with the console. You run this:

aws ec2 describe-vpc-peering-connection –region us-west-2 | grep pcx-abcd1234

The results show you a peering connection called pcx-abcd1234

You run this from a Python3 interpreter prompt:

import boto3
foo = boto3.resource(‘ec2’)
foo.describe_vpc_peering.connections()
print(foo)

You then search the output for pcx-abcd1234. You do not see it.

How Do You Troubleshoot the Error “Could not find suitable distribution for Requirement.parse(‘botocore==2.0.0dev11’)”?

Problem scenario
You try to install the AWS CLI version 2.x

You run this command:

$ sudo python3 setup.py install

You get a message like this:

Processing dependencies for awscli==2.0.7
Searching for botocore==2.0.0dev11
Reading https://pypi.org/simple/botocore/
No local packages or working download links found for botocore==2.0.0dev11
error: Could not find suitable distribution for Requirement.parse(‘botocore==2.0.0dev11’)

What should you do?

How Do You Install Django on a CentOS/RHEL/Fedora server?

Problem scenario
You want to install Django and have it working for browsing from a desktop workstation. You do not want to leverage Django’s API functionality. How do you install and configure Django from scratch?

Prerequisite
pip3 is probably installed. If you are not sure, you could run this: sudo find / -name pip3

You must have pip or pip3 installed.

How Do You Troubleshoot the Python Error “json.decoder.JSONDecode..”Expecting property name enclosed in double quotes…”?

Problem scenario
You are running a Python program that uses “import json”. You get this error: “json.decoder.JSONDecode..”Expecting property name enclosed in double quotes…” You are not allowed to use bson, but you can use other Python packages. What should you do?

Solution
This solution only works if you can eliminate single quotes in the content to be serialized (or translated into JSON).

How Do You Troubleshoot the Python Program Problem “FileNotFoundError: [Errno 2] No such file or directory”?

Problem scenario
Your Python program is trying to invoke a Bash command. But you get an error like this: “FileNotFoundError: [Errno 2] No such file or directory”. What should you do?

Possible Solution #1 Use the shell=True command if your environment is secure. This is not a recommended practice from a security perspective. We have found that this with shell=True syntax can make the error go away:

subprocess.Popen(variableofcommand,

How Do You Use Python to Create a Tuple with Extracted and Nested Values from JSON Files?

Problem scenario
You have some JSON files like this:

$cat first.json
{“bird”: {“name”: “singy”, “species”: “sparrow”, “amount”: “45”}}

$ cat second.json
{“bird”: {“name”: “flighty”, “species”: “seagull”, “amount”: “21”}}

Within the “bird” key there are other keys. How do you create a tuple with the species values?

Solution
Place first.json, second.json and the Python program below (called reader.py) in the same directory.

How Do You Display the VPC Peering Connections with Boto3?

Problem scenario
You want to list all the VPC Peering Connections for a specific region using Python.

You want the equivalent of aws ec2 describe-vpc-peering-connections

How do you show (retrieve or fetch) the VPC peering connections using Boto3?

Solution

import boto3
contint = boto3.client(‘ec2’)
var1 = contint.describe_vpc_peering_connections()
print(var1) …

In Python, How Do You Call a Bound Function as a New Thread with the threading Module?

Problem scenario
You want to write a program to call a bound function in a new thread. How do you do this?

Solution
Run this program (e.g., python foobar.py):

from threading import Thread

class mighty:
def good():
print(“Good”)

def contint():
print(“Hello!”)

if __name__ == “__main__”:
thread = Thread(mighty.good())
thread.start()
thread.join()
print(“thread finished…exiting”) …