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
Related
I have read a couple of threads on StackOverflow but did not find any answer that has fixed my problem. I'm pretty new to Python and cannot figure out how does modeling system works. I do understand the difference between module and package, but that does not help me with my problem. Here goes:
Here's my project structure:
venv/
root/
commons/
config.py
main/
main.py
Here's my config.py:
class Config:
...
Here's my main.py
from commons.config import Config
...
when running (venv) python3 root/main/main.py I get ModuleNotFoundError: No module named 'config'. What am I doing wrong? It is a problem with my app? with my main.py? With the way I execute my main.py?
Execution is done using Python 3.9 on MacOS
The path to config.py is not added to the list of paths where the interpreter looks for modules and hence does find the config module.
A simple workaround is to change in main.py:
from commons.config import Config
to
from root.commons.config import Config
and execute main.py as a module in the project directory with
python -m root.main.main
When the file is executed as a module it will add the path from where it is executed to the paths the interpreter looks for modules and root.commons.config is a absolute reference from then on.
As #Iliya mentioned, the interpreter doesn't find the way to 'config' module.
You can modify your main.py to fix the issue:
import sys
sys.path.append("absolute_path_to_root")
from commons.config import Config
...
or
import os
import sys
# set the parent dir into python's search path
current = os.path.dirname(os.path.realpath(__file__))
parent = os.path.dirname(current)
sys.path.append(parent)
from commons.config import Config
...
After carefully browsing StackOverflow I finally managed to find a solution that works for me using setup.py.
Thank you #np8 for this answer
I'm just getting started with Python and Django. So within my Django application, I created a new module and within that, I want to import some variable defined in the parent module. However, I am getting certain errors while I tried various combinations.
Below is how my directory structure looks like
Now in my kafka_producer.py I am trying to import constants.py.
kafka_producer.py:
from confluent_kafka import Producer
import sys
import logging
import json
from my_app.constants import KAFKA_BROKER_URL
logging.basicConfig()
#Assign Configuration
conf = {'bootstrap.servers': KAFKA_BROKER_URL}
print(conf)
However, I am getting no module found error. What is the correct way of importing modules in Python?
It should work if you write:
from ..constants import KAFKA_BROKER_URL
Through this way, you leave the kafka directory and you are in your my_app directory.
Can you please paste full trace of error, as in :
ModuleNotFoundError: No module named --name
I want to know what it is saying in --name.
One thing you can try is adding path of your app to the environment variable PATH.
Try adding below lines in your kafka_producer.py
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))
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?
I have a script in .openshift/cron/daily that looks like this
#!/usr/bin/python
import sys
import os
sys.path.append(os.environ['OPENSHIFT_REPO_DIR'])
import EmilyBlogModel
EmilyBlogModel.Poll()
EmilyBlogModel.py is in $OPENSHIFT_REPO_DIR
However, when the cron job runs, I get an ImportError
No module named EmilyBlogModel
Why isn't this working?
Have you tried printing the system path before the import statement? OPENSHIFT_REPO_DIR might be pointing to the wrong path.
I'm writing an application in Flask, that consists of two files, app.py and tags.py. app.py imports tags.py, which in turn contains from eyed3 import load. Eyed3 is a Python module to extract mp3 tags from files. For some reason import of Eyed3 suppresses Flask's console output. Usually when starting Flask with python app.py returns:
* Running on http://127.0.0.1:17000/
When Eyed3 is imported, this line doesn't appear. It doesn't matter, whether it is import eyed3 or from eyed3 import load, or if import is in app.py or tags.py, or Flask has debug mode on/off. I even tried to run
import sys
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
just after import or before app.run(), no success. Why and how does mere import of a module hide console output? How can i restore it?
Edit: Order of import doesn't matter. Nothing happens, if i import Eyed3 before Flask, error still holds. Does it have to do with this line of code?
I'm willing to bet it has something to do with the logging manipulations that EyeD3 is doing in this file: https://bitbucket.org/nicfit/eyed3/src/97905ecfcd6c8f8df0349582b90258b154e583b5/src/eyed3/utils/log.py?at=default