Relative import user made module in Odoo 14 - python

I am doing a refactor of our code base and was suggested we create a python module that will store functions that are used across different modules in Odoo 14 (sale, project for example), this is in a different file in the structure. My question is, Odoo will allow importing the new relative module and that way call the function I need?
Trying to import the module gives me ModulenotFoundError: no module named ...
from rw_utilities import working_hours_calculation
With the research that I've done, this should be possible with a simple python app but inside Odoo for the moment I'm not too clear. We run the application in the Odoo.sh cloud Not local.
File structure the goal is modules like sales and project can use the same function.

If you log something like that.
import logging
_logger = logging.getLogger(__name__)
_logger.info("Test")
Then you should get a log line like that, and it contains the right path to the module:
2022-01-01 00:01:01,111 1 INFO hostname odoo.addons.your_module.models.your_file: Test
And it should be usable like that
from odoo.addons.your_module.models.your_file import your_item
Another way would be:
class MyTools(models.Model):
_inherit = 'my.tools'
#api.model
def _my_function(self):
pass
# to call
self.env['my.tools']._my_function()
# or even
my_function = self.env['my.tools']._my_function
my_function()

Related

Importing a function from another folder

Let's say I have a structure like this:
tests----------------
___init__.py
test_functions_a
functions-----------
___init__.py
functions_a
functions_b
I want to test a function from functions_a, but inside functions_a I am importing a function from functions_b.
When I am trying:
from functions.functions_a import function_aa
I am getting an error, because inside functions_a I have a line:
from functions_b import function_bb
and not:
from functions.functions_b import function_bb
How can I solve this?
Any good practises are welcome, as I have no experience in structuring projects.
According to Google Python Style Guide, you should:
Use import statements for packages and modules only, not for
individual classes or functions. Note that there is an explicit
exemption for imports from the typing module.
You should also:
Import each module using the full pathname location of the module.
If you follow those two conventions, you will probably avoid, in the future, situations like the one you just described.
Now, here's how your code will probably look like if you follow those tips:
Module functions.functions_a:
from functions import functions_b as funcs_b
def function_aa():
print("AA")
def function_aa_bb():
function_aa()
funcs_b.function_bb()
Module functions.functions_b:
def function_bb():
print("BB")
And, finally, test_functions_a.py:
from functions import functions_a as funcs_a
if __name__ == "__main__":
funcs_a.function_aa()
funcs_a.function_aa_bb()
Output:
AA
AA
BB
You cannot directly import the function instead you could import File 1 to some other File and then call the function from that particular file you imported .

Does a package 'see' itself in __init__.py?

I have a flask app with the root folder called project_folder.
A code snippet from the __init__.py file of this project_folder package:
#jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
jti = decrypted_token['jti']
return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti)
from project_folder.Controller.root import root
from project_folder.Controller import auth_controller
from project_folder.Controller import item_controller
Now the interesting thing is, that the project_folder package naturally has other smaller packages itself, which I'm importing to use them (for REST resources in this example). These are the last 3 lines, nothing throws an error so far.
But, if you take a look at the annotated function (in this example it always runs before some kind of JWT Token is being used), I am returning some inner package's function. Now when the logic truly runs this part the code breaks:
PROJECT_ROUTE\project_folder\__init__.py", line 38, in check_if_token_in_blacklist
return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti)
NameError: name 'project_folder' is not defined
After thinking about it, it seems understandable. Importing from project_folder does import from the __init__.py file of the package, which is the actual file the interpreter currently is. So removing the package name prefix form the
return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti)
to
return Model.RevokedTokenModel.is_jti_blacklisted(jti)
does not throw an error anymore.
The question is: Why is it only a problem inside the callback function and not with the last 3 imports?
This has to do with circular imports in python. Circular import is a form of circular dependency, created at the module import level.
How it works:
When you launch your application, python keeps a register (a kind of table) in which it records all the imported modules. When you call somewhere in your code a module, python will see in its registry if it has already been registered and loads it from there. You can access this registry via sys.module, which is actually a dictionary containing all the modules that have been imported since Python was started.
Example of use:
>>> import sys
>>> print('\n'.join(sys.modules.keys()))
So, since Python is an interpreted language, reading and execution of code is done line by line from top to bottom.
In your code, you put your imports at the bottom of your __init__.py file.
While browsing it, when python arrives at the line return project_folder.Model.RevokedTokenModel.is_jti_blacklisted(jti), it will look if the module exists in its register. Which is clearly not yet the case. That's why he raises an NameError: name 'project_folder' is not defined exception.

Cannot import local module in Python despite trying multiple suggestions

