0

I am trying to find a way to call a function in the __main__ file from a file that i have imported at the top. I do not want to have to add in a import for the __main__ file into the file i am importing into the __main__ file since i want this to work for and file that imports it. Here's an example

main file:

import importedFile

def Function():
    print("test")

and what I want the imported file to do, this code does not work but it explains what I want to do:

__main__.Function()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 1
    _I am trying to find a way to call a function in the main file from a file that i have imported at the top. I do not want to have to add in a import for the main file into the file i am importing into the main file since i want this to work for and file that imports it._ Can you rephrase this? It's tough to keep track of what's going on. – AMC Jan 05 '20 at 20:01
  • Please use PEP8 standards! – Irfanuddin Jan 05 '20 at 20:02
  • You have to import it. – juanpa.arrivillaga Jan 05 '20 at 20:02
  • Or this? [Visibility of global variables in imported modules](https://stackoverflow.com/q/15959534/7509065) – Joseph Sible-Reinstate Monica Jan 05 '20 at 20:02
  • So i have the main file that i click run on, and at the top it is importing another file. I would like the file that is imported to be able to run commands on the main file. I hope this is a bit more clear. –  Jan 05 '20 at 20:02
  • `__main__` is the name of the module, as in `"__main__"` – Tomerikoo Jan 05 '20 at 20:03
  • Would there be a way to access the functions in the "____main____" module? –  Jan 05 '20 at 20:04
  • Yes. *import them*. `import whatever; whatever.Function()` – juanpa.arrivillaga Jan 05 '20 at 20:05
  • `from main import Function` – Tomerikoo Jan 05 '20 at 20:06
  • i have tried `import __main__` but it dosn't seem to work. –  Jan 05 '20 at 20:06
  • Why do you think that would work? What's the name of the file? – juanpa.arrivillaga Jan 05 '20 at 20:08
  • currently the name of the file is "test", but i would like it to work no matter what the file name is –  Jan 05 '20 at 20:08
  • 1
    _but i would like it to work no matter what the file name is_ Can you be more specific? Have you read any guides, or the documentation, on the organization of Python programs and their dependencies? – AMC Jan 05 '20 at 20:09
  • There's no way for two files to import each other. You have to put `Function` into a 3rd file and have both files import that. – luther Jan 05 '20 at 20:12

0 Answers0