使argparse在python 2和3中表现相同

时间:2019-06-12 19:04:00

标签: python argparse

因此,我正在使用Error Traceback (most recent call last): File "C:\Users\strohl\Documents\Project\Who\who_db\who_db_tests\test_model_methods.py", line 101, in setUp self.M1 = MixinTest.objects.create(name='M1') File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\query.py", line 422, in create obj.save(force_insert=True, using=self.db) File "C:\Users\strohl\Documents\Project\Who\who_db\models.py", line 132, in save super(MixinTest, self).save(*args, **kwargs) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\base.py", line 741, in save force_update=force_update, update_fields=update_fields) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\base.py", line 779, in save_base force_update, using, update_fields, File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\base.py", line 870, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\base.py", line 908, in _do_insert using=using, raw=raw) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\query.py", line 1186, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\sql\compiler.py", line 1331, in execute_sql for sql, params in self.as_sql(): File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\sql\compiler.py", line 1275, in as_sql for obj in self.query.objs File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\sql\compiler.py", line 1275, in <listcomp> for obj in self.query.objs File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\sql\compiler.py", line 1274, in <listcomp> [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields] File "C:\Users\strohl\Documents\VirtualEnv\Who\lib\site-packages\django\db\models\sql\compiler.py", line 1205, in prepare_value if value.contains_column_references: AttributeError: 'Query' object has no attribute 'contains_column_references' 用python编写程序。但是我的问题是它在python 2和3中的行为不同。这是代码

argparse

在python 2中,它提供输出(仅通过输入import argparse,sys class CustomParser(argparse.ArgumentParser): def error(self, message): sys.stderr.write('\033[91mError: %s\n\033[0m' % message) self.print_help() sys.exit(2) parser = CustomParser(description='Short sample app') subparsers = parser.add_subparsers(title='Available sub-commands', dest="choice", help="choose from one of these") ana = subparsers.add_parser("test", formatter_class=argparse.RawTextHelpFormatter, description = 'test', help ="test") ana.add_argument("-testf", type=int, help="a test flag", required=True) args = parser.parse_args() python file.py标志)

-h

但是,如果我在python 3(Error: too few arguments usage: test.py [-h] {test} ... Short sample app optional arguments: -h, --help show this help message and exit Available sub-commands: {test} choose from one of these test test )中运行相同的命令,则没有输出。我知道是否提供python3 file.py标志,然后可以看到说明。但是我希望仅通过像Python 2一样输入-h就可以显示帮助描述。另外请注意,我不想使子解析器成为必需。

0 个答案:

没有答案
相关问题