How Do You Troubleshoot a Python Error “UnboundLocalError: local variable ‘x’ referenced before assignment”?

Problem scenario
You run a Python program but you get this error: “UnboundLocalError: local variable ‘x’ referenced before assignment”

Solution
Root Cause

You have a function in Python and it refers to a previously assigned variable.  This variable was not passed as a parameter.

Procedures
You need to redeclare (but not necessarily reassign) the variable as a “global”. 

How Do You Troubleshoot This Message “ImportError: No module named ‘google.cloud’?

Problem scenario
You run a Python program or from the Python command prompt you receive this message:

Traceback (most recent call last):
  File “main.py”, line 21, in <module
    from google.cloud import pubsub_v1
 ImportError: No module named ‘google.cloud’

ModuleNotFoundError: No module named ‘google’

What should you do to eliminate this problem?

Possible Solution #1
Try to run the program as sudo:

sudo python nameOfProg.py

Possible Solution #2
Try this with or without sudo (depending on whether or not you are using “sudo” to run the Python program):

sudo pip install google-cloud-bigquery

# You may or may not want to omit the “sudo” in the above command.

How Do Quotes Affect Python if Conditional Tests of Equality for a Variable?

Problem scenario
You are programming in Python, and you want to use conditional logic.  You want to know how variables are evaluated and tested.  You want to use the keyword “if” to evaluate a variable assignment.

Solution
This program below shows how the evaluation of a variable in Python behaves when quotes are involved. Errors are not thrown or displayed to the programmer.  Therefore in some instances the programmer must know how quotes affect logical evaluations with variables.

How Do You Delete Servers in AWS with Python?

Problem scenario
You want to write a Python program to delete servers.  How do you delete EC-2 instances in AWS with Python?

Solution
Prerequisites

a.  You have installed Boto3.  If you do not know how, see this posting
b.  You know the instance IDs.  If you do not know them, see this posting.

Procedures
Read the “Usage instructions” of this Python program we call “deleteec2.py”.

How Are Processes and Threads Different from Each Other in Python?

Problem scenario
You have heard of processes and threads in Python.  Are there illustrative Python programs that use processes and threads to demonstrate the differences between processes and threads themselves?

Solution
Here are two differences between processes and threads in Python:

Difference #1  Children processes keep running when parent process is killed.  Child threads stop processing when parent process is killed.

How Do You Troubleshoot the AWS CLI Error “Traceback … import pkg_resources”?

Problem scenario
You run an “aws” command but you receive an error message such as this:

Traceback (most recent call last):
  File “/usr/bin/aws”, line 4, in <module>
    import pkg_resources
  File “/usr/lib/python2.7/site-packages/pkg_resources.py”, line 3007, in <module>

What should you do?

Solution
1.  Install pip.  If you need assistance, see this posting (especially the “Problem scenario” paragraph with hyperlinks based on distribution families of Linux).

How Do You Use a Generator and the Yield Keyword in Python?

Problem scenario
You want to use a generator in Python.  You also want to use the yield keyword in Python.  How do you use these?

Solution
The short answer to the question about how do you use a generator is to create a function with the yield keyword (source Programiz.com).  “Both yield and return will return some value from a function.”  (The source of this quote is 

How Do You Create Your Own Dockerfile to Create a Flask Application?

Problem scenario
You want to use Flask inside a Docker container.  You want to build your own image for the Docker container.  How do you create your own Dockerfile to create a Flask application?

Solution
Prerequisite

Install Docker.  See this posting if you need directions.

Procedures
1.  Create a Dockerfile with the following content (you may want to replace “16.04” with “latest” and if you want to change the location of where the .py file is,

How Do You Use GCP’s App Engine?

Problem scenario
You want to leverage GCP’s serverless App Engine component with its scalability.  What do you do?

Solution
1.  Go to the cloud shell.  In the web UI of GCP in the upper righthand corner go to the icon that looks like this: “>_” and click on it.  From cloud shell run these commands:

git clone https://github.com/GoogleCloudPlatform/python-docs-samples

cd python-docs-samples/appengine/standard_python37/hello_world

virtualenv –python python3 ~/envs/hello_world

source ~/envs/hello_world/bin/activate

pip install -r requirements.txt

python main.py

2. 

How Do You Write a BubbleSort Algorithm in Python That Runs in O(n) in a Best Case Scenario?

Problem scenario
You want to use BubbleSort in Python that runs in O(n) time when the list to be sorted is already in perfect order.  How do you write such a program?

Solution
Use this code for BubbleSort with a refinement:

def refinedBubbleSort(coollist):
needtoswap = True
x = len(coollist)-1 # x is now the index value of last item in list
while x 0 and needtoswap:
needtoswap = False
for i in range(x): # start with leftmost number and iterate to the xth position one space at a time
if coollist[i] coollist[i+1]: # swap if left item in pair is greater than right item
needtoswap = True
coollist[i],coollist[i+1] = coollist[i+1],coollist[i]
x = x-1 # Go through the list from rightmost item until the leftmost item
coollist = [1,2,3,4,5,6,7,8,9,10]
refinedBubbleSort(coollist)
print(coollist) …