import module in Django - python

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.

Related

Python - No module named "modules"

I am currently trying to import some python files inside of a folder named "modules". My file structure is as follows:
src
- classes
- modules
- image generator ( the file where I am trying to import modules)
Error:
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
ModuleNotFoundError: No module named 'modules'
from modules.processing import StableDiffusionProcessingTxt2Img, process_images
I have attempted to add both an __init__.py in my classes folder, as well as my modules folder, but unfortunately that did not resolve any of my problems.
I have verified that the files I am trying to import are infact in my modules folder.
You must import from the projects' root directory. Assuming src/ is the root directory, you must import as follows:
from classes.modules.processing import StableDiffusionProcessingTxt2Img, process_images
Since you are trying to import functions from another python file that's located on another folder, it is having a hard time figuring out where it is located.
Try this:
import sys
sys.path.insert(1,'/src/classes/modules')
import (your python file)
Make sure you keep the above code on the python file where you are trying to import functions from other .py files
Remember, the path is the absolute path from root. Not sure how many more layers you have before src, put accordingly.
Also, the last line is import python file. So, if you have x.py inside modules, try import x. Then you will be able to use functions inside there.

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.

Import custom module to Django

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.

Can't import a module in parent directory on App Engine

Say I have a module called "test_module", but I want to import a file within the parent directory with this kind of folder structure:
/app_root
__init__.py
script_to_import.py
/test_module
test_script.py
Basically I want to be able to import "script_to_import.py" from "test_script.py". I added the following code to test_script.py in order to add the parent directory:
import sys
import os
sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
However, I'm still getting a "ImportError: No module named script_to_import" error when I try to run dev_appserver.py. Any ideas how to add a parent directory to my python path for import?
Thanks!

Categories