argparse模块在Python中不起作用

时间:2021-02-16 20:28:10

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

我试图让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:

test.py是:

import argparse

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

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

1 个解决方案

#1


42  

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).

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

#1


42  

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).

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