Giter VIP home page Giter VIP logo

Comments (5)

GoogleCodeExporter avatar GoogleCodeExporter commented on July 18, 2024
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 [email protected] on 9 Sep 2013 at 5:19

  • Changed state: Invalid

from prettytable.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 18, 2024
Thank you, it works now. Clearly overlooked that.
Thank you for maintaining this project. Very handy!

Original comment by [email protected] on 12 Sep 2013 at 9:29

from prettytable.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 18, 2024
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 [email protected] on 5 Nov 2013 at 7:54

from prettytable.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 18, 2024
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 [email protected] on 5 Nov 2013 at 8:04

from prettytable.

GoogleCodeExporter avatar GoogleCodeExporter commented on July 18, 2024
Wow, thank you for the prompt and easy to understand response! It's working 
now. 

Original comment by [email protected] on 5 Nov 2013 at 8:46

from prettytable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.