How to keep CMake from destroying PyDev configuration in Eclipse project - python

I'm developing a project using CMake that involves a large number of Python scripts. As part of my workflow, I'm generating Eclipse projects from the CMake files, and then using PyDev within Eclipse to edit and test the scripts.
One problem that I have, however, is that each time modify CMakeLists.txt, it regenerates the Eclipse project which overwrites any PyDev configuration (PYTHONPATH, Python Nature, Project References, etc) that I have set.
What I'd like to do is either of the following:
1) Somehow add the PyDev configuration to my CMakeLists.txt so that it is included in the generated project.
2) Have some command-line tool that runs after CMake that modifies the project to include the PyDev stuff.

I've been looking into the same problem for a while now, and I've finally found the solution. It appears that in CMake 3.0+, they've introduced a global property called ECLIPSE_EXTRA_NATURES, that can be used for this purpose. Just include the following line in your CMakeLists.txt file:
set_property(GLOBAL APPEND
PROPERTY ECLIPSE_EXTRA_NATURES "org.python.pydev.pythonNature")
And run cmake (as usual):
cmake <Folder_Containing_CMakeLists.txt> -G"Eclipse CDT4 - Unix Makefiles"
You'll now see that the generated .project file contains the needed <nature> element.

Related

VSCode Python package inspection failure

