how to access function in different folder in python? - python

I have organized Python files in differnt folders:
root_dir
utils/
some_file.py
models.py
In some_file.py I just did this:
import models
But this is not working. I also updated some_file.py with:
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
import models
Still i get:
import models
ModuleNotFoundError: No module named 'models'

Try to import it as follows from within the some_file.py:
import sys
sys.path.append('..')
import models
# Use as follows:
# models.function()

Related

How do I correctly import models in Python if I'm having such directories?

I'm using Django-REST framework with a telegram-bot in here. I need to import models from Django inside my telegram-bot file. I'm getting module not found error and probably thinking something wrong. Telegram-bot file is commands.py and the django models is models.py. The whole project looks like this:
Project directories
I just want to properly import models inside my commands.py file
Here is the possible solution for your question..
add following code inside my commands.py file
import sys
from django.apps import apps
from django.conf import settings
settings.configure(INSTALLED_APPS=['app_name'])
apps.populate(settings.INSTALLED_APPS)
from app_name.models import YourModel
also you may need to update path.
import sys
sys.path.append('path/to/your/django/project')
As you mentioned, You are using Django REST Framework, so you should try with a serializer file. import your model in serializer file, because the syntax is correct 'from app_name.models import YourModel'. Moreover, instead of settings.configure(INSTALLED_APPS=['app_name']) try this in settings.py
file in INSTALLED_APPS = 'app_name.apps.AppNameConfig'.

A Value Error As "attempted relative import beyond top-level package"

My Project looks like this :
https://imgur.com/B7fHzb4
how do I import the views.py in the pages app onto urls.py file of the trydjango app?
when I do :
from ..pages import views
gives
ValueError: attempted relative import beyond top-level package.
when i do:
import pages.views
it says no module pages as hint.

importing models inside django application

I have made an app inside my django application. I'm trying to create a file(testabc.py) in the same directory as views.py and I want to import models in that file.
The name of model is "Example"
Now, in views.py, I import models in the following way:
from .models import Example
a = Example.objects.get()
Here, I am getting proper output
However, in my testabc.py file when I write the same code
I get the following error
from .models import Example
ValueError: Attempted relative import in non-package
You need to add file __init__.py in your folder.
Here is the doc for python module.

Can't import Django model into scrapy project

I have a folder Project which contains Django project called djangodb and Scrapy project called scrapyspider.
So it looks like:
Project
djangodb
djangodb
myapp
scrapyspider
scrapyspider
spiders
items.py
scrapy.cfg
__init__.py
I want to import model Product from myapp app in items.py
The problem is that it returns import error:
from Project.djangodb.myapp.models import Product as MyAppProduct
ImportError: No module named djangodb.myapp.models
Tried many things but couldn't avoid this error. Do you have ideas?
Your problem is that you're trying to do an import from a file that's outside from Django schema, to solve that you can overwrite the sys.pathvar which includes locations as the actual dir, so you can change it to:
import sys
sys.path.insert(0, 'C:\\Users\\your_path\\Project')
sys.path.insert(0, '/path/to/application/Project/') # Linux
# And then import #
from djangodb.myapp.models import Product as MyAppProduct

How to arrange files in Odoo

How do you arrange model files and controller files in their respective folders?
And what do you have to write in the __init__.py file?
Currently I have all my models and controllers in the root folder of the module like this
addons\
-->mymodule\
-->views\
-->view.xml
-->__init__.py
-->__openerp__.py
-->models.py
-->controllers.py
I have tried like this
addons\
-->models\
-->models.py
And then import the models.py using this inside __init__.py
from models import models
But this does not work
addons\
->yourmodule\
->controllers\
->__init__.py
->controllers.py
->models\
->__init__.py
->modelname.py
->__init__.py
->__openerp__.py
Content of the init.py in the controllers folder:
from . import controllers
Content of the controllers.py in the controllers folder:
from openerp import http
Content of the init.py in the models folder:
from . import modelname
Content of the init.py in the module folder:
from . import controllers
from . import models
Content of the openerp.py in the module folder: List of all your xml files (Instruction)

Categories