Import custom module to Django - python

I'm making a django app and cannot import a custom module inside the views.py file.
I'm trying to import class "Integrate" from auth.py inside modules folder from integrations/views.py
I tried placing init.py inside the app folder and modules folder but still doesn't work.
views.py:
from ..modules.auth import Integrate
Powershell:
from ..modules.auth import Integrate
ValueError: Attempted relative import beyond toplevel package

I do this a lot in my projects. Creating custom modules and importing them.
Try this:
from modules.auth import Integrate

You can use this :
from .models import Integrate
It will be work for use models

I am pretty certain I had this exact problem.
It helps if you make the directory holding the module a 'Sources Root'
You right click on the directory and down the bottom of the pop-up is 'Mark Directory As' option.
AFAIK this adds that directory to the PythonPath so the module in there will be found.

Related

import module in Django

I have a project with structure like on this picture.
Folders structure
Where 'backend' folder is Django project folder.
I need to import module from another folder 'main' inside Django app file, i.e. import main.Text_Generator in backend.app.views file.
I tried: from ...main.Text_Generator import *. This raise an error while running a server: "attempted relative import beyond top-level package"
And from main.Text_Generator import *, also error "No module named 'main'"
What is the correct way to do such import?
Add this:
import sys
sys.path.append("..")
And then you should be able to get it with:
from main.Text_Generator import *
You're using a module outside your Django project. I would recommend moving the folder inside your project directory [or app directory] rather than messing with your PATH. If you move main inside backend your existing stuff will work.

Django No Module Named With Inner Package Using

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.

Can't import modules - ImportError: No module named

I've created a new app called engineapp. Inside this app, there is a folder engine which is a Scrapy project.
When I try to import model from storage app inside top.py file, it returns:
from storage.models import TopItem
ImportError: No module named storage.models
Or the similar problem, when I try to import settings of scrapy project:
from engineapp.engine.engine import settings
It returns:
from engineapp.engine.engine import settings
ImportError: No module named engineapp.engine.engine
This is when I run scrapy project from command line.
Both imports created PyCharm itself.
As you can see, I've added __init__() everywhere so python would be able to recognize those files.
Do you know what should I do to be able to import those files?
PyCharm autocomplete relies on different IDE settings. You've marked your realstate_scanner as sources root, so PyCharm can resolve imports for this. From docs:
These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.
If you can't import some module in python, you should check PATH/PYTHONPATH variables first to make sure python interpreter knows where to find your module.

global/app lib folders import in Django

I have a Django structure like this (only showing libs):
project/lib/ # Global libraries that will be used cross apps
project/lib/global_stuff.py
project/apps/app1/lib/special_for_app1.py
project/apps/app2/lib/special_for_app2.py
Some apps don't have a lib folder.
from apps.app1.lib import special_for_app1 works fine. But how can I import from the global lib folder when I am inside a folder already containing a local lib folder?
From inside the apps views.py file on one of the apps:
from lib import global_stuff
Gives me ImportError: cannot import name global_stuff
from .lib import global_stuff
Gives me ImportError: cannot import name global_stuff
from ..lib import global_stuff
Gives me ImportError: No module named lib
from ...lib import global_stuff
Gives me ValueError: Attempted relative import beyond toplevel package
from project.lib import global_stuff
Works, but I really dont want to be stuck with using the project name itself in the import.
Is there any way to solve this, not using the project name in the import, or changing the whole lib idea.
Or is there any other good practice to store the main part of the code?
You are correct for not wanting to associate the project name with the imports, so there is a common pattern for this:
project/
|__/source/
| |__/lib/
| |__/app/
|__/deployment/ # code for your production deployment maybe
|
|__/docs/
|__/tests/
|__README
|__requirements.txt
and put /path/to/project inside your paths on your virtualenv(you use virtualenv right?).
Then you can do inside your code
from source.lib.blah import foo
from source.app.baz import bar
EDIT: This is only optimal if you don't release your code as open source of course. Only when you have an internal project where the management keeps changing the name of the project :D
I really dont want to be stuck with using the project name itself in the import
Why not? It's the best way. Note that 'relative imports for intra-package imports are highly discouraged', - as said in PEP-8.

Install Django Application so It's Available on Python Path

I have a django application structured like this...
app_foo
__init__.py
urls.py
views.py
models.py
bar_app
__init__.py
...
bar_app...
By using distutils, I can get the application to install into the python path under the "app_foo" module name.
However, any of the code inside of the "bar_app" python files which refers to things inside the django app relatively does not work when executed from the python path. For example,
from bar_app.views import stuff
I know that I can go through the app and change all the references to be absolute. For example,
from app_foo.bar_app.views import stuff
My question:
Is there anyway I can get all of the apps inside "app_foo" to also be on the python path?
Conceptually this would be similar to saying from app_foo import * for the entire path.
You can do
from .bar_app.views import stuff
http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports

Categories