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

7
  • from one_file import Create should work. What happens when you try that? What's the error message? Commented Sep 17, 2018 at 9:38
  • Have you try this method This method works. I tried before Commented Sep 17, 2018 at 9:38
  • one_file.Create() should also work Commented Sep 17, 2018 at 9:39
  • if i try from one_file import Create ImportError: cannot import name Create Commented Sep 17, 2018 at 9:46
  • 1
    Can your post code of one_file.py Commented Sep 17, 2018 at 9:47

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

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.