4
votes

I am using Python 3 through Jupyter Notebook I have two modules "color.py" and "painting.ipynb" in the same working directory.

color.py

from enum import Enum

class Color(Enum):
    RED = 1
    BLUE = 2

def painting(color):
        if color == Color.RED:
            print("The painting is red")
        elif color == Color.BLUE:
            print("The painting is blue")
        else:
            raise ValueError("The painting is not blue nor red!")

painting.ipynb

import color 
color.painting(Color.RED)

When I try to run "painting.ipynb" in jupyter notebook, I have the following error.

--------------------------------------------------------------------------- NameError Traceback (most recent call last) in () 1 import color 2 ----> 3 color.painting(Color.RED)

NameError: name 'Color' is not defined

I do not understand why I cannot access the color. Am I supposed to call the class ? It is the first time that I use jupyter notebook and Enum. Please help :) Thank you.

1

1 Answers

3
votes

Since you are importing the entire .py file; you'll need to refer to your "Color" enum with the following:

import color 
color.painting(color.Color.RED)

It might be better to import the bits you need individually depending on how frequently you plan to reference that Enum:

from color import Color
from color import painting