Debugging a Python Extension in Eclipse - python

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).

Related

Clion (using CMake) gets confused when setting python interpreter

I'm using CLion (2020.2.5) with a mixed project: both C++ and python.
When I load the project from scratch, CLion picks up the CMake files and everything works fine on the C++ side. I get code insights, I can run unit tests etc.
Then I open a python file in my project. CLion suggests I set the python interpreter for the project which I do. Everything on the python side starts working (code insight, unit tests).
But when I do that the C++ side stops working, and I get that warning on every cpp/h file I open:
This file does not belong to any project target, code insight might not work properly
So far the only work around I found is to toggle the python interpreter (to No Interpreter) when I want to do C++.

Can the Python interpreter and Python app source code be embedded into a compiled program?

I want to put the Python interpreter and all the source files of a fairly large Python application with multiple .py files into an executable Linux file.
Thus when running the application, the Python interpreter will run the Python source code that is embedded into the executable.
Using Python 3.
Is such a thing possible?
Edit: in addition to the selected answer another option is to use Cython.
Sounds like you're looking for a packaging module, like py2exe or cx_Freeze (I prefer the latter). They bundle the interpreter and your files together so that a machine without an installation of Python can run your program.

How to keep CMake from destroying PyDev configuration in Eclipse project

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.

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.

How to deploy Python to Windows users?

I'm soon to launch a beta app and this have the option to create custom integration scripts on Python.
The app will target Mac OS X and Windows, and my problem is with Windows where Python normally is not present.
My actual aproach is silently run the Python 2.6 install. However I face the problem that is not activated by default and the path is not set when use the command line options. And I fear that if Python is installed before and I upgrade to a new version this could break something else...
So, I wonder how this can be done cleanly. Is it OK if I copy the whole Python 2.6 directory, and put it in a sub-directory of my app and install everything there? Or with virtualenv is posible run diferents versions of Python (if Python is already installed in the machine?).
I also play before embedding Python with a DLL, and found it easy but I lost the ability to debug, so I switch to command-line plug-ins.
I execute the plug-ins from command line and read the STDOUT and STDERR output. The app is made with Delphi/Lazarus. I install others modules like JSON and RPC clients, Win32com, ORM, etc. I create the installer with bitrock.
UPDATE: The end-users are small business owners, and the Python scripts are made by developers. I want to avoid any additional step in the deployment, so I want a fully integrated setup.
Copy a Portable Python folder out of your installer, into the same folder as your Delphi/Lazarus app. Set all paths appropriately for that.
You might try using py2exe. It creates a .exe file with Python already included!
Integrate the python interpreter into your Delphi app with P4D. These components actually work, and in both directions too (Delphi classes exposed to Python as binary extensions, and Python interpreter inside Delphi). I also saw a patch for Lazarus compatibility on the Google Code "issues" page, but it seems there might be some unresolved issues there.
I think there's no problem combining .EXE packaging with a tool like PyInstaller or py2exe and Python-written plugins. The created .EXE can easily detect where it's installed and the code inside can then simply import files from some pre-determined plugin directory. Don't forget that once you package a Python script into an executable, it also packages the Python interpreter inside, so there you have it - a full Python environment customized with your own code.

Categories

Resources