How to import the right package (python-django) - python

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

Related

Module 'project_name' has no attribute 'celery'

I'm trying to set up a background task using celery and rabbitmq on django but I'm getting an error saying that my project has no attribute celery. I'm using PyCharm and installed celery through that.
I'm new to celery but I've read a lot of articles similar to this issue (AttributeError: module 'module' has no attribute 'celery' this one seems the closest but not sure it's quite the same)
Project structure:
project_name
├──project_name
| ├── settings.py
├──app_name1
| └──celery.py
├──app_name2
| └──tasks.py
I run the following command:
celery -A project_name worker -l info --pool=solo
But I get the following error:
Error: Invalid value for "-A" / "--app":
Unable to load celery application.
Module 'project_name' has no attribute 'celery'
celery.py file:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings')
app = Celery('project_name')
app.config_from_object('django.config:settings', namespace='CELERY')
app.autodiscover_tasks()
tasks.py file:
from __future__ import absolute_import, unicode_literals
from celery import shared_task
#shared_task
def add(x, y):
return x + y
Just for the record: Put celery.py file into the main project file, not inside the app.
Try to enter into the project via the terminal. Write:
cd project_name
It worked with me

Importing celery task within Django view

I am trying to execute a celery task that among other things, communicates with a specific Django view with a pipe.
I have been trying all day to import the celery tasks file (tasks.py) from the Django views (views.py) without any success.
I have also checked the file permissions but this was not the case.
I have added to sys.path the path of the file desired to import (tasks.py), and then import it, but I keep getting an ImportError. However, when trying to import another script on the same folder (script1), the import succeeds.
views.py
...
sys.path.append('/home/celery')
import script1 #SUCCEEDS
# Trying to import "/home/celery/tasks.py" here
import tasks #FAILS
...
tasks.py
...
#app.task
def start_operation(client,**kwargs):
# Pipe comes here
...
The celery directory structure is the following:
/home
├── celery
├── script1.py
tasks.py
And the Django proyect directory structure:
/myapp
├── myapp
├── views.py
Thanks in advance.

How to import from project level package in Django without conflicting with app level module with same name?

I have a Django project (Python 2.7.15) with the following structure:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
utils.py
utils/
__init__.py
filters.py
In my utils/filters.py file I have a class MyFilter. From polls/admin.py, however, when I try to run from utils.filters import MyFilter, I get ImportError: No module named filters. How can I import my custom filter inside the polls app without renaming the polls/utils.py module or the utils package?
NOTE: This it's not a circular import problem. This happens even if I don't import anything from utils/filters.py. It's a name conflict between utils/ and polls/utils.py. Python tries to find filters.MyFilter inside polls/utils.py and it doesn't find it so it throws the error. I just want to figure out a way to bypass this conflict and force python to look for filters.MyFilter inside the utils/ package in the project root.
In Python 2, import utils is ambiguous because it can be a relative or an absolute import.
If you enable the Python 3 behaviour by adding the following import to the top of your module,
from __future__ import absolute_import
then from utils.filters import MyFilter will be treated as an absolute import and will work.
Once you have added the future import, you would need to use an explicit relative import import .utils if you wanted to import polls/utils.py from polls/admin.py.

Django and aiohttp together under the same project

Under my Django project I have created a directory for an aiohttp service.
1) How is the best way to structure it?
This is my current structure:
myproject/
myservice/
__init__.py
service.py
utils.py
myproject/
__init__.py
settings.py
urls.py
uwsgi.py
manage.py
2) If my service needs to import some settings from myproject.settings, how can I do it? Should I move service.py under the root?
I get:
ImportError: No module named 'myproject'
Your server.py script, which uses aiohttp should be next to your manage.py file.
You should probraly also add:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
from django import setup
setup()

Why won't my class import in Python?

Edit: __init__.py files are included, but I'm using Python 3 - I don't think it matters.
Another edit: Anything in config.py will import with no problem. If I simply omit from cache import Cache then no errors. Interestingly, no errors occur when importing Config in config.py
I cannot figure out what's wrong here. I'm getting an error whenever I try to import a specific class. Here's what my project layout looks like:
app/
dir1/
config.py
cache.py
manager.py
__init__.py
test/
test.py
__init__.py
cache.py:
import sys
import os
sys.path.append(os.path.dirname(__file__))
from manager import Manager, AnotherClass
from config import Config
manager.py
import sys
import os
sys.path.append(os.path.dirname(__file__))
from config import Config
from cache import Cache
test.py
cwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(os.path.join(cwd, os.pardir)) + '/dir1')
from cache import Cache, AnotherClass
from manager import Manager
test = Cache()
...
So when I run test.py I get this:
File "/path/to/project/app/dir1/<module>
from cache import Cache
ImportError: cannot import name 'Cache'
from manager import Manager line 5,
Even though config.Config loads just fine, no errors there, but as soon as I try to import cache.Cache it suddenly can't find or import any class in cache.py. All files have the same permissions. Can someone tell me what's wrong here?
You are missing the __init__.py file in your module
app/
__init__.py
dir1/
__init__.py
config.py
cache.py
manager.py
test/
test.py
and instead of messing with sys.path should do a relative import like
from .config import Config
from .cache import Cache
Python 2 may also need a line
from __future__ import absolute_import
before those imports.

Categories