This question has been flagged
16 Replies
68467 Views

I want to debug my python code in open erp i had given print statement but its not showing in log I check in tail -f /var/log/openerp/openerp-server.log any one please help?

Avatar
Discard
Best Answer

Hi,

I use this for debugging :

import logging
_logger = logging.getLogger(__name__)

class my_class(osv.osv):

    # in the code when needed :
    _logger.error("my variable : %r", my_var)

    # Or
    msg = "This is my error message : " % my_var
    _logger.error(msg)

and in the log file , I'll have a line containing : my variable: + value of my_var

or : This is my error message + value of my_var

Of course, debug must be enabled in openerp-server.conf

Best regards

Avatar
Discard
Author

i enabled debug mode by setting debug_mode = true in /etc/openerp/openerp-server.conf and checking log tail -f /var/log/openerp/openerp-server.log but still its not showing print

Author
import logging
from openerp.osv import fields, osv
import time
import pdb

_logger = logging.getLogger(__name__)
class calldata_print_report_wiz(osv.osv_memory):
    _name = "calldata.print.report.wiz"

    def _sel_func(self, cr, uid, context=None):

     print "hello"*100
     _logger.debug('helooooooooooooooooooooo')
     obj = self.pool.get('calldata1')
     ids = obj.search(cr, uid, [])
     res = obj.read(cr, uid, ids, ['Caller'], context)
     res = [r['Caller'] for r in res]
     return res
Best Answer

Follow the given steps to debug your python code in eclipse:

1) Go to '/openerp/server/openerp-server' and select openerp-server file, then right click on that file and select Debug As -> Python Run which will start the server.

2) After creating new debug configuration Go to 'Run -> Debug Configuration' and follow the given steps: 

    2.1: In "Arguments" tab under "Program Arguments", click on button "Variables" and new window will appear. 

    2.2: Then create new "Variable" by clicking on "Edit Variables" button and new window will appear.

    2.3: Press on "New" button and give your addons path as value. Ex: --addons ../addons,../your_module_path

     2.4: Press Ok in all the opened windows and then "Apply".

3) Now into "PyDev Package Explorer" view go to 6.1/server and right click on "openerp-server" file, Select 'Debug As --> Python Run'.

4) Now in "Console" you can see your server has been started.

5) Now open your .py file which you want to debug and set a breakpoint.

6) Now start your module's form from 'gtk' or 'web-client' and execution will stop when execution will reach to breakpoint.

7) Now debug your code by pressing "F5, F6, F7" and you can see value of your variables.

Thanks & regards,

Serpent Consulting Services Pvt. Ltd.

Avatar
Discard
Best Answer

A non IDE-specific way to debug Python code is to import the pdb module (Python Debugger) and call pdb.set_trace() in your code where you want the debugger to open, it basically sets a breakpoint. Once in the debugger you can do a lot of things, like printing variables values, moving in the call stack, etc. see the pdb module documentation.
 

Avatar
Discard

Thanks I like it!

Best Answer

Simple howto debugging openerp7 with intellij idea on windows.

Prerequests:

  • installed IntelliJ Idea 12 (www.jetbrains.com/idea/download/)
  • installed Openerp7 openerp-allinone-setup-7.0-latest.exe (www.openerp.com/start?download)

Step by step:

  • install intellij python plugin file->settings->plugins->install jetbrains plugins->python->install
  • download and install python 2.6 (i prefere portable version) portablepython.com/wiki/PortablePython1.1Py2.6.1/
  • copy listed files from portable python (F:\PortableApps\PortablePython_1.1_py2.6.1\App) to openerp (F:\OpenERP 7.0-20130731-231106\Server\server): python.exe Lib\site.py Lib\Queue.py Lib\trace.py
  • create new python project (openerp7debug) and configure python sdk file->new project->python module->openerp7debug->next->Python Interpreter->Configure-> + ->Python SDK->local->F:\OpenERP 7.0-20130731-231106\Server\server\python.exe->next->finish
  • right click openerp7debug->new->python file->openerp-server.py->add content

    import openerp if __name__ == "__main__": openerp.cli.main()

  • create debug configuration Edit configuration...-> + -> Python->Name: Debug openerp7->Script: F:\Users\orlov\YandexDisk\Work\openerp7debug\openerp-server.py-> Script parameters: "--config=F:\OpenERP 7.0-20130731-231106\Server\server\openerp-server.conf"-> Working directory: F:\OpenERP 7.0-20130731-231106\Server\server->OK

  • add breakpoint (on line openerp.cli.main() for example) and click debug, test breakpoints, inspect vars, test UI from browser
  • Happy debugging!
