gpt4 book ai didi

python - 执行 .py 文件也会运行另一个 .py 文件

转载 作者:行者123 更新时间:2023-12-01 06:24:03 32 4
gpt4 key购买 nike

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

# 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

[编辑2]

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

最佳答案

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

关于python - 执行 .py 文件也会运行另一个 .py 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60250396/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com