【问题标题】:Django-extension runscript No (valid) module for scriptDjango-extension runscript 脚本的无(有效)模块
【发布时间】:2017-05-14 18:59:44
【问题描述】:

我正在尝试创建一个脚本,该脚本将使用从文本文件中提取的信息填充我的模型families。 这是我在StackOverflow 的第一篇文章,如果问题表达得不好或格式不正确,请轻描淡写,抱歉。

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.

【问题讨论】:

    标签: django python-3.x django-models django-extensions


    【解决方案1】:

    尝试将空文件 __init__.py(双下划线)添加到您的 /scipts 文件夹并运行:

    python manage.py runscript scipts.familiespopulate
    

    【讨论】:

    • 我试图执行你的代码,但结果是一样的。 python manage.py runscript scipts.familiespopulate 找不到脚本'scipts.familiespopulate'的(有效)模块尝试以更高的详细级别运行,例如:-v2 或 -v3
    【解决方案2】:

    除了添加 init.py 之外,您不应该在 run 方法中传递任何参数。

    def run():
         <your code goes here>
    

    【讨论】:

    • 感谢您的回复,脚本目录中有init.py。我尝试在 def run() 中移动 file_path 定义,但执行仍然返回并出现以下错误:(.VirEnvStructuRna) work@pboccaletto  ~/Desktop/StructuRNA/website   master ●  python manage.py runscript familypopulate Exception while在“scripts.familiespopulate”中运行 run()
    • 是的。我解决了这个问题。我现在要发布解决方案。
    【解决方案3】:

    感谢有用的 cmets。 我以这种方式修改了我的代码:

    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],
        )
    

    这就是全部。现在它工作顺利。 我想向大家确认我的文件:familypopulate.py 位于文件夹脚本中,文件为 init.py

    我放的时候问题好像解决了

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

    在run函数内部,从run(file_path)中移除参数file_path。

    对我的代码的另一个修改是 open(file_path, 'r') 中的参数 r,在它应该对应于读取二进制文件的 open(file_path, 'rb') 之前。

    【讨论】:

      【解决方案4】:

      我也遇到了完全相同的错误,我尝试了上述所有解决方案,但不幸的是对我不起作用。然后我意识到我的错误,我找到了。

      在脚本文件(位于脚本/文件夹内)中,我为函数使用了不同的名称,应该命名为“运行”。因此,如果您收到此错误,请确保您也检查了它。

      Here you can read more about "runscript"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-17
        • 1970-01-01
        • 2015-08-01
        相关资源
        最近更新 更多