2013-03-18 81 views
3

使用__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__我的腳本的作品,但我想要這個變量。

有人可以幫我嗎?

回答

5

您不應在腳本中設置__name__; Python已經設置了它,並將其用於其他用途,例如導入。 Python腳本或模塊的__name__值可以更改,但您需要知道自己在做什麼,並在嘗試這樣做之前瞭解Python的內部組件。

不要設置雙下劃線變量一般甚至。它們僅供Python使用;只有當它們被記錄爲可設置時才設置它們,例如模塊中的__all__

請參閱Python data model documentation以瞭解雙下劃線名稱(dunder名稱)當前正在使用的概述。

+0

非常感謝您的回答!我不知道這個,現在我不那麼愚蠢;) – antoine4790 2013-03-19 10:26:25

+0

沒問題,你做得很好。 :-) – 2013-03-19 10:42:44

+0

我只注意到,如果我移動所有的行下面的導入線下劃線變量,它的作品。我只是不確切地知道它爲什麼與它有關。 – antoine4790 2013-03-19 14:04:16

相關問題