Mark Mark - 1 year ago 91
Python Question

ImportError in python

In I have:

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')

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(,

if not dryrun:


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

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

if not dependencies_number:
file_delete(file, dryrun)

if not dryrun:


# 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'):

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

I need start def run()
in console I write:

And I have outputs :

`Traceback (most recent call last):
File "", line 7, in <module>
from components import db, app
ImportError: No module named 'components' is located in- C:\App\model\ is located in - C:\ is located in - C:\modules\workflow\

Please, tell me what could be the problem?

Answer Source

The problem is that modules for import are searched in certain locations:

In your case you can put all source directory paths in PYTHONPATH var like:

PYTHONPATH=... python

But I guess it would be better to relocate your code files (i.e. put all the libs in one location)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download