0

So I have two python scripts- main.py and gui.py in the same directory/folder. But when I try to import main.py in gui.py, it doesn't work. It's the same when I try to import gui.py in main.py

I tried it this way

from main import *

I saw several questions about similar things but nothing works. Please help and thanks in advance! I'm using Pycharm and Python 3.10

JKR
  • 69
  • 1
  • 1
  • 10
  • 1
    Can you share the error. – Shadowcoder Mar 30 '22 at 03:52
  • No error in this way. but if I use `from .main import *`, variables and functions can be used but it gives `attempted relative import with no known parent package` error when compiling. – JKR Mar 30 '22 at 04:05
  • "when I try to import main.py in gui.py, it doesn't work" "I saw several questions about similar things but nothing works." "No error in this way." I don't understand. Is there a problem or not? "variables and functions can be used but it gives attempted relative import with no known parent package error when compiling" You can always "use" (i.e., write the code) any variable or function name, no matter whether it was defined or not. The error is detected *when the program runs*. Python's "compilation" does very little - you can only get a `SyntaxError`. Imports happen at runtime. – Karl Knechtel Mar 30 '22 at 05:10
  • "I saw several questions about similar things but nothing works." Specifically what things did you try, and what happened when you tried them? Please read [ask] and https://stackoverflow.com/help/minimal-reproducible-example. – Karl Knechtel Mar 30 '22 at 05:11
  • "attempted relative import with no known parent package" Okay; did you try searching specifically for questions about this error? Did you try *those* solutions? – Karl Knechtel Mar 30 '22 at 05:12

1 Answers1

1

from . import main

and then, to call a function that belongs to main.py, you should do;

main.function_name()

Sol_is_here
  • 110
  • 9