This's probably quite off-topic but maybe someone else share my experience... when I started coding KiCAD plugins and learning Python I decided to use VSCode (with Microsoft Python extension) because I already use it for a number of other things and works great.
I have two main issues mostly bound to Python extension itself other than VSCode:
VSCode try to use python 3.7 binary (I installed it on my
machine and I need it) instead kicad/python 2 (it's installed on a different location);
even if I setup VSCode to look for additional packages with a specific configuration file (settings.json):
"python.autoComplete.extraPaths": ["C:/Program Files/KiCad/lib/python2.7/site-packages"]
VSCode refuse to investigate pcbnew.py (located in that folder) and thus every Intellisense like function (inspection etc) doesn't work.
How can I fix that? ... I'm more interested in fixing the second issue other than the first.
To correctly enable per project configuration of VSCode / Python open folder and create a workspace configuration for the project itself (i.e. create a .vscode folder inside project folder and store a settings.json file inside it with you custom configuration). In my specific case I needed to specifiy a custom location for an older Python binary trough python.pythonPath directive.

In what order are PyDev project references, sourrce folders, and external libraries in the PYTHONPATH

Using Eclipse with the PyDev plugin, if you choose myProject>Properties>PyDev-PYTHONPATH, you then see two tabs: Source Folders and External Libraries. You can also choose myProject>Properties>Project References and see a widget with a checkable list of other parallel subprojects in your Eclipse/Pydev IDE workspace. I understand that the values in these widgets configure the PYTHONPATH when you run your project.
But the documentation does not seem to say the ordering of the values you specify. For example, are Project References values always after Source Folders and before External Libraries, in the generated PYTHONPATH? (That is the ordering I wish, so that I can Python install one of my subprojects, and my main project will find the installed version if I have turned off Project References, but my main project will find the same project from my workspace if I turn on a Project Reference to it, while I am changing and debugging the subproject.)
Similarly (recursively) are the External Libraries of a Referenced Project inserted in the PYTHONPATH AFTER the Source Folder of a Referenced Project?
It seems like my PYTHONPATH has site-packages external library directory BEFORE the source folder of my subproject, so Python never finds the development version of my subproject, only the subproject version as installed in site-packages.
I have tried several times to 'Force restore internal info' and to restart Eclipse. I suppose I could have made a mistake somewhere outside of Eclipse.
The PYTHONPATH in PyDev is computed in the following order:
Source folders of the project have highest priority (since this is the code you're expecting to be editing).
External source folders of the project being used for the launch.
Computed PYTHONPATH of the dependent projects (again, first source then external).
PYTHONPATH of the related interpreter selected.
Note that the final sys.path is actually computed by Python itself (so, it may be a bit different depending on your Python version -- i.e.: it could add things from the current working dir, current module or eggs even if you remove it from what's configured in PyDev -- although for PyDev, modules not added won't be available for code completion and would be present as errors when searched for as they won't be indexed), PyDev only changes the PYTHONPATH environment variable to match the order presented above.
If you somehow have a different outcome, please report this as a bug... (you can see what will be actually used before running in the launch run configuration > interpreter tab > see resulting command-line).

PyDev Debugging

My interpreter is the python 2.6 interpreter that comes with cygwin.
I have a pydev project that contains a linked folder.
In the folder, due to outside constraints, I have two sub-folders, one with a bunch of scripts I develop, and one which serves as the run directory for all my run configurations.
When I run the project (not debug) everything works fine.
However, when I choose to debug the project, my custom modules aren't found, and the import fails.
I tried adding my scripts folder to PYTHONPATH, but that didn't help, and when I printed sys.path It contained weird stuff, all of the form:
<path to run directory>/<path to entry in PYTHONPATH>
I can't make heads or tails out of it.
I'll appreciate any help.
Unfortunately, that's a known issue. PyDev does not support Cygwin for debugging. The primary reason is that the paths that Python reports don't match the paths you configure in windows, so, a translation step would be needed for each path (even some other areas such as code-completion or code-analysis could have problems).
You can probably do this translation yourself at eclipse/plugins/org.python.pydev_x.x.x/pysrc/pydevd_file_utils.py (instructions on the file), but note that this may be more work than it's worth (the recommended approach is using a native Python install on Windows instead of through cygwin) -- or if you need an interpreter on Linux, develop on Linux :)
Thanks, you made my day, its actually very easy,
you have to change the PATHS_FROM_ECLIPSE_TO_PYTHON variable inside pydevd_file_utils.py as,
PATHS_FROM_ECLIPSE_TO_PYTHON = [
(r'C:\Users\usernam\workspace-cpp\python-proj\src',
r'/cygdrive/c/Users/usernam/workspace-cpp/python-proj/src')
]
But this needs to be changed for all your own python source files you desire to debug
I am happy to announce a new release of ePyUnit, which solves the load-path issue of 'pydevd.py'. This works from within Eclipse/PyDev as well as from an arbitrary process started from the command line.
ePyUnit now includes the automation of remote debugging with PyDev and Eclipse by 'pydevd.py'. This works seamlessly for the 'subprocess' call as well as independently started command line processes.
See:
https://pypi.python.org/pypi/epyunit
https://pythonhosted.org/epyunit/
For basics of remote debugging:
http://www.pydev.org/manual_adv_remote_debugger.html
Also enhanced unittest integration into PyUnit.
Comments and fixes are welcome.
Have fun...

Debugging a Python Extension in Eclipse

I have a Python extension that I have successfully built and used on Windows, OSX, and linux. I now need to be able to debug this Python extension. I am averse to the use of gdb at the command line, so I would really like to get this to work in Eclipse.
To begin with, I did my best to follow the instructions in http://www.heikkitoivonen.net/blog/2008/07/21/debugging-python-extension-written-in-c-with-eclipse/, and I added the folder for the Python source as well as the folder for the python extension code as source folders to the empty project. Since it is relatively easy to get debug builds of everything required on linux, I started out with the debug development in Ubuntu.
Once I got a debug build of python (lets call it python_d), I ran the setup.py for my extension with
python_d setup.py build
which should also yield a debug build of the extension module. I have verified that symbols are being exported by opening the extension as an application in Eclipse and I can see the source code linked with the Python shared object.
Now if I create another project in Eclipse with the folder of my code, and add a breakpoint in the source that is used to create the extension, it doesn't stop at the breakpoint. It is entirely possible that I am missing something rather critical here, but for the life of me I can't get it to work. The crux of the problem is:
How can you get Eclipse to stop at a breakpoint in a Python extension module?
Which compiler are you using, MSVC or GCC? For MSVC, you can start python first, and attach the python_d.exe (windbg or visual studio), then you can load your module, setup the breakpoint, you can verify whether your module's symbol got loaded in the debug->module windows (MSVC).

How to debug SCons scripts using eclipse and pydev?

I'm a newbie to SCons and also using pydev. Can someone help me with instructions on how to debug scons scripts using Eclipse and pydev? Is it even possible considering the fact that SCons is a seperate app and not an extension to python?
You are right. Since the SCons is python based, the SCons scripts are debuggable via EClipse PyDev. For this, you need to do the following in the debug configuration...
1. Under the main tab, set the main module to the SCons file which will be available under the python/scripts directory if you have installed SCons. If you have not run the install of SCons you can point to this file under the SCons directory.
2. Under the arguments tab, set the working directory to the root of your project.
Now set the breakpoint either on SConstruct or SConcript and run in debug mode. That's all!!
With this approach you can not only debug your product code but also the build scripts that builds your product :-) Happy Debugging!!!!
I'm not an Eclipse expert, but since you didn't get any other answer...
If you make the SCons source a part of the Eclipse project, and run the whole command from within Eclipse it should work like any Eclipse debugging. SCons is written in Python, there is no reason it shouldn't be debuggable in Eclipse just like anything else.
if you are using SCons for C/C++ development and Eclipse CDT check out http://sconsolidator.com (within the next weeks), we will release our SCons Eclipse plug-in for free public use shortly. It also contains an "interactive mode" that allows SCons builds to run more quickly (no startup time) and also to debug SCons in a console. However, the tip with using PyDev still applies (can be installed alongside with CDT in the same Eclipse instance.
On MAC to debug scons through pydev follow Lennart's answer but with one simply addition.
Using Finder (or terminal) browse to where scons is installed. You can find this with the "which" command.
e.g. which scons
-> /usr/local/bin/scons
Make a copy of the scons file and call it scons.py.
Now when you create the Debug Configuration in Eclipse use scons.py as the "Main Module".
PS: To add a scons project to Eclipse I found it easier to use a "Linked Folder" pointing at /usr/local/bin/. i.e. Because I was getting a read-only error when trying to add the directory itself.
I've since gain more experience with SCons / Python and I'd recommend using python's pdb module. To use it simply add the following code to your SCons/Python files.
import pdb; pdb.set_trace()
When the file is run from the command line a breakpoint will be hit at this line. I also moved away from Eclipse. A lightweight editor will be just as good for Python development. I use Sublime.
As an addendum: on Windows, I had to copy the scons-installed files to reside under C:\Python27\Lib\site-packages\scons in order for this to work. Adding the original installed location, qualified with the version number, to the PYTHONPATH, did not work.

Categories

Resources