argparse 模块在 Python 中不起作用

发布于 2024-11-18 23:42:37 字数 452 浏览 4 评论 0原文

我正在尝试让 argparse 模块在 Python 中工作。我的问题是,在全新安装时,我得到以下信息:

File "test.py", line 3, in <module>
import argparse
File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>
parser = argparse.ArgumentParser(description='Short sample app')
AttributeError: 'module' object has no attribute 'ArgumentParser'

test.py is:

import argparse

显然,我遗漏了一些东西。有人可以帮忙吗?

I'm trying to get the argparse module working in Python. My problem is that on a fresh install, I get the following:

File "test.py", line 3, in <module>
import argparse
File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>
parser = argparse.ArgumentParser(description='Short sample app')
AttributeError: 'module' object has no attribute 'ArgumentParser'

test.py is:

import argparse

Clearly, I'm missing something. Can anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2024-11-25 23:42:37

通常,此症状是由于使用您自己的模块遮蔽内置模块而导致的。从错误消息来看:

File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>

看起来您有自己的模块 argparse.py,这导致了问题,因为它是 test.py 试图导入的模块,并且缺少 ArgumentParser。将 argparse.py 重命名为其他名称(并删除任何 .py[c/o] 文件)。

Usually this symptom is the result of shadowing a builtin module with one of your own. And from the error message:

File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>

it looks like you have your own module argparse.py, which is causing the problem, because it's the one which test.py is trying to import, and which lacks ArgumentParser. Rename your argparse.py to something else (and remove any .py[c/o] files).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文