I have a Django project with the following structure:
project
apps
news
__init__.py
models.py
hose
tasks.py
Within tasks.py, I have this import
from apps.news.models import Company
When I try to run tasks.py, I get the following error:
ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package
How do I fix this error? I don't understand why this import does not work.
Reference: https://docs.python.org/2/tutorial/modules.html#packages
You need to add an empty __init__.py (4 underscores in total) file in the apps folder for it to be recognized by Python as a package.
In case anybody else is still struggling with this. I had the __init__.py in the app folder but it still didn't work. Running the following command in app parent folder worked for me.
export PYTHONPATH=$PWD
Related
My project structure is:
APP(dir)
app(dir)
model(dir)
metric_data.py
__init__.py
app.py
setup.py
When I'm running app.py with import statement of
from model.metric_data import MetricData
MetricData is a class, I'm able to successfully run the application retrieving data from metric_data.py file. But when I build it as a package and then try importing package app
from model.metric_data import MetricData
this statement is failing. Can anyone help me with the issue
here, I looked on the relative import part and tried but it didn't work.
Try to create a __init__.py file in the model directory.
Im creating my own simple package that i will upload on pypi that generates jokes on my native language. While creating the package I'm bugged with this error : ModuleNotFoundError: No module named 'utils'
Here is my folder structure :
Myjokes/
myjokes/
data/
jokes.json
utils/
helpers.py
__init__.py
jokes.py
when i am in the Myjokes/ directory and i run the python -c "from myjokes import jokes" it runs but when i run the functions inside the jokes file it displays a module not found error : from utils.helpers import get_jokes_by_count , get_joke_by_dialect , pinoy_jokes ModuleNotFoundError: No module named 'utils'
if i go inside myjokes/ directory which is the subdirectory of Myjokes i can run the jokes.py script via import jokes and everything is running. i dont know why this is happenin I'm fairly new to python.
Python doesn't know that your utils/ folder is supposed to be treated like a python package unless you include an __init__.py in that folder as well.
Try adding a blank __init__.py file inside utils/ and see if that works for you.
I created a new utils package and an http_utils file with some decorators and HTTP utility functions in there. I imported them wherever I am using them and the IDE reports no errors, and I also added the utils module to the INSTALLED_APPS list.
However, when launching the server I am getting an import error:
ImportError: No module named http_utils
What am I missing? What else do I need to do to register a new module?
Make sure the package is correct (Include init.py file).
Make sure there are no other utils files in the same directory level. That is if you are importing from utils import http_utils from views.py, there should not be a utils.py in the same folder. Conflict occurs because of that.
You dont have to include the folder in the INSTALLED_APP settings. Because the utils folder is a package and should be available for importing
As stared by Arundas above, since there is a utils.py file I suggest renaming the module to something such as utilities, and also make sure you have a __init__.py file in that directory.
from utilities.http_utils import class_name
ImportError: no module named location.models at the following line:
from location.models import Zipcode
But there is a models.py, an __init__.py, and a Zipcode model in an installed app called location in my project.
Further, the module is easily imported in the python shell using the same command. What could be the problem here? Thanks for your ideas!
Thanks for your commentary #Marcin. Turns out that I had a file called location.py in my views.py folder, which was causing some kind of conflict. I renamed this file location_view.py and voila.
So moral of the story, I guess, is check to make sure that you dont have any name conflicts in an app when you are trying to import an app of the same name..
Hey friends i set my python path as
/home/rohit/test
Here is the structure my test folder
test/
__init__.py
meetinghandler/
__init__.py
meetinghandler.py
db/
__init__.py
models.py
setting.py
manage.py
My problem is when i am trying to import
from test.meetinghandler import meetinghandler
from models.py
i am getting error i.e;
ImportError: No module named test.meetinghandler
please help me out what i am doing wrong .
from test import meetinghandler looks for a test module, which it will not find in your python path.
The reason? You added /home/rohit/test/ to your python path, but that directory doesn't itself contain a test directory :-) Python looks for modules inside the path, that's why.
In your case you'd have to add your home dir to your python path to be able to find it. (Or better, google for virtualenv and so).
Try debugging from the console.
See if the import test works without error. If so, likely there is some error statement in your meetinghandler.py that is not loding the file.
If the import test itself fails on the console, see if the test is in the python path (i.e, as a part of the folder that is present in the settings.INSTALLED_APPS)