Avatar
Discard
Best Answer

You can try with ipdb, it's a python debugger available from pip. 

​pip install ipdb

To use it, just import it somewhere on your code, set the trace and use your module (you should be running odoo NOT as a service)

# python code ...
import ipdb; ipdb.set_trace()
# python code ...

Start your server

<openerp_path>$ ./openerp-server

When the line with ipdb is executed, you will notice your process stops, and your terminal is ready to debug.
Some keywords:

  • c (continue): will quit from debugging mode and continue with the execution
  • n (next): will do a step forward on your debugged code

Hope it helps you!

Regards

 

Avatar
Discard

I got this error when i added in called function in odoo

also i have install ipdb

Traceback (most recent call last):

File "/var/lib/odoo10/odoo/addons/base/ir/ir_cron.py", line 119, in _callback

getattr(model, method_name)(*args)

File "/var/lib/odoo/addons/10.0/allip_provu_phone/models/mac_address.py", line 116, in syncMacAddressProvuOrder

updateResponse = baseClass().updateInventoryForProvuPhoneOrder(product_itemlist, item_mac)

File "/var/lib/odoo/addons/10.0/allip_provu_phone/base/base_class.py", line 180, in updateInventoryForProvuPhoneOrder

import ipdb; ipdb.set_trace()

File "/usr/lib/python2.7/site-packages/ipdb/__main__.py", line 93, in set_trace

p = _init_pdb(context).set_trace(frame)

File "/usr/lib/python2.7/site-packages/ipdb/__main__.py", line 76, in _init_pdb

p = Pdb(def_colors)

File "/usr/lib/python2.7/site-packages/IPython/terminal/debugger.py", line 15, in __init__

self.pt_init()

File "/usr/lib/python2.7/site-packages/IPython/terminal/debugger.py", line 38, in pt_init

self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop)

AttributeError: 'TerminalInteractiveShell' object has no attribute '_eventloop'

Best Answer

There is several ways to debug and I see you've got some good tips on the topic, but I'll narrow down your question to get worked print statement in python code as it's what you've tried, maybe it's your preferred way...

Instead of redirecting logs to logfile (to the /var/log/openerp/openerp-server.log in your case) and then read them from there using "tail -f" you can simply run openerp server from command line and you'll get printed stuff (including simple "print" command in python code) directly in the terminal from where you'll run the server. If you've installed nightly version and/or you're running openerp server in daemon mode, then you've to shutdown your server instance first ( something like: "/etc/init.d/openerp stop" or whatever is correct for your case). Then start the server from the terminal (you've to figure out what's path to the openep startup script, the odoo.py for v8 or openerp.py/openerp-server.py for v7), using command line options, at least use "--config=/path/to/your/config/file" option for reuse your config file. lets say server path is /usr/bin/openerp-server and config file is: /etc/openerp/openerp-server.conf, then you'll run it as "/usr/bin/openerp-server --config=/etc/openerp/openerp-server.conf" ( here you can use other options as well, including  --logfile=/var/log/openerp/openerp-server.log if you like) from terminal. Then in your python code you can

print "whatever" 

and you'll get whatever printed to the terminal.

Avatar
Discard

Is this for versions 7 and 8 only? I'm using version 11; Had to use logging module, ```python print() ``` doesn't work.

Best Answer

Print statements should appear in log, if not- maybe your print statement is after some error and does not get executed?

For OpenERP debugging tool and its usage, look at this question and answer

Avatar
Discard
Author

