2016-11-25 75 views
1

clean.py我:导入错误python中

import datetime 
import os 

from flask_script import Manager 
from sqlalchemy_utils import dependent_objects 

from components import db, app 
from modules.general.models import File 
from modules.workflow import Workflow 

manager = Manager(usage='Cleanup manager') 

@manager.command 
def run(dryrun=False): 

    for abandoned_workflow in Workflow.query.filter(Workflow.current_endpoint ==     "upload.upload_init"): 
     if abandoned_workflow.started + datetime.timedelta(hours=12) < datetime.datetime.utcnow(): 
      print("Removing abandoned workflow {0} in project {1}".format(
       abandoned_workflow.id, abandoned_workflow.project.name 
     )) 

     if not dryrun: 
      db.session.delete(abandoned_workflow) 

    db.session.commit() 


    for file in File.query.all(): 
     dependencies_number = dependent_objects(file).count() 

     print("File {0} at {1} has {2} dependencies".format(file.name, file.path, dependencies_number)) 

     if not dependencies_number: 
      file_delete(file, dryrun) 

     if not dryrun: 
      db.session.delete(file) 

db.session.commit() 

# List all files in FILE_STORAGE directory and delete ones tat don't have records in DB 
all_files_hash = list(zip(*db.session.query(File.hash).all())) 

for file in os.listdir(app.config['FILE_STORAGE']): 
    if file.endswith('.dat'): 
     continue 

     if file not in all_files_hash: 
      file_delete(os.path.join(app.config['FILE_STORAGE'], file), dryrun)enter code here 

我需要开始高清的run() 在控制台我写: python clean.py 我有输出:

`Traceback (most recent call last): 
File "cleanup_command.py", line 7, in <module> 
from components import db, app 
ImportError: No module named 'components' 

clean.py位于C:\ App \ model \ clean.py

components.py位于 - C:\ components.py

Workflow.py位于 - C:\模块\工作流程\ Workflow.py

请告诉我,可能是什么问题呢?

回答

0

要在调用python clean.py时开始运行(),请在脚本的末尾添加这些行。

if __name__ == '__main__': 
    r = run() 
    ## 0-127 is a safe return range, and 1 is a standard default error 
    if r < 0 or r > 127: r = 1 
    sys.exit(r) 

也作为尤金Primako提到最好将您的代码文件重新定位在一个位置。

from components import db, app 
ImportError: No module named 'components' 

这意味着它在放置clean.py的位置查找名为components.py的脚本。这就是你有导入错误的原因。