How Do You Troubleshoot the Python Problem “NameError: global name ‘var2’ is not defined”?

Problem scenario
You have a Python program that works, but you get an error when you run it. You get a message like this: “NameError: global name ‘var2’ is not defined”

Here is your source code:

def fun1(var1):
print(“this is how it works”)
var1 = var2
return var2
b = fun1(“check it out”)
print(b)

What should you do about this error?

Solution
A variable inside a function must be defined.

How Do You Create a Master Branch (or First Branch) with the GitHub API?

Problem scenario
You do not want to copy a branch to create a new one. How do you create a branch of a repository that has no branches?

Solution
You must know the relevant organization name of the Git repository and the name of the repository itself. Do these changes:

  • Change “acme” to the organization name
  • Change “goodrepo” to the repository name of your choice.

How Do You Troubleshoot a pom.xml File?

Problem scenario
You are trying to run your Java program from a .jar file. You get an error like this:

Error: Could not find or load main class target.contint-0.1.0.jar
Caused by: java.lang.ClassNotFoundException: target.contint-0.1.0.jar

What should you do?

Solution
Examine the mainClass stanza. The word to the left of the period should be the name of the package.

How Do You Use Multi-Line String Variables in JavaScript?

Problem scenario
You want to present multiple lines in an “alert” message in JavaScript. How do you have new lines with text in JavaScript?

Possible Solution #1 (less preferred)
Use syntax like this:

var y = `
line1
line2
line3
`;

Possible Solution #2 (preferred)

var z = [
line1,
line2,
line3
].join(“\n”);

You could use this stand-alone file multiline.html:

<!DOCTYPE html<html<body<h2This demonstrates a multi-line variable in JavaScript</h2<p id=”demo”</p<scriptline1 = “aaa”
line2 = “bbb”
line3 = “ccc”

var z = [
line1, …

How Do You Get Past “ImportError: No module named ‘boto'”?

Problem scenario
You want to retrieve VPC peering connection info and other VPC info via Boto3. With Python 3 you tried to run this Python 2 (and Boto 2.x) program:

import boto.vpc
c = boto.vpc.connect_to_region(‘us-east-1’)
vpcs = c.get_all_vpcs()
vpc_peering_connection = c.create_vpc_peering_connection(vpcs[0].id, vpcs[1].id)

(It was written by the person who developed Boto 2.x here.)

You have a variety or problems with the syntax not working.

How Do You Create a Function in JavaScript?

Problem scenario
You want to create a function in JavaScript. This way you can use code over-and-over. How do you use a function in JavaScript?

Solution
Call this file contint.html

<!DOCTYPE html<html<body<h2This demonstrates accepting input and using a function in JavaScript</h2<p id=”demo”</p<scriptx = 5
var foo = prompt(“Please enter a number”);

function contintFunction(p1, …

How Do You Get Nginx Logs to be in JSON format?

Problem scenario
The book Expert Python Programming (page 278) says 12 Factor app recommends that the application should not be aware of the format of the logs. JSON format for logs can be beneficial for various purposes. You want Nginx’s access logs — but not error logs — to be in JSON format. What do you do?

Solution
Use a file like this for your nginx.conf.