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..
Related
I want to write my own python package inside django's app. It looks like that: I have secondary app. It totally works except one thing. Someday I'd decided to make my app more structured. And there where problems started. First of all, I've added python package(with simple init.py inside directory) to the app. Then I've added second package inside that package. And when I try to run django.setup() inside packages' files, I'm getting this error: ModuleNotFoundError: No module named '<primary settings container module name goes here>'
How to make my custom package's functions working properly?
The problem is that settings module isn't "viewable" from your package, e.g. you should add the location of main directory to PATH variable, which could be done like this:
from sys import path
from pathlib import Path
path.append(str(Path(__file__).resolve().parent))
Or you can simply add it permanently to system PATH.
I have these two python files.
api.py has a class called API, which is imported by __init__.py using from api import API. Before this file was named __init__.py, this worked perfectly. But when I changed the name to __init__.py so that I could use it with Flask, I now get the error Unable to import 'api' under the from keyword. Doing from directoryName.api import API works, but the directory name is different on my computer and on my VPS. I could just change the name of the directory to make them the same, but I'd prefer if there was a way that I could change the directory name at any point and it wouldn't break the script.
Does anyone know, firstly, why this doesn't work in __init__.py? And secondly, a way to import a file from the same directory in the init file?
Thanks!
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
I am running the project and shows this error. I have googled and tried some of the solutions but doesn't seem to work.
File "/home/bs-094/Dev/backend/busgroup-backend/src/bus_portal_backend/apps/inquiry/forms/inquiry_details.py", line 2, in <module>
from ..models.inquiry import Inquiry
ValueError: attempted relative import beyond top-level package
My folder structure is
busgroup-backend
src
bus_portal_backend
apps
inquiry
models
_init_.py
inquiry.py
forms
inquiry_details.py
It would be great help is anyone helps me solve the problem. I am fairly new to django. Thanks
EDIT:
Here's what solved my problem, I had to import in this way
from bus_portal_backend.apps.inquiry.models import Inquiry
Also, I was running with debugger. My script path was busgroup-backend/src/manage.py, I changed it to /home/bs-094/Dev/backend/busgroup-backend/src/manage.py, which made me run the project successfully.
forms does not appear to contain an __init__.py file, and so Python doesn't think it's a package. Therefore an attempt to import from ..models is an attempt to ascend above the current package's root (because ...busgroup-backend/src/bus_portal_backend/apps/inquiry/forms/inquiry_details.py is actually a standalone module).
Relative imports only work within a package. Therefore you should make both forms and models subpackages of inquiry by creating inquiry__init__.py as well.
If you import from ..models.inquiry import Inquiry then .. refers to
busgroup-backend
src
bus_portal_backend
apps
Which does not have an __init__.py and thusly is not considered a package by python.
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