22

I have two files:

fig.py

import math
PI=math.pi

class Fig:
    def __init__(self):   
        self.name= " "

And

circle.py

class Circle(Fig):
    def __init__(self, radius):
        self.name= "Circle"
        self.data= ["Radius: ", radius]

But I am trying to load them I whenever I try it jupyter-notebook throws the error:

NameError: name 'Fig' is not defined

I have tried using import fig at the beggining of circle.py and it does not work, neither does running both files. They are both in the same directory.

D1X
  • 5,025
  • 5
  • 21
  • 36

4 Answers4

30

Ok it's not exactly clear what's going wrong because you haven't sent us precisely what you are doing, but here is my guess. If your circle.py file is as follows

import fig
class Circle(Fig):
    def __init__(self, radius):
        self.name= "Circle"
        self.data= ["Radius: ", radius]

This will break because python doesn't know where to find Fig. If instead you write

import fig
class Circle(fig.Fig):
    def __init__(self, radius):
        self.name= "Circle"
        self.data= ["Radius: ", radius]

or

from fig import Fig
class Circle(Fig):
    def __init__(self, radius):
        self.name= "Circle"
        self.data= ["Radius: ", radius]

Everything should work fine. This is because you either have to tell python the namespace through which it can access the class (my first solution) or explicitly import the class (my second solution). The same logic applies if you want to use PI:

import fig
class Circle(fig.Fig):
    def __init__(self, radius):
        self.name= "Circle"
        self.data= ["Radius: ", radius]
        #use PI from fig.py by informing python of namespace
        self.circumference = 2.*fig.PI*radius 

or

from fig import Fig, PI
class Circle(fig):
    def __init__(self, radius):
        self.name= "Circle"
        self.data= ["Radius: ", radius]
        #PI is now explicitly imported so don't need namespace
        self.circumference = 2.*PI*radius
Angus Williams
  • 2,284
  • 19
  • 21
  • With both options inheritance works but the first two lines do not. If I *run circle* PI is not defined. If I run both I get *SyntaxError: invalid syntax*. – D1X Oct 25 '16 at 10:35
4

You need to do from fig import FIG in your circle.py. Also make sure that you have __init__.py file present in the folder which is having circle.py and fig.py.

Please also take as look at:

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

You need to import the class Fig from fig.py in circle.py.

If you just import the module fig, you must access the class Fig through the respective namespace:

class Circle(fig.Fig):
    <snip>

If you want to use the class name directly, import it using from fig import Fig.

Richard Neumann
  • 2,986
  • 2
  • 25
  • 50
0
from packagename.fig import Fig

class Circle(Fig):
    def __init__(self, radius):
        self.name = "Circle"
        self.data = ["Radius: ", radius]

Where packagename is your actual package name.

gogaz
  • 2,323
  • 2
  • 23
  • 31