tienbm90 / prettytable

Automatically exported from code.google.com/p/prettytable
Other
0 stars 0 forks source link

NameError: name 'PrettyTable' is not defined #35

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Followed the 'hard-way installation' on Windows with python27
2. imported module like this: 'import prettytable'
3. created new instance like this: 'myTable = PrettyTable()'

=> NameError: name 'PrettyTable' is not defined

Version: Tried with RC01 and 0.7.2

What am I doing wrong here?
Thanks

Original issue reported on code.google.com by matth...@langhard.com on 26 Aug 2013 at 12:51

GoogleCodeExporter commented 8 years ago
You need to do either this:

-----
import prettytable
myTable = prettytable.PrettyTable()
-----

or this:

-----
from prettytable import PrettyTable
myTable = PrettyTable()
-----

This is just the way that Python imports work - all libraries work this way and 
it's not an issue specific to PrettyTable.

Sorry for the very slow reply!  Hope this helps.

Cheers,
Luke

Original comment by luke@maurits.id.au on 9 Sep 2013 at 5:19

GoogleCodeExporter commented 8 years ago
Thank you, it works now. Clearly overlooked that.
Thank you for maintaining this project. Very handy!

Original comment by matth...@langhard.com on 12 Sep 2013 at 9:29

GoogleCodeExporter commented 8 years ago
I am very new to programming and having trouble getting this to work. I copied 
the example code from the tutorial and added the import code from the previous 
response in this thread but still getting the same error as the op:

Traceback (most recent call last):
  File "D:\Program Files\PROGRAMMING\Python\test1.py", line 4, in <module>
    x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
NameError: name 'PrettyTable' is not defined

My code is:

import prettytable
mytable = prettytable.PrettyTable()

x = PrettyTable(["City name", "Area", "Population", "Annual Rainfall"])
x.align["City name"] = "l" # Left align city names
x.padding_width = 1 # One space between column edges and contents (default)
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
print(x)

What am I doing wrong?

Original comment by nightfli...@gmail.com on 5 Nov 2013 at 7:54

GoogleCodeExporter commented 8 years ago
You are basically having the same problem as the person who originally opened 
this issue, and the solution is the same.

When you do "import prettytable", this basically lets you use the word 
"prettytable" in your code without Python getting confused.  Anything else 
needs to be prefaced with "prettytable", so to instantiate a PrettyTable object 
you'd need to use "prettytable.PrettyTable".

Alternatively you can do "from prettytable import PrettyTable", which then lets 
you use PrettyTable directly.

You are correctly instantiating the "mytable" variable on the second line of 
your code.  But then you go on to try to instantiate the "x" variable 
incorrectly, using PrettyTable directly.

The easiest way to change the code you have posted to somethin that works is:

1) Delete line 2 (you aren't using "mytable" anywhere anyway)
2) Change line 3 to start with "x = prettytable.PrettyTable(" (keep the rest of 
the line as it is)

This should then display the example table of Australian cities.

Original comment by luke@maurits.id.au on 5 Nov 2013 at 8:04

GoogleCodeExporter commented 8 years ago
Wow, thank you for the prompt and easy to understand response! It's working 
now. 

Original comment by nightfli...@gmail.com on 5 Nov 2013 at 8:46