1

I am a relatively new Python user. I am trying to replicate a series of modules and classes discussed in textbooks. Even though I have all the libraries available in Python that are imported in the code I am replicating, I am still get a lot of error messages like:

‘NameError: name <insert name> is not defined’

To me, this means either:

  • I do not have all libraries I need;
  • I have not been able to successfully import all libraries.

My questions are:

  1. When I am in Jupyter, or in IDLE, how can I tell what libraries I have successfully imported?

  2. For the libraries I have imported, how can I tell what functions there are available to me (so that I can check if the functions that give a NameError are in the libraries I imported)?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Thdh
  • 81
  • 4
  • 1
    No, this means that `name` is not defined. e.g., if you do `print (steve)` you'll get the same error, unless you have previously assigned some value to an object named `steve` It may not have anything to do with your libraries/modules at all. Show your code? – David Zemens Nov 13 '15 at 15:31
  • it can also mean that you are using a variable before defining it, however you can always use the interactive python and do your imports and see if they work, and use the 'dir' function to see what is defined in them – gkusner Nov 13 '15 at 15:32

1 Answers1

0

This more likely means that name is not defined or you're referencing a name/variable before assignment.

 def foo():
     print(name)

 foo()

The above code will raise the same error, as name is undefined.

how can I tell what libraries I have successfully imported?

If the libraries haven't imported, you'll get an import error. If you don't get any import error, then the libraries have imported successfully. Here is an example you can try:

import david  #assuming you have no module named 'david'

Should result in:

ImportError: no module named david

how can I tell what functions there are available to me

import some_module  # modify as needed
name = 'david'

print name in dir(some_module)  # returns True/False
David Zemens
  • 53,033
  • 11
  • 81
  • 130
  • So, `print dir(some_module)` would print all the names available for use from `some_module`? – Jonathan Leffler Nov 13 '15 at 15:49
  • You should try to avoid asking questions which can be answered through trial and error :) or by reading the dox: https://docs.python.org/2/library/functions.html#dir – David Zemens Nov 13 '15 at 15:51