I've read through about ten posts on how to import local modules, and I'm still stumped on why this isn't working. I have an extremely simple module, actor.py, with a single class inside it:
class Actor(object):
def __init__(self, name, age):
self.name = name
self.age = age
I'm trying to import it into another module, scraper.py, within the same directory:
Some fixes have listed not having init.py as being a problem with local imports, so I know that's not my problem.
Initially I tried these:
import actor
and
from actor import Actor
but it tells me that actor and Actor are unresolved references. here tells me that's Python 2 syntax, and I'm using Python 3. That answer instead recommends that I do:
from .actor import Actor
When I run my program with that syntax, I get this error:
ModuleNotFoundError: No module named '__main__.actor'; '__main__' is not a package
So I go searching again, and this post tells me to remove the dot from 'actor,' but as stated before, I've tried that as well. My final guess was
from . import actor
but that yields
ImportError: cannot import name 'actor'
which I follow to here, but the answers there mention circular dependencies, and I'm certain actor and scraper have none. Am I perhaps not writing my module correctly? I can't think of any other ways to write an import statement.
edit: if it helps at all, I'm using Intellij
Try from WebScraper.actor import Actor. If this doesn't work its because your package directory is not in the PYTHONPATH. You can set that in the IntelliJ Python run configuration.
The relative import is not working for you because you are trying to run a module as a script. You can see an explanation of what is happening at https://stackoverflow.com/a/8300343/7088038. If you want relative imports to work you will have to add a __main__.py file to your module to allow it to be runnable, or execute from an external script where you use an absolute import so you don't clobber the package namespace.
One other stylistic note- usually (but not always) package names in python use all lowercase names. CamelCase is reserved for class names. So if you wanted to follow convention you would call your package webscraper and use from webscraper.actor import Actor
To import a class into your script use:
from actor import Actor
Or to import the .py entirely (including whatever imports included in it) into the namespace use:
from actor import *

Does Django execute all the python files in its project directory?

I'm not sure what the best way of phrasing this question, but I am noticing that if I have say this file in the project directory:
a.py
import logging
import track
from raven.contrib.django.raven_compat.models import client
from django.conf import settings
from .MetricLogger import log_metrics
_LOG = logging.getLogger('application')
class TrackingClass(track.SubClass):
def record(self, *args, **kwargs):
try:
metrics = {
"metric": 'something',
"trigger": 'request',
"element": 'request'
}
# log_metrics(None, metrics)
except Exception:
client.captureException()
track.tracker.register(EventsappTrackingPixel) # this line gets called at some point without importing this module
The print statement gets called. What is happening and what is triggering it? Is this part of the class indexing that needs to be done or how is this working?
EDIT:
Turns out the track package we depend on is specifically looking for this filename in the root directory and importing it!
Django does not automatically import all files within a directory. It only automatically imports specific predefined files (should they exist), such as models.py and apps.py.
If you have additional custom modules in the directory, like profanityDetector.py, you'll need to manually import them, perhaps within apps.py.
Note that in Django, all modules are imported only once per worker process. This means that if you are not manually importing a.py from another module, it must have been registered as a callback through Django's logging system, possibly due to a pathing or directory mixup.
The call to print is not part of the class definition. It's at the module level, therefore it gets executed when the module is imported.
You can figure out what is triggering the import by inspecting the call stack. Insert these two lines right after (or before) the call to print:
import traceback
traceback.print_stack()

Import statement in library definition does not seem to be executed

I am having a problem that may be quite a basic thing, but as a Python learner I've been struggling with it for hours. The documentation has not provided me with an answer so far.
The problem is that an import statement included in a module does not seem to be executed when I import this module from a python script. What I have is as follows:
I have a file project.py (i.e. python library) that looks like this:
import datetime
class Project:
""" This class is a container for project data """
title = ""
manager = ""
date = datetime.datetime.min
def __init__( self, title="", manager="", date=datetime.datetime.min ):
""" Init function with some defaults """
self.title = title
self.manager = manager
self.date = date
This library is later used in a script (file.py) that imports project, it starts like this:
import project
print datetime.datetime.min
The problem then arises when I try to execute this script with Python file.py. Python then complains with the folliwing NameError:
Traceback (most recent call last):
File "file.py", line 3, in <module>
print datetime.datetime.min
NameError: name 'datetime' is not defined
This actually happens also if I try to make the same statements (import and print) directly from the Python shell.
Shouldn't the datetime module be automatically imported in the precise moment that I call import project?
Thanks a lot in advance.
The datetime module is only imported into the project namespace. So you could access it as project.datetime.datetime.min, but really you should import it into your script directly.
Every symbol (name) that you create in your project.py file (like your Project class) ends up in the project namespace, which includes things you import from other modules. This isn't as inefficient as it might seem however - the actual datetime module is still only imported once, no matter how many times you do it. Every time you import it subsequent to the first one it's just importing the names into the current namespace, but not actually doing all the heavy lifting of reading and importing the module.
Try thinking of the import statement as roughly equivalent to:
project = __import__('project')
Effectively an import statement is simply an assignment to a variable. There may be some side effects as the module is loaded, but from inside your script all you see is a simple assignment to a name.
You can pull in all the names from a module using from project import *, but don't do that because it makes your code much more brittle and harder to maintain. Instead either just import the module or exactly the names you want.
So for your code something like:
import datetime
from project import Project
is the sort of thing you should be doing.

Categories