为什么这个 Python 代码运行了两次? [英] Why is this Python code running twice?

查看:63
本文介绍了为什么这个 Python 代码运行了两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有这两行的 Python 脚本:

I have a Python script with just these 2 lines:

import requests
print len(dir(requests))

它打印:

12
48

当我打印实际列表dir(requests)时,我得到:

When I print the actual list dir(requests), I get this:

['__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__']
['ConnectionError', 'HTTPError', 'NullHandler', 'PreparedRequest', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'adapters', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'utils']

我猜有多个 requests 模块或类似的东西.请帮忙.

I'm guessing there are multiple requests modules or something like that. Please help.

推荐答案

您为脚本指定了标准模块的名称或由 requests 包导入的其他名称.您创建了循环导入.

You gave your script a name of a standard module or something else that is imported by the requests package. You created a circular import.

yourscript -> import requests -> [0 or more other modules] -> import yourscript -> import requests again

因为 requests 没有在您第一次看到支持对象列表中的这些差异时完成导入.

Because requests didn't complete importing the first time you get to see these differences in the list of supported objects.

不要那样做.将您的脚本重命名为其他名称,一切都会好起来的.

Don't do that. Rename your script to something else and it'll all work.

这篇关于为什么这个 Python 代码运行了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