0

I have an issue whilst importing code into my program. I have a file dedicated to functions that I can call from anywhere inside my program. I use a function called clear(), which looks something like this:

def clear():
   os.system("cls")

All this does is clears the terminal. Basically, I recently redid my code, and now whenever I use it anywhere in my program, it throws back one of 2 errors. Either:

File "C:\Users\Owner\Desktop\Programming\Python\project\main.py", line 90, in <module>
    home()
  File "C:\Users\Owner\Desktop\Programming\Python\project\homescreen.py", line 89, in home
    main_menu()
  File "C:\Users\Owner\Desktop\Programming\Python\project\homescreen.py", line 37, in main_menu
    clear()
NameError: name 'clear' is not defined

If i import my functions like this: from functions import *. Or this:

Traceback (most recent call last):
  File "C:\Users\Owner\Desktop\Programming\Python\project\main.py", line 3, in <module>
    from functions import *
  File "C:\Users\Owner\Desktop\Programming\Python\project\functions.py", line 8, in <module>
    from homescreen import home
  File "C:\Users\Owner\Desktop\Programming\Python\project\homescreen.py", line 3, in <module>
    from functions import clear
ImportError: cannot import name 'clear' from partially initialized module 'functions' (most likely due to a circular import) (C:\Users\Owner\Desktop\Programming\Python\project\functions.py)

If I import it like this: from functions import clear

A factor of this problem may be because my python program is spread across 3 files, but I couldn't find any illegal imports. Any feedback would be greatly appreciated! Thanks!

shoebob
  • 71
  • 7
  • 1
    "NameError: name 'clearterm' is not defined": which indeed is nowhere else defined here in the question. So you are not showing all the relevant code, because Python doesn't dream up random names. You are using `clearterm` somewhere. Same goes for the second error: you are not showing all of the relevant code. – 9769953 Jun 07 '22 at 01:53
  • 3
    "my python program is spread across 3 files,": and when done incorrectly, circular imports may happen. – 9769953 Jun 07 '22 at 01:55
  • You mean you did *not* copy-paste the error message? – 9769953 Jun 07 '22 at 01:55
  • Please include the full traceback error. – ewokx Jun 07 '22 at 01:55
  • 1
    Did you read the part about circular imports in the error message? – Barmar Jun 07 '22 at 01:58
  • 2
    `homescreen` imports something from `functions`, and `functions` imports something from `homescreen`. That is a circular import. Rearrange your functionality, so that you avoid that. – 9769953 Jun 07 '22 at 02:04

0 Answers0