执行.py文件还会运行另一个.py文件

时间:2020-02-16 16:05:08

标签: python python-3.x

我在一个目录中有三个文件:

# Untitled-1.py

print("UTITLEDPY")

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

# test.py
import google_image_search

print("TESTPY")

还有这个

# google_image_search.py

print("IMAGESPY")

运行python3 Untitled-1.py时,将显示以下输出:

UTITLEDPY
IMAGESPY
TESTPY

请解释原因。预期的输出仅为UTITLEDPY

[编辑]

我将raise ValueError()添加到了test.py

# test.py
import google_image_search

print("TESTPY")

raise ValueError()

运行python3 Untitled-1.py之后,将显示以下输出:

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]

test.py重命名为test1.py解决了该问题。现在,我得到了预期的输出。

1 个答案:

答案 0 :(得分:1)

test.py重命名为test1.py解决了该问题。现在,我得到了预期的输出。