Velocity Reviews > Problem with importing in Python

# Problem with importing in Python

su29090
Guest
Posts: n/a

 01-11-2013
I'm trying to import a python file it keeps saying:

ImportError: cannot import name Circle

Here is the file I'm trying to import:

Circle.py

import math

class circle:
#Construct a circle object

def getPerimeter(self):
return 2 * self.radius * math.pi

def getArea(self):

from Circle import Circle

def main():
#Create a circle with a radius 1
circle1 = Circle()
print("The area of the circle of radius",

#Create a circle with a radius 25
circle2 = Circle(25)
print("The area of the circle of radius",

#Create a circle with a radius 125
circle3 = Circle(125)
print("The area of the circle of radius",

print("The area of the circle of radius",

main() # Call the main function

How can I solve this problem?

Guest
Posts: n/a

 01-11-2013
Python is case sensitive.
Circle and circle is not same.

/* sent from android */
On Jan 11, 2013 11:22 PM, "su29090" <(E-Mail Removed)> wrote:

> I'm trying to import a python file it keeps saying:
>
> ImportError: cannot import name Circle
>
> Here is the file I'm trying to import:
>
> Circle.py
>
> import math
>
> class circle:
> #Construct a circle object
> def __init__(self, radius = 1):
>
> def getPerimeter(self):
> return 2 * self.radius * math.pi
>
> def getArea(self):
>
>
> from Circle import Circle
>
> def main():
> #Create a circle with a radius 1
> circle1 = Circle()
> print("The area of the circle of radius",
>
> #Create a circle with a radius 25
> circle2 = Circle(25)
> print("The area of the circle of radius",
>
> #Create a circle with a radius 125
> circle3 = Circle(125)
> print("The area of the circle of radius",
>
> print("The area of the circle of radius",
>
> main() # Call the main function
>
> How can I solve this problem?
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Chris Angelico
Guest
Posts: n/a

 01-11-2013
On Sat, Jan 12, 2013 at 9:17 AM, su29090 <(E-Mail Removed)> wrote:
> Circle.py
>
> class circle:
>
> from Circle import Circle

Inside the Circle module is a class named circle. You can't import
Circle from that.

But Python isn't Java. You don't have to put each class into its own
file. Just put class circle (or class Circle to follow Python naming
convention) into the other file, whose name you haven't given.

If they're already in the same file, then just drop the import. Easy!

By the way:
> main() # Call the main function

You'll need to put that flush left. At the moment, that call is part
of the definition of main().

Hope that helps!

ChrisA

su29090
Guest
Posts: n/a

 01-11-2013
> Python is case sensitive.
>
> Circle and circle is not same.
>
>
>
>
>
>
> /* sent from android */
>
> On Jan 11, 2013 11:22 PM, "su29090" <(E-Mail Removed)> wrote:
>
> I'm trying to import a python file it keeps saying:
>
>
>
> ImportError: cannot import name Circle
>
>
>
> Here is the file I'm trying to import:
>
>
>
> Circle.py
>
>
>
> import math
>
>
>
> class circle:
>
> * * #Construct a circle object
>
> * * def __init__(self, radius = 1):
>
>
>
>
> * * def getPerimeter(self):
>
> * * * * return 2 * self.radius * math.pi
>
>
>
> * * def getArea(self):
>
>
>
>
>
>
>
>
> from Circle import Circle
>
>
>
> def main():
>
> * * #Create a circle with a radius 1
>
> * * circle1 = Circle()
>
> * * print("The area of the circle of radius",
>
> * * * * * circle1.radius, "is" , circle1.getArea())
>
>
>
> * * #Create a circle with a radius 25
>
> * * circle2 = Circle(25)
>
> * * print("The area of the circle of radius",
>
> * * * * * circle2.radius, "is" , circle2.getArea())
>
>
>
> * * #Create a circle with a radius 125
>
> * * circle3 = Circle(125)
>
> * * print("The area of the circle of radius",
>
> * * * * * circle3.radius, "is" , circle3.getArea())
>
>
>
> * * #Modify circle radius
>
>
> * * print("The area of the circle of radius",
>
> * * * * * circle2.radius, "is" , circle2.getArea())
>
>
>
> * * main() # Call the main function
>
>
>
> How can I solve this problem?
>
>
>
>
>
>
> --
>
> http://mail.python.org/mailman/listinfo/python-list

It still keeps showing the same message.

su29090
Guest
Posts: n/a

 01-11-2013
> Python is case sensitive.
>
> Circle and circle is not same.
>
>
>
>
>
>
> /* sent from android */
>
> On Jan 11, 2013 11:22 PM, "su29090" <(E-Mail Removed)> wrote:
>
> I'm trying to import a python file it keeps saying:
>
>
>
> ImportError: cannot import name Circle
>
>
>
> Here is the file I'm trying to import:
>
>
>
> Circle.py
>
>
>
> import math
>
>
>
> class circle:
>
> * * #Construct a circle object
>
> * * def __init__(self, radius = 1):
>
>
>
>
> * * def getPerimeter(self):
>
> * * * * return 2 * self.radius * math.pi
>
>
>
> * * def getArea(self):
>
>
>
>
>
>
>
>
> from Circle import Circle
>
>
>
> def main():
>
> * * #Create a circle with a radius 1
>
> * * circle1 = Circle()
>
> * * print("The area of the circle of radius",
>
> * * * * * circle1.radius, "is" , circle1.getArea())
>
>
>
> * * #Create a circle with a radius 25
>
> * * circle2 = Circle(25)
>
> * * print("The area of the circle of radius",
>
> * * * * * circle2.radius, "is" , circle2.getArea())
>
>
>
> * * #Create a circle with a radius 125
>
> * * circle3 = Circle(125)
>
> * * print("The area of the circle of radius",
>
> * * * * * circle3.radius, "is" , circle3.getArea())
>
>
>
> * * #Modify circle radius
>
>
> * * print("The area of the circle of radius",
>
> * * * * * circle2.radius, "is" , circle2.getArea())
>
>
>
> * * main() # Call the main function
>
>
>
> How can I solve this problem?
>
>
>
>
>
>
> --
>
> http://mail.python.org/mailman/listinfo/python-list

It still keeps showing the same message.

su29090
Guest
Posts: n/a

 01-11-2013
On Friday, January 11, 2013 5:27:21 PM UTC-5, Chris Angelico wrote:
> On Sat, Jan 12, 2013 at 9:17 AM, su29090 wrote:
>
> > Circle.py

>
> >

>
> > class circle:

>
> >

>
> > from Circle import Circle

>
>
>
> Inside the Circle module is a class named circle. You can't import
>
> Circle from that.
>
>
>
> But Python isn't Java. You don't have to put each class into its own
>
> file. Just put class circle (or class Circle to follow Python naming
>
> convention) into the other file, whose name you haven't given.
>
>
>
> If they're already in the same file, then just drop the import. Easy!
>
>
>
> By the way:
>
> > main() # Call the main function

>
> You'll need to put that flush left. At the moment, that call is part
>
> of the definition of main().
>
>
>
> Hope that helps!
>
>
>
> ChrisA

It worked! Thanks so much!

su29090
Guest
Posts: n/a

 01-11-2013
On Friday, January 11, 2013 5:27:21 PM UTC-5, Chris Angelico wrote:
> On Sat, Jan 12, 2013 at 9:17 AM, su29090 wrote:
>
> > Circle.py

>
> >

>
> > class circle:

>
> >

>
> > from Circle import Circle

>
>
>
> Inside the Circle module is a class named circle. You can't import
>
> Circle from that.
>
>
>
> But Python isn't Java. You don't have to put each class into its own
>
> file. Just put class circle (or class Circle to follow Python naming
>
> convention) into the other file, whose name you haven't given.
>
>
>
> If they're already in the same file, then just drop the import. Easy!
>
>
>
> By the way:
>
> > main() # Call the main function

>
> You'll need to put that flush left. At the moment, that call is part
>
> of the definition of main().
>
>
>
> Hope that helps!
>
>
>
> ChrisA

It worked! Thanks so much!

Dave Angel
Guest
Posts: n/a

 01-11-2013
On 01/11/2013 05:17 PM, su29090 wrote:
> I'm trying to import a python file it keeps saying:
>
> ImportError: cannot import name Circle
>
> Here is the file I'm trying to import:
>
> Circle.py
>
> import math
>
> class circle:
> #Construct a circle object
> def __init__(self, radius = 1):
>
> def getPerimeter(self):
> return 2 * self.radius * math.pi
>
> def getArea(self):
>
>
> from Circle import Circle
>
> def main():
> #Create a circle with a radius 1
> circle1 = Circle()
> print("The area of the circle of radius",
>
> #Create a circle with a radius 25
> circle2 = Circle(25)
> print("The area of the circle of radius",
>
> #Create a circle with a radius 125
> circle3 = Circle(125)
> print("The area of the circle of radius",
>
> print("The area of the circle of radius",
>
> main() # Call the main function
>
> How can I solve this problem?
>
>

As Adnan has pointed out, Python is case insensitive. You're apparently
trying to refer to the class Circle by the name circle, or the other way
around.

1) Specify the Python version. I presume 3.3 It probably doesn't
matter here, but it might have.
2) When showing two source files, identify where each starts and ends,
and what the second one is called.
3) When showing an error, include the entire traceback, not just the
last line.

Now, there are conventions to follow as well (see Pep. One is that
modules should use all lowercase, and classes should begin with a
capital. So the source file of your module should be named
circle.py and the class Circle. When you imported and instantiated
the class, you assumed it was called Circle, but when you defined it,
you mistakenly called it circle.

The next error is the accidental indentation of the call to main(). As
it stands now, it's a recursive call to itself. And main() will never
be called, because there's no call at top-level.

--

DaveA

su29090
Guest
Posts: n/a

 01-11-2013
On Friday, January 11, 2013 5:43:10 PM UTC-5, Dave Angel wrote:
> On 01/11/2013 05:17 PM, su29090 wrote:
>
> > I'm trying to import a python file it keeps saying:

>
> >

>
> > ImportError: cannot import name Circle

>
> >

>
> > Here is the file I'm trying to import:

>
> >

>
> > Circle.py

>
> >

>
> > import math

>
> >

>
> > class circle:

>
> > #Construct a circle object

>
> > def __init__(self, radius = 1):

>

>
> >

>
> > def getPerimeter(self):

>
> > return 2 * self.radius * math.pi

>
> >

>
> > def getArea(self):

>

>
> >

>

>

>
> >

>
> > from Circle import Circle

>
> >

>
> > def main():

>
> > #Create a circle with a radius 1

>
> > circle1 = Circle()

>
> > print("The area of the circle of radius",

>
> > circle1.radius, "is" , circle1.getArea())

>
> >

>
> > #Create a circle with a radius 25

>
> > circle2 = Circle(25)

>
> > print("The area of the circle of radius",

>
> > circle2.radius, "is" , circle2.getArea())

>
> >

>
> > #Create a circle with a radius 125

>
> > circle3 = Circle(125)

>
> > print("The area of the circle of radius",

>
> > circle3.radius, "is" , circle3.getArea())

>
> >

>

>

>
> > print("The area of the circle of radius",

>
> > circle2.radius, "is" , circle2.getArea())

>
> >

>
> > main() # Call the main function

>
> >

>
> > How can I solve this problem?

>
> >

>

>
> >

>
>
>
> As Adnan has pointed out, Python is case insensitive. You're apparently
>
> trying to refer to the class Circle by the name circle, or the other way
>
> around.
>
>
>
>
>
>
> 1) Specify the Python version. I presume 3.3 It probably doesn't
>
> matter here, but it might have.
>
> 2) When showing two source files, identify where each starts and ends,
>
> and what the second one is called.
>
> 3) When showing an error, include the entire traceback, not just the
>
> last line.
>
>
>
> Now, there are conventions to follow as well (see Pep. One is that
>
> modules should use all lowercase, and classes should begin with a
>
> capital. So the source file of your module should be named
>
> circle.py and the class Circle. When you imported and instantiated
>
> the class, you assumed it was called Circle, but when you defined it,
>
> you mistakenly called it circle.
>
>
>
> The next error is the accidental indentation of the call to main(). As
>
> it stands now, it's a recursive call to itself. And main() will never
>
> be called, because there's no call at top-level.
>
>
>
>
>
>
>
>
>
>
>
> --
>
>
>
> DaveA

Thanks for explanation which was very clear!

su29090
Guest
Posts: n/a

 01-11-2013
On Friday, January 11, 2013 5:43:10 PM UTC-5, Dave Angel wrote:
> On 01/11/2013 05:17 PM, su29090 wrote:
>
> > I'm trying to import a python file it keeps saying:

>
> >

>
> > ImportError: cannot import name Circle

>
> >

>
> > Here is the file I'm trying to import:

>
> >

>
> > Circle.py

>
> >

>
> > import math

>
> >

>
> > class circle:

>
> > #Construct a circle object

>
> > def __init__(self, radius = 1):

>

>
> >

>
> > def getPerimeter(self):

>
> > return 2 * self.radius * math.pi

>
> >

>
> > def getArea(self):

>

>
> >

>

>

>
> >

>
> > from Circle import Circle

>
> >

>
> > def main():

>
> > #Create a circle with a radius 1

>
> > circle1 = Circle()

>
> > print("The area of the circle of radius",

>
> > circle1.radius, "is" , circle1.getArea())

>
> >

>
> > #Create a circle with a radius 25

>
> > circle2 = Circle(25)

>
> > print("The area of the circle of radius",

>
> > circle2.radius, "is" , circle2.getArea())

>
> >

>
> > #Create a circle with a radius 125

>
> > circle3 = Circle(125)

>
> > print("The area of the circle of radius",

>
> > circle3.radius, "is" , circle3.getArea())

>
> >

>

>

>
> > print("The area of the circle of radius",

>
> > circle2.radius, "is" , circle2.getArea())

>
> >

>
> > main() # Call the main function

>
> >

>
> > How can I solve this problem?

>
> >

>

>
> >

>
>
>
> As Adnan has pointed out, Python is case insensitive. You're apparently
>
> trying to refer to the class Circle by the name circle, or the other way
>
> around.
>
>
>
>
>
>
> 1) Specify the Python version. I presume 3.3 It probably doesn't
>
> matter here, but it might have.
>
> 2) When showing two source files, identify where each starts and ends,
>
> and what the second one is called.
>
> 3) When showing an error, include the entire traceback, not just the
>
> last line.
>
>
>
> Now, there are conventions to follow as well (see Pep. One is that
>
> modules should use all lowercase, and classes should begin with a
>
> capital. So the source file of your module should be named
>
> circle.py and the class Circle. When you imported and instantiated
>
> the class, you assumed it was called Circle, but when you defined it,
>
> you mistakenly called it circle.
>
>
>
> The next error is the accidental indentation of the call to main(). As
>
> it stands now, it's a recursive call to itself. And main() will never
>
> be called, because there's no call at top-level.
>
>
>
>
>
>
>
>
>
>
>
> --
>
>
>
> DaveA

Thanks for explanation which was very clear!