1

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))

John_python
  • 45
  • 2
  • 6

1 Answers1

2

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()
Hamperfait
  • 465
  • 1
  • 6
  • 18