2016-12-30 27 views
1

我試圖創建一個腳本,將填充我的模型families與從文本文件中提取的信息。 這是我的第一篇文章,在StackOverflow,請溫柔,對不起,如果問題沒有很好地表達或格式不正確。Django-extension腳本編號爲(有效)的腳本

Django的V 1.9和Python的運行3.5
Django的擴展安裝

這是我的模型:它在叫browse

from django.db import models 
from django_extensions.db.models import TimeStampedModel 


class families(TimeStampedModel): 
    rfam_acc = models.CharField(max_length=7) 
    rfam_id = models.CharField(max_length=40) 
    description = models.CharField(max_length=75) 
    author = models.CharField(max_length=50) 
    comment = models.CharField(max_length=500) 
    rfam_URL = models.URLField() 

這裏的應用程序,我有我的劇本familiespopulate.py。定位在PROJECT_ROOT/scripts目錄中。

import csv 
from browse.models import families 

file_path =  "/Users/work/Desktop/StructuRNA/website/scripts/RFAMfamily12.1.txt" 

def run(file_path): 
    listoflists = list(csv.reader(open(file_path, 'rb'), delimiter='\t')) 
    for row in listoflists: 
     families.objects.create(
       rfam_acc=row[0], 
       rfam_id=row[1], 
       description=row[3], 
       author=row[4], 
       comment=row[9], 
     ) 

從終端當我運行:

python manage.py runscript familiespopulate 

返回:

No (valid) module for script 'familiespopulate' found 
Try running with a higher verbosity level like: -v2 or -v3 

的問題必須在導入模型families,我是新來django,我做不到在StackOverflow或其他任何網上找到任何解決方案。 這就是爲什麼我要求你的幫助!

你知道應該如何導入模型嗎? 或...我在做別的事情嗎?

重要的信息是腳本運行,如果我修改它來打印出參數,而不是在家庭中創建一個對象。

爲了您的信息和好奇心,我還會在這裏發佈我正在使用的文本文件的摘錄。

RF00001 5S_rRNA 1302 5S ribosomal RNA Griffiths-Jones SR, Mifsud W, Gardner PP Szymanski et al, 5S ribosomal database, PMID:11752286 38.00 38.00 37.90 5S ribosomal RNA (5S rRNA) is a component of the large ribosomal subunit in both prokaryotes and eukaryotes. In eukaryotes, it is synthesised by RNA polymerase III (the other eukaryotic rRNAs are cleaved from a 45S precursor synthesised by RNA polymerase I). In Xenopus oocytes, it has been shown that fingers 4-7 of the nine-zinc finger transcription factor TFIIIA can bind to the central region of 5S RNA. Thus, in addition to positively regulating 5S rRNA transcription, TFIIIA also stabilises 5S rRNA until it is required for transcription. NULL cmbuild -F CM SEED cmcalibrate --mpi CM cmsearch --cpu 4 --verbose --nohmmonly -T 24.99 -Z 549862.597050 CM SEQDB 712 183439 0 0 Gene; rRNA; Published; PMID:11283358 7946 0  0.59496 -5.32219 1600000 213632 305 119 1 -3.78120 0.71822 2013-10-03 20:41:44 2016-04-21 23:07:03 

這是第一行,並從listoflists提取的結果是:

RF00002 
5_8S_rRNA 
5.8S ribosomal RNA 
Griffiths-Jones SR, Mifsud W 
5.8S ribosomal RNA (5.8S rRNA) is a component of the large subunit of the eukaryotic ribosome. It is transcribed by RNA polymerase I as part of the 45S precursor that also contains 18S and 28S rRNA. Functionally, it is thought that 5.8S rRNA may be involved in ribosome translocation [2]. It is also known to form covalent linkage to the p53 tumour suppressor protein [3]. 5.8S rRNA is also found in archaea. 

回答

0

除了添加init .py你不應該在run方法中傳遞任何參數。

def run(): 
    <your code goes here> 
+0

感謝您的答覆,該init.py是存在於腳本目錄中。 我試圖移動def run()中的file_path定義,但執行仍然返回此錯誤: (.VirEnvStructuRna)工作@ pboccaletto〜/ Desktop/StructuRNA /網站主python manage.py runscript familiespopulate 在'scripts.familiespopulate'中運行run()時出現異常 – Peter

+0

您的腳本文件夾位於manage.py所在的目錄中嗎? –

+0

是的。我解決了這個問題。我現在要發佈解決方案。 – Peter

1

嘗試增加空文件__init__.py(雙下劃線)到你的/ scipts文件夾,並運行:

python manage.py runscript scipts.familiespopulate 
+0

我試着執行你的代碼,但結果是一樣的。 蟒蛇manage.py的runScript scipts.familiespopulate 沒有(有效的)模塊,腳本「scipts.familiespopulate」發現 嘗試用更高的詳細級別如跑步:-V2或-V3 – Peter

0

感謝您的有用評論。 我以這種方式修改了我的代碼:

import csv 
from browse.models import families 


def run(): 
    file_path = "/Users/work/Desktop/StructuRNA/website/scripts/RFAMfamily12.1.txt" 
    listoflists = list(csv.reader(open(file_path, 'r'),delimiter='\t')) 
print(listoflists) 
for row in listoflists: 
    families.objects.create(
       rfam_acc=row[0], 
       rfam_id=row[1], 
       description=row[3], 
       author=row[4], 
       comment=row[9], 
    ) 

這就是全部。現在它運行順利。 我想向大家確認我的文件:familiespopulate.py位於文件夾腳本中,文件爲init。PY

的問題似乎得到解決,當我把

file_path = "/Users/work/Desktop/StructuRNA/website/scripts/RFAMfamily12.1.txt" 

裏面的運行功能,除去運行(FILE_PATH)參數FILE_PATH。

我的代碼的另一個修改是open(file_path,'r')打開前的參數r(file_path,'rb'),它應該可以讀取二進制文件。