'Executing .py file also runs another .py file

I have three files in one dir:

# Untitled-1.py

print("UTITLEDPY")

if __name__== "__main__":
    from telegram.ext import Updater, CommandHandler, InlineQueryHandler
    import logging
    from telegram import InlineQueryResultArticle, InlineQueryResultPhoto, InputTextMessageContent

and

# test.py
import google_image_search

print("TESTPY")

and this one

# google_image_search.py

print("IMAGESPY")

When I run python3 Untitled-1.py, I have the following output:

UTITLEDPY
IMAGESPY
TESTPY

Please, explain why. The expected output is UTITLEDPY only.

[EDIT]

I added raise ValueError() to test.py:

# test.py
import google_image_search

print("TESTPY")

raise ValueError()

After that running python3 Untitled-1.py gives the following output:

UTITLEDPY
IMAGESPY
TESTPY
Traceback (most recent call last):
  File "Untitled-1.py", line 5, in <module>
    from telegram.ext import Updater, CommandHandler, InlineQueryHandler
  File "/usr/local/lib/python3.7/site-packages/telegram/__init__.py", line 47, in <module>
    from .files.file import File
  File "/usr/local/lib/python3.7/site-packages/telegram/files/file.py", line 24, in <module>
    from future.backports.urllib import parse as urllib_parse
  File "/usr/local/lib/python3.7/site-packages/future/backports/__init__.py", line 14, in <module>
    import_top_level_modules()
  File "/usr/local/lib/python3.7/site-packages/future/standard_library/__init__.py", line 810, in import_top_level_modules
    with exclude_local_folder_imports(*TOP_LEVEL_MODULES):
  File "/usr/local/lib/python3.7/site-packages/future/standard_library/__init__.py", line 781, in __enter__
    module = __import__(m, level=0)
  File "/Users/arturzielinski/Desktop/ibodi_bot/test.py", line 5, in <module>
    raise ValueError()
ValueError

[EDIT2]

Renaming test.py to test1.py solved the issue. I now get the expected output.



Solution 1:[1]

Renaming test.py to test1.py solved the issue. I now get the expected output.

Solution 2:[2]

The culprit is that TOP_LEVEL_MODULES from https://python-future.org/_modules/future/standard_library.html imports test. Which happens to collide with your file that takes precedence.

TOP_LEVEL_MODULES = ['builtins',
                 'copyreg',
                 'html',
                 'http',
                 'queue',
                 'reprlib',
                 'socketserver',
                 'test',
                 'tkinter',
                 'winreg',
                 'xmlrpc',
                 '_dummy_thread',
                 '_markupbase',
                 '_thread',
                ]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ibodi
Solution 2 Sopel