John_python
John_python

Reputation: 45

Import class form another python file Error

I have three file .py. Main.py, one_file.py, second_file.py. In one_file.py, I have a class named Create.

In the main.py I have something like this:

import one_file
var = input("yes or no?")
if var == "yes":
    Create()

But I receive this error NameError: name 'Create' is not defined. I also tried with from one_file import Create and from . import one_file but it does not work anyway.

Code in one_file.py:


import random
class Create:
    def __init__(self):
        self.word()

    def word(self):
        word_list = ["hello", "word", "sun", "moon"]
        print("This is your word")
        print(random.choice(word_list))

Upvotes: 1

Views: 189

Answers (1)

Hamperfait
Hamperfait

Reputation: 515

If you want to run the code from an imported library, you should first call the imported library.

import one_file
one_file.Create()

Otherwise, you can try

from one_file import Create
Create()

Upvotes: 2

Related Questions