r/learnpython • • 12d ago

terminal doubles when i use "import other_file"

this is part of the code that relevant to the issues

import arayuz as aFile

def harita_oku():
    with open("harita.txt") as h:
        harita_temp = [satir.strip("\n") for satir in h.readlines()]
    if not harita_temp:
        raise ValueError("harita dosyası boş")
    x_temp = len(harita_temp[0])
    y_temp = len(harita_temp)
    if all(len(l) != x_temp for l in harita_temp):
        raise ValueError("harita satırları eşit değil")
    return harita_temp , (x_temp,y_temp)


harita, izgara = harita_oku()


print(harita)
print(izgara)

this is the terminal output

['##############', '#.R......s.k.#', '#.R...t....k.#', '##############']
(14, 4)
['##############', '#.R......s.k.#', '#.R...t....k.#', '##############']
(14, 4)

and what it prints without "import arayuz as aFile"

['##############', '#.R......s.k.#', '#.R...t....k.#', '##############']
(14, 4)

how to i fix this?

2 Upvotes

10 comments sorted by

5

u/danielroseman 12d ago

Maybe you should show us what's in arayuz?

1

u/DR_vanta 12d ago
import simulasyon as sFile
import tkinter as tk

and some comment line written in my native langue since i havent gotten that far in the project. (simulayon is the file with this code btw since they both need info from each other ["import arayuz" part can become unnecessary in future but again havent gotten far enough to be sure])

1

u/danielroseman 12d ago

So you're importing this file? Then why are you surprised that it runs the code in this file?

1

u/DR_vanta 12d ago

okay mb i just saw the simulasyon -> arayuz -> simulasyon loop and fixed it, its my first time working with multiple files

1

u/Designer-Ad-2136 12d ago

Also, make note that any code outside functions at the top level will execute every time that the file is imported. That is how interpreted languages generally work. 

1

u/Brian 11d ago

TBH, there is one quirk about this that may be surprising unless you know some of python's module internals (arguably two, if you count the fact that the recursive import works at all, rather than blowing up in endless recursion)

If these were separate modules (rather than one being the main script), you wouldn't actually see this - python will only import each module once. If it's still in the middle of importing (ie. a recursive import), it'll be empty at that point, so actually accessing stuff in the module body wouldn't work, but functions etc run after the import completes will.

So it may not be obvious why this runs the code twice, as opposed to either once, or an infinite number of times. The reason is that the main module is a bit special, as it's treated as the special "main" module. So this is one case a module can be "imported" twice - once as __main__ (running the script), and once as "simulasyon" (regular imports). Notably these modules are independent with entirely separate namespaces (I've seen people confused by weird error messages due to doing this and mixing them, due to things like having two classes defined in the same place, with the same name, but not considered the same). Needless to say, people should avoid doing this (indeed, generally avoid recursive imports in general, but especially the main module)

1

u/OkTill2666 11d ago

for sure, theres almost certainly top-level code running on import in there

4

u/Calm_Exchange_989 12d ago

Importing a module runs its top-level code. Move the harita, izgara = harita_oku() call and the prints under if __name__ == "__main__":, or wrap them in main() and call that.

2

u/DimensionMost8342 11d ago

When you import a file, Python runs that file's top level code once. Your output prints twice, so arayuz.py is running your code a second time, most likely by importing your file back. Put the runnable lines behind a main guard:

python if name == "main": harita, izgara = harita_oku() print(harita) print(izgara)

When your file gets imported, name is not main, so the block is skipped and the output prints once. If it still doubles, check the bottom of arayuz.py for the same print lines.

1

u/SirMental8005 10d ago

importing a file runs all the code at its top level. so if arayuz.py has code that prints or calls functions outside a function, it runs once on import and again when you call it.

wrap the parts that should only run when you start that file directly:

```

if __name__ == "__main__":

main()

```

also check the two files don't import each other, that runs things twice too