This question already has answers here:
How to access a standard-library module in Python when there is a local module with the same name? [duplicate]
(2 answers)
Importing installed package from script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError
(2 answers)
Closed 4 years ago.
This might have nothing to do with celery but here is my problem:
I have an app structured like this:
/app
/__init__.py
/api_1.0/foo.py
/proj
/__init__.py
/celery.py
/tasks.py
So in celery.py I create a celery app:
flask = create_app(os.getenv('FLASKCONFIG') or None)
celery = Celery(__name__,
broker=flask.config['CELERY_BROKER_URL'],
include=['proj.tasks'])
celery.conf.update(flask.config)
and in tasks.py there are lists of celery tasks. One of them is list_users
In foo.py I try to use the task:
from proj import tasks
but this is causing an importation problem when I do:
celery -A proj worker --logleve=info
error message:
from proj.celery import celery
ImportError: cannot import name celery
Strange enough, if I remove the creation of the flask app and simply create a celery app, the problem goes away.
it looks like a circular import problem. How to avoid this?
Related
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
This question already has answers here:
Relative imports for the billionth time
(12 answers)
Closed 1 year ago.
So my file system looks something like this
/projects
--setup.py
--setup.cfg
--/app
--/--__init__.py
--/--app.py
--/--file_with_class.py
In my app.py I import file_with_class.py, but when I use setup to install my module with pip install . and try running it, I get this error: ModuleNotFoundError: No module named 'className'
supose file_with_class.py contains a class named className
I'm importing this class in app.py like this:
from file_with_class import className
Also, when executing app.py on its own it works perfectly, the problem comes when creating the package with setuptools and after installing my own package, trying to run it.
You can use relative import:
from . import class
You should use either relative, or fully qualified module name imports:
from . import class
# or, probably better:
import app.class
from app import class
BTW, class is a reserved word. Not sure if this was the module's name in your original code, but if it was you probably want to avoid it.
This question already has answers here:
Django importing another file from another package
(2 answers)
Closed 2 years ago.
In my project, I have an app called pages:
apps/pages/experiments
Within experiments, the relevant files are:
models.py
a folder called parser (which includes an empty file init.py (with 4 underscores)
The folder called parser has python code that I would like to import in models, and use there. I have a line of code:
from parser import Tables
However, when I run the command
python manage.py runserver
I get the following error:
from parser import Tables
ModuleNotFoundError: No module named 'XML_parser'
edit: I just realized that If I remove imports of my own code, and simply try to import models.py within views.py, then I also get the same error, but now "models.py" instead of parser
as it turns out, i have to start importing from the base of my django project:
Django importing another file from another package
Try to run makemigrations and migrate after defining your app name in settings.INSTALLED_APPS
INSTALLED_APPS = [
# ...
'Abc.apps.AbcConfig' // Replace your app's name with 'Abc'
python manage.py makemigrations
python manage.py migrate
I am using celery for for an app that downloads data. The directory structure is:
proj
__init__.py
celeryApp.py
tasks.py
startup.py
fetcher.py
I run celery with celery worker --app=celeryApp:app -l info from inside the proj folder.
I run my app which queues tasks by running: python startup.py. Both startup.py and fetcher.py import tasks.py. In startup.py I create an object of a class Fetcher defined in fetcher.py and pass that as an argument to a task runFetcher in tasks.py.
This results in an error
DecodeError: No module named fetcher
What changes would I need to make, so that I can safely pass such an object to a task?
Update: I added import fetcher to tasks.py which was fine to do as these tasks were closely related to the fetcher. Can anyone suggest a solution which does not require importing fetcher in tasks?
This question already has an answer here:
Absolute import failing in subpackage that shadows a stdlib package name
(1 answer)
Closed 9 years ago.
enter code hereI have both a package name celery and a filename celery.
When I say import celery its trying import the celery file instead of celery package. And it says it's unable to import the Celery
from __future__ import absolute_import
from celery import Celery
celery = Celery('celery_app',
broker='redis://localhost:6379/0',
backend='amqp://',
include=['celery_app.tasks'])
# Optional configuration, see the application user guide.
celery.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
celery.start()
Like below. Is there way to explain that importing the package instead of the file.
Edit:
Tried
from __future__ import absolute_import
import sys
del sys.path[0]
from celery import Celery
But still the same. When I try those commands from python shell; it does not give any error.
Is there something that I am missing?
When I try those commands from python shell; it does not give any error. Is there something that I am missing?
I really don't recommend it, RENAME your file as everyone says, but you could possibly try
import sys, os
[sys.path.remove(i) for i in sys.path if i == os.getcwd() or i == '']
from celery import Celery
Also, you're importing absolute_import but you don't seem to be using it (docs). When importing from your local file celery.py
from .celery import my_func