Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, March 7, 2017

Remote Debugging Python on Eclipse

Remote debugging is extremely useful, especially when you have limited access to the system used to run the application.

Let us call the system where the application is to be run as rsys and the one used to debug as dsys.

Setup dsys :
1. Install eclipse on dsys.

2. Make sure you have the pydev and debug perspectives enabled in eclipse.

3. Run the debug server on eclipse. The console should give you the port number that the debug server is listening to (default : 5678).

Setup rsys:
1. If you have root privileges on rsys install the python package 'pydevd'.
  pip install pydevd

2. If you do not have root privileges, use the target option in pip :
  pip  install  --target=d:\other\location  package_name

3. If you also do not have pip installed, download the package and install it to a custom directory. (refer https://docs.python.org/2/install/)
For 2. and 3., do not forget to add the custom directory to PYTHONPATH to import the package in your program.

Test the debugger:
1. Create a file hello_world.py and make sure it can be accessed both on rsys and dsys. (It should be the same file and not a copy of it)

2. Type the following into hello_world.py :

import pydevd
pydevd.settrace("10.X.Y.Z", port=5678)
# where 10.X.Y.Z is the IP address of dsys and 5678 is the port of the debug server running on dsys.

print "Hello World!"
# where your actual program goes.

3. In Eclipse (on dsys) create a new PyDev project including hello_world.py in its source. (Make sure the debugger is still running on port specified in the source file.)

4. Run hello_world.py on rsys. Hopefully your running application should be caught on the remote debug server running on Eclipse. (It may prompt you to choose the source file.)
Note: If break points do not seem to be working at this point, relevant paths need to be incorporated in the configuration for Eclipse and pydevd as below.

Configuration for project level debugging and working breakpoints:
(Refer to this pydevd documentation)

Important: Make sure you have the SAME source file structure mounted both on rsys and dsys. Unless there is a one-one correspondence between the files, adding breakpoints in eclipse editor on dsys will not be recognized by the program running on rsys and a warning will be displayed on rsys :
>pydev debugger: warning: trying to add breakpoint to file that does not exist:
/home/pi/python/f:/python projects/server/server.py (will have no effect)

1. Edit  the file pydevd_file_utils.py in the installed packages pydevd on rsys.
A portion of comments on that file explicitly instructs the corresponding project source paths on rsys and dsys be added to the file :
"E.g.:
        If the server (your python process) has the structure
            /user/projects/my_project/src/package/module1.py

        and the client has:
            c:\my_project\src\package\module1.py

        the PATHS_FROM_ECLIPSE_TO_PYTHON would have to be:
            PATHS_FROM_ECLIPSE_TO_PYTHON = [(r'c:\my_project\src', r'/user/projects/my_project/src')]"

4. In Eclipse, go to Window > Preferences > PyDev > Debug > Source Locator and add a similar entry as the one to pydevd_file_utils.py with the Path to translate being the path on rsys and Translated path the one on dsys.

Saturday, April 16, 2016

OpenCV Error on Anaconda


Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config

Solution:
conda remove opencv
conda update conda
conda install --channel menpo opencv

This installs the opencv from public channel menpo which works fine.

Tuesday, September 15, 2015

Pickled Object Problem

When unpickling an object of some class, if you encounter the error,
AttributeError: 'module' object has no attribute 'classname'
read this.

Wednesday, November 5, 2014

How to interrupt Ipython Kernel in Spyder IDE (Anaconda)


In Spyder IDE, the usual interrupt 'Ctrl-C' does not work if you are running your script on an IPython Console. This is because multiple of these consoles are linked to a single kernel. In fact, this message is displayed on the kernel itself :

NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.

To exit, you will have to explicitly quit this process, by either sending
"quit" from a client, or using Ctrl-\ in UNIX-like environments.

Read more about this, here.


A quick work-around this is to tweak some settings in Spyder:

Go to Run -> Configure. Select Execute in a new dedicated Python console. Check Interact with the Python console after execution.

Now when you run a script, the Interrupt button on the right top of the console should stop the script.

Thursday, October 9, 2014

Useful IPython Notebook Shortcuts

Command Mode Shortcuts

enter: edit mode
shift+enter: run cell
ctrl+enter: run cell, select below
alt+enter: run cell, insert below
y: to code
m: to markdown
t: to raw
1: to heading 1
2: to heading 2
3: to heading 3
4: to heading 4
5: to heading 5
6: to heading 6
up: select previous cell
down: select next cell
k: select previous cell
j: select next cell
ctrl+k: move cell up

Edit Mode Shortcuts
esc: command mode
ctrl+m: command mode
shift+enter: run cell
ctrl+enter: run cell, select below
alt+enter: run cell, insert below
alt+-: split cell
meta+s: save notebook
ctrl+s: save notebook


ctrl+j: move cell down
a: insert cell above
b: insert cell below
x: cut cell
c: copy cell
v: paste cell below
z: undo last delete
d: delete cell (press twice)
shift+m: merge cell below
s: save notebook
meta+s: save notebook
ctrl+s: save notebook
l: toggle line numbers
o: toggle output
shift+o: toggle output
h: keyboard shortcuts
i: interrupt kernel
.: restart kerne