简体   繁体   中英

How to import the right package (python-django)

I have a django application I've added celery. In django application I have a package named 'parser' , 'api' . I configured the celery as I followed the following tutorial: First steps with Django . In parser package I have 'models.py' . Do you 'task.py' package 'api'. When I try to do 'from parser import models' in api package . I get the following error: No module named models

I looked and found that the following import file: lib/python2.7/lib-dynload/parser.x86_64-linux-gnu.so

webapp/               
  manage.py         
  api/       
    __init__.py
    models.py
    views.py
    tasks.py
    ...
  parser/       
    __init__.py
    models.py
    views.py
    ...
  settings/
    __init__.py
    base.py
    celery.py
    dev.py
    live.py
    local.py
    urls.py
    wsgi.py

In case I need 'models.py' of parser package. Command you use to start the celery is following: celery -A settings worker --loglevel=info . When I run celery in manage.py then take the right file: python manage.py celery -A settings worker --loglevel=info

api/task.py

from __future__ import absolute_import, division, print_function, unicode_literals
import time
from celery import task
from parser.models import FileUploadProcess # Error import


@task()
def test_task(param1):
    print("Test task called. Param: {}".format(param1))
    return 42


@task()
def parse_file(file_candidate, candidate_id):
    FileUploadProcess(candidate_id=candidate_id, is_process=True).save()
    # parse file
    time.sleep(15)
    FileUploadProcess.objects.filter(candidate_id=candidate_id).update(is_process=False)

Can somehow tell me Imports right package?

'from parser import models'

You need to use is so:

 from parser.models import ClassName

where ClassName is name of class you want to import

or just

 import parser.models as models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM