其他分享
首页 > 其他分享> > 我在脚本中使用__name__变量时遇到运行时警告

我在脚本中使用__name__变量时遇到运行时警告

作者:互联网

我尝试在python脚本的开头使用___name__:

#!/usr/bin/python
# Comments...
# blabla
#

__name__="cigarline_by_pos.py"
__synopsis__ = "cigarline_by_pos.py | INPUT_BAM_FILE  --output OUTPUT_FILE [--output OUTPUT_FILE_R2 --readsplit]"
__example__ = "cigarline_by_pos.py hg18.bam  --output hg18_read1.csv --output  hg18_read2.csv --readsplit"
__date__ = "05/2013"
__authors__ = "Frederic Escudie & Antoine Leleu"
__keywords__ = "SAM Alignment"
__description__ = "Sum by position the status of reads. This provides for example the number of reads that have a mismatch at first base."

__version__ = '2.3.1'


import argparse,re,sys

the rest of the code...

但随后Python打印出一个警告:

cigarline_by_pos.py:29: RuntimeWarning: Parent module 'cigarline_by_pos' not found while handling absolute import
  import argparse,re,sys

我不知道它可能来自什么.

我只知道如果我用___name__删除该行,我的脚本可以工作,但我想要这个变量.

有人能帮助我吗?

解决方法:

您不应在脚本中设置__name__; Python已经设置它并依赖于其他目的的值,例如导入.可以更改python脚本或模块的__name__值,但是在尝试执行此操作之前,您需要知道自己在做什么并了解Python的内部.

一般情况下,不要设置双下划线变量.它们仅供Python使用;仅在将它们记录为可设置时设置它们,例如模块中的__all__.

有关当前使用的双下划线名称(dunder名称)的概述,请参见Python data model documentation.

标签:python,python-2-7,ubuntu-12-04
来源: https://codeday.me/bug/20190901/1780846.html