How Do You Troubleshoot “AttributeError: module ‘re’ has no attribute ‘IGNORECASE'”?

Problem scenario
When you run your Python program, you get this message: “AttributeError: module ‘re’ has no attribute ‘IGNORECASE'”

What should you do to get your program to work correctly?

Solution
Change the name of the program to something different. Do not name your program “re.py”. If you use the “import string” and your program is called “re.py”, you may get this error.

How Do You Troubleshoot Messages about a markupsafe Fatal Error, C Extension Not Being Compiled, And/Or a Syntax Error with async when You Are Trying to Build a Docker Image?

Problem scenario
You wrote a Dockerfile. You are using it to try to create a Docker image. When you run the “docker build” command you get some error messages. The errors include one or more of the following:

1) A MarkupSafe fatal error related to Python.h.
2) A C extension not being compiled.
3) An invalid syntax error related to async (e.g., “Jinja2/jinja2/asyncfilters.py”).

You need to base the image off Ubuntu.

When Manipulating Lists in Python, Why is pop Slower than del?

Problem scenario
You use del and pop to remove items from lists. You noticed that pop is slower than del. Why is this the case?

Solution
There are two reasons. One, pop returns the value that is removed. Two, pop acts as a function call as opposed to a primitive action. Whereas the del invokes a primitive action (a non-recursive function call),

How Do You Find Where the Python Interpreter Will Look for Modules?

Problem scenario
You want to use custom modules (e.g., .py files that will be called by another Python program). You are trying to figure out which location(s) the Python interpreter will look for such modules. How do you determine the directory where Python will look when it uses the “import” key word?

Solution
Run these three commands (the first is a Bash command and the other two are Python):

python
import sys
print(sys.path) …

How Do You Troubleshoot the Python Error “subprocess.CalledProcessError: Command ‘…’ returned non-zero exit status 1”?

Problem scenario
You are automating a Linux infrastructure task with Python using subprocess calls. You get this error “subprocess.CalledProcessError: Command ‘…’ returned non-zero exit status 1”, what should you do?

Solution
Run the Linux command without Python. Then run echo $? to determine the exit code. If you see a 1, that means Python notices this command is not considered to have run successfully.

How Do You Retrieve an Application-Level Secret from AWS?

Problem scenario
You want to obtain a non-database secret from AWS and you know the name of the secret (in Secrets Manager) and the region it is in. What should you do?

Solution
Prerequisites
You have installed and configured the AWS CLI. If you need assistance with this, click on this posting if you can use pip or this posting if you cannot use pip.

Python Tips when Dealing with Lists

Here are some tips when using Python and dealing with lists.

  1. Use snake_case for list variable names. For more information on coding style in Python, see the PEP 8 style guide here.
  2. Never call a variable “list”. (If you are using a variable for a list, use something like small_list or big_list — not “list”.) Any word that is not a reserved word and not a built-in data type will work.

In Python How Do You Print Just The Values of The Two-dimensional List without The Quotes, Commas or Brackets?

Problem scenario
In Python your program has a two-dimensional list. It prints out like this:

[’ ‘, ‘X’, ‘ ‘, ‘ ‘, ‘ ‘]
[’ ‘, ‘X’, ‘X’, ‘ ‘, ‘ ‘]
[’ ‘, ‘X’, ‘X’, ‘X’, ‘ ‘]
[’ ‘, ‘X’, ‘X’, ‘X’, ‘X’]

You want it to print out like this:

X
XX
XXX
XXXX

Solution
Assuming that there are n rows of your matrix (aka two-dimensional array),

In Python How Do You Print Three Integers Separated by a Space on the Same Line?

Problem scenario
You want to print three integers. You do not want to convert them to be part of a string. You want a space to separate each integer. How do you do this in Python?

Solution
Assuming your variables are x, y, and z, and they are integers, use a line like this:

print(x, y, z)

Here is a complete program that proves it works:

x = 5
y = 3
z = 100
print(x, …