Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I solve NameError: name 'threading' is not defined in python 3.3

I have the following program, and nothing else, python 3.3. When I run it. I get

NameError: name 'threading' is not defined

I googled but none of the answers given explain my situation. any clues? Thanks!

#!/usr/bin/python

import Utilities
import os
import sys
import getopt
import time
from queue import Queue
from threading import Thread

_db_lock=threading.Lock()

I also tried

_db_lock=threading.Lock
like image 391
Chris F Avatar asked Apr 03 '14 20:04

Chris F


1 Answers

You must import threading. Add the following to the beginning of your file:

import threading

The error originates from the line:

_db_lock=threading.Lock()

That's because you've used from threading import Thread, but you've never actually introduced threading in to the local namespace. So far there's only Thread (even though technically the import is there, it's just not in the namespace, you cannot use it).

If for some reason you wish to keep threading from 'polluting' your namespace, import the Lock in the same manner as you've imported Thread, like so:

from threading import Thread, Lock
_db_lock = Lock()
like image 179
msvalkon Avatar answered Oct 07 '22 08:10

msvalkon