Author Archives: Kiprono Koech

Fix NameError name is not defined

This Python error may occur because you are trying to create an array but not importing necessary packages accordingly, or the variable/identifier being accessed has not been defined in the code.

Case 1: Importation of packages

Improper importation of Python packages or failing to import the necessary packages altogether can cause this error. For example, attempting to convert the list [1, 5, 6] using the following code snippet will lead to NameError,

c = array([1, 5, 6])

Output:

NameError: name 'array' is not defined

To fix this error, we need to import the Python packages and methods correctly.

Solution 1: Using the NumPy package

The array function and all other functions in the NumPy package are accessible under the alias name np in this solution. If you are interested in the array function of NumPy only you can use the following code snippet, instead:

import numpy as np # import package
b = np.array([1.1, 2.0, 5.4]) #list to array
print(b) #print b
print(type(b))

Output:

[1.1 2.  5.4]
<class 'numpy.ndarray'>

Solution 2: Using the array package as follows

from array import array # import package
c = array("i", [1, 5, 6]) #list to array
print(c) # print the just created array
print(type(c)) # print the data-type

Output:

array('i', [1, 5, 6])
<class 'array.array'>

Note: array package requires the data type that the array will hold to be explicitly defined. The “i” string stands for integer. You can read more about array at https://docs.python.org/3/library/array.html.

Case 2: The identifier being accessed is not defined

In this case, you are trying to access a variable, or a function named “array” or otherwise when it has not been defined or has been defined in a different scope. This case captures all other causes of NameError in Python. They include:

a) Calling a function/ variable before it is declared

Python executes a script from top to bottom except for functions. The contents of any function are only executed when the function is called.

print(books) # accessing a variable
books = 4 # initializing a variable

Output:

NameError: name 'books' is not defined
books_collections() # calling function
def books_collections():# declaring function 
    print(books)

Output:

NameError: name 'books_collections' is not defined

In the first case, the variable ‘books’ is referred to before it is initialized and the function ‘books_collections’ is called before it is declared in the second case, hence the error.

The NameError created here is equivalent to when we try to access a variable/function that has not been declared at all.

b) Defining a variable out of the scope

The variable defined inside the function is called a local variable and can only be accessed inside the function. In contrast, a variable defined outside a function (global variable) is accessible anywhere in the script after being declared. Referencing a local variable outside the function causes NameError. Here is an example.

rulers = 8

def classroom():    
    pens = 3
    books = 4
    # pens, books declared inside function
    # they are local variables
    # rulers is declared outside function
    # it is a global variable
    print("pens (local): ", pens)
    print("rulers (global): ", rulers)

classroom()
print(pens)

Output:

pens (local):  3
rulers (global):  8
NameError: name 'pens' is not defined

The last line leads to NameError because the “pens” variable is defined inside the function (local variable) and therefore cannot be accessed as we are trying to do.

c) Misspelled built-in functions

For example, using “power” instead of “pow” to imply exponent leads to NameError.

power(4, 2) # it should be pow(4, 2)

Run Background Process in Python

Background processes are computer processes that run behind the scenes. The core advantage of running processes in the background is that you can do other tasks in the foreground. In this article, we will discuss how to run background processes using Python.  We will also discuss how to check and stop running processes/applications.

Checking Running Processes and Services

To check the running processes in Windows, you can use the Task Manager App or run the command “tasklist” on the command line.

Some processes or services start when the computer is started, while the user explicitly starts others. A process can also create another process. For example, a Python process is begun when running a Python script on an editor or an IDE like Sublime Text or Visual Studio.  The rest of the processes running under Sublime Text in the Task Manager below are just processes supporting the two main processes.

The problem is that once Sublime Text is closed, all these processes are closed – including the program being executed in Python. This is where understanding how to run the background process comes in.

Running background processes in Python

In Python, any of the two packages, namely, subprocess and os, can be used to run background processes. The general syntax is:

subprocess:

import subprocess
subprocess.Popen(<list of commands and options>)

os:

import os
os.system(<the commands and options>)

To demonstrate how these packages work in running a background process, let’s run a Python script. Even if the code editor is closed, the python.exe process will keep running in the background until execution is complete. This is because the Python process is not attached to the process holding the editor alive.

Step 1: Create a Python file with the code we are interested in running in the background

Create a python script “process.py” with the following code in it:

import time

with open("results.txt", "a+") as fp:
    for i in range(5):
        # Each loop should take around
        # 20 secs to execute. Total ~ 100 seconds
        time.sleep(20)
        fp.write(f"{i}\n")

The code should run for around 100 seconds and write a text file (results.txt) into the disk with numbers 0 through 4 written in it. If you execute this file on the editor directly and accidentally or otherwise close the editor, the execution process is also terminated. This is because closing the editor also closes the Python process, which is responsible for executing the code.

Step 2: Create another Python script that will wake up the Python process in the background

To ensure that the code on “process.py” is executed in the background until completion, let us create another file Python script called “run_process.py” and use the two packages, subprocess, and os, to run the code in “process.py” in the background.

The file “run_process.py” contains the following lines of codes based on the choice of the package to use:

subprocess package:

import subprocess
subprocess.Popen(["python", "process.py"], shell=True)

and, for the os package, we have

import os
os.system("python process.py")

Step 3: Execute the run_process.py file on the code editor

Note that a Python background process running “process.py” is initiated when you execute “run_process.py” on the code editor. This process remains awake until the execution of the “process.py” script is completed, even if you close the editor.

This process locks the results.txt file, so if you try to delete it during the program operation, you will be greeted with the message that the file is in use, so you can either wait or kill the process.

Killing a running process

We need the Image Name or the Process ID (PID) to kill a running process. This information can be found on the Task Manager or by running the tasklist on the command line. You might need to right-click on the title section and select PID and Process name on Task Manager (see below).

Any of the following options can be used to terminate a running process (refer to the above screenshot of Task Manager for the following points).

Using Task Manager:

select the process, right-click, and click “end task” at the bottom right of the manager window.

On the command prompt:

  • when using PID run “taskkill /PID <pid> /PID <another_pid> /F“, for example, “taskkill /PID 11024 /F” to kill CTF Loader.
  • When using Image Name, run “taskkill /IM <ImageName> /IM <another_ImageName> /F”  for example “taskkill /IM ctfmon.exe /F” to kill CTF Loader.

In Python

You can kill running tasks using subprocess or os as follows (note that you need to import these packages):

  • subprocess.Popen([“taskkill”, “/PID”, “11024”, “/F”])
  • os.system(“taskkill /IM ctfmon.exe /F”)

Note: The /F option issued to the taskkill command is optional. It is used to terminate a process forcefully. This option might be needed when closing a task used by another process, for example.

The PID changes every time you open the process/app. This means that the PID used to kill a task generated by a given app may not work when an app is closed and opened again. In such a case, Image Name is a better option to use.

Conclusion

In this article, we discussed how to wake up a process and keep running in the background using Python. We have demonstrated how this is done by running a python.exe process in the background (you can do this with any process). Lastly, we have shown how to terminate a running process(es) using Task Manager, command prompt, and Python packages.