I dont have any error before print.i enabled debug mode by setting debug_mode = true in /etc/openerp/openerp-server.conf and checking log tail -f /var/log/openerp/openerp-server.log but still its not showing print

Best Answer

I have solved remote debugging to my remote ubuntu server by doing the following:

On your remote server, run: pip install cython pydevd

IMPORTANT: Make sure that your computer is visible to server's IP by pinging your computer's IP

On your computer, configure a Python Remote Debug in run configurations.

Set Local Host Name: The IP of your computer. Yes, that computer with the pycharm installed.

Set the Port: 7999. Actually, any port will do as long as its above 1000.

Make sure to check the box, SINGLE INSTANCE ONLY

On File>Settings>Execution>Deployment, create an SFTP Server Connection.

Set the SFTP host to the server's IP. set the root path to the installation directory of your odoo server. In my case, its located at /opt/odoo

Set the username and password.

On the mappings tab,

Set the Local Path to your project's directory root

Set the deployment path to "\" or backslash.

Set the web path to "/" or forwardslash.

Test the SFTP connection.

On the server, open the file /opt/odoo/openerp-server (It's a python file without the extension)

Add these statements after the "import openerp"

import pydevd

pydevd.settrace('YOUR COMPUTER IP', port=7999, stdoutToServer=True,stderrToServer=True)

TODO EVERYTIME YOU MODIFY YOUR CODE AND DEBUG THATNow to start the debug, add a breakpoint in your code.

Run the remote debug to start listening from the server.

Restart the openerp instance you got running in your server. In my case, I force restart the init.d/odoo-server file:

/etc/init.d/odoo-server restart

If successful, pycharm will light up in the menu bar and ask you to set the path mappings. Just click the autodetect and press Save forever.

Notice that if you visit the odoo server website, it just won't load. Actually, pycharm sets a "virtual" breakpoint after the settrace command you have provided so you have to press the play button thing at the debug(or F8)

Viola! Now it works and any breakpoint you set will stop odoo from executing and let you analyze everything.

Test it by adding a breakpoint in your code and try to run that code. Try the @api.onchange ones since they are more predictable to run.

Avatar
Discard
Best Answer

As mentioned above, eclipse will work. You can debug sql query also.

There you can see your cron running

SELECT * FROM ir_cron
                              WHERE numbercall != 0
                                  AND active AND nextcall <= (now() at time zone 'UTC')
                              ORDER BY priority None
SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s ('base', '7.0.1.3')
SELECT * FROM ir_cron
                              WHERE numbercall != 0
                                  AND active AND nextcall <= (now() at time zone 'UTC')
                              ORDER BY priority None
SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s ('base', '7.0.1.3')
SELECT * FROM ir_cron
                              WHERE numbercall != 0
                                  AND active AND nextcall <= (now() at time zone 'UTC')
                              ORDER BY priority None
SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s ('base', '7.0.1.3')
SELECT * FROM ir_cron
                              WHERE numbercall != 0
                                  AND active AND nextcall <= (now() at time zone 'UTC')
                              ORDER BY priority None
SELECT 1 FROM ir_module_module WHERE name=%s AND latest_version=%s ('base', '7.0.1.3')
SELECT * FROM ir_cron
                              WHERE numbercall != 0
                                  AND active AND nextcall <= (now() at time zone 'UTC')
                              ORDER BY priority None

Avatar
Discard
Best Answer

doesnt seem to be a Lib/Queue.py file in python 2.7 ??

Avatar
Discard

oh, actually i found it so it is there!! my mistake

Best Answer

Hello Anuradha,

I personally prefer to pass the command line argument "--log-level=debug", to the "openerp-server" script, in order to set the log level, instead of using the configuration file.

Several useful options can be passed as command line arguments as well, run "./openerp-server  --help", from the appropriate directory, in order to get the complete list of options and values it accepts if applicable, e.g.: available log levels.

The Python interpreter won't reload your code changes automatically, you will require to restart OpenERP after adding additional logging statements into your code.

I hope this helps.

Avatar
Discard