2

Very basic Jupyter question:

I have two cells in a Jupyter notebook. Neither one has a name (neither is named Perceptron.py - I don't know how to name them )

One begins like this:

class Perceptron(object):

and the other does this:

from Perceptron import Perceptron

and I get this error:

ImportError: No module named Perceptron

Clearly I haven't named the first cell "Perceptron.py," but I don't see how to do that. Or, the directory isn't part of the path for classes. What is the best way to resolve this?

3
  • ( Noob ) -- Just coz it was removed from the title Commented Jul 7, 2016 at 14:40
  • ( Noob ) Is the Perceptron class within a file called Perceptron.py? Is Perceptron.py on your Python Path or within the same directory as the file you're calling it from? Commented Jul 7, 2016 at 14:42
  • @tehjoker, thanks, I am in an IDE, and I have not set the Python path within the IDE. I believe the cell (what I want to be Perceptron.py) is in the same directory. I don't know how to set the name of the cell. Commented Jul 7, 2016 at 14:47

2 Answers 2

4

Your comment about "naming" the cells suggests that you consider cells as separate files; however, Jupyter notebook cells are not separate files - they are part of the same file.

If you were writing a python script, you would declare your class, and then execute your class like so:

class Foo:
    def __init__(self):
        self.Bar = "win!"

foo = Foo()
print(foo.Bar)

Notebooks are the same, but you can choose to separate the code into different cells to organize your code:

# Cell 1
class Foo:
    def __init__(self):
        self.Bar = "win!"

and

# Cell 2
foo = Foo()
print(foo.Bar)

Breaking code into cells also allows you to execute the code in a different order than if it were in a regular script. So, if you haven't executed the code that declares the Perceptron class, you can't instantiate a Perceptron instance.

Another approach to debugging your problem is to merge the cells that seem to be misbehaving. There is nothing wrong with putting all of your code in the same cell. If your code won't work all together, something is wrong with your code. Once you get it working, then you can split the code into separate cells.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Gordon. I did merge them, and it works. So I have to try splitting now, separately. Thanks for helping.
2

Since you don't have a file called Perceptron.py, you shouldn't need to do an import statement for that. Cells in Jupyter notebooks can use variables from one another. All you have to do is click on a cell, and press shift and enter to 'run' it.

If you have a class (like Perceptron) in that cell, it should then be accessible from any other cell in the same notebook.

In another cell, you should just be able to do:

foo = Perceptron()

2 Comments

Thanks @Ryan, but it gives me "name 'Perceptron' is not found". Somehow, it can't find the other cell, or I am missing some statement in the cell with Perceptron. I don't have a module statement in the Perceptron cell; it just begins with class Perceptron.
Thanks again @Ryan. I liked the other answer a bit better but you were very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.