在 PyCharm 中,如何在导入的 Cython 扩展代码中换行 [英] In PyCharm, how to break at a line in an imported Cython extension code

查看:26
本文介绍了在 PyCharm 中,如何在导入的 Cython 扩展代码中换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查以下 Python 代码中调用的函数 LinearNDInterpolator

从 scipy.interpolate.interpnd 导入 LinearNDInterpolator

我想运行一个 Python 脚本调用函数 LinearNDInterpolator 并设置一个断点,例如

所以我自己测试了这个.

找到 site-packages 文件夹以找到 scipy 源文件

在PowerShell中

python -m 站点

对我来说,这个文件夹是C:Program Files (x86)Python37-32Libsite-packagesscipy

在pycharm中打开init.py文件,在第一行代码设置断点

__all__ = ['test'] <- 断点

运行 > 调试 > 进入我的代码

I am trying to examine the function LinearNDInterpolator invoked in the following Python code

from scipy.interpolate.interpnd import LinearNDInterpolator

I would like to run a Python script invoking function LinearNDInterpolator and set a breakpoint at, say, line 304 of function LinearNDInterpolator. How can I do this?

I am using PyCharm. I am not able to "step into" the code of LinearNDInterpolator. The following is an example script I am running.

import numpy as np
from scipy.interpolate.interpnd import LinearNDInterpolator
import matplotlib.pyplot as plt

x = np.linspace(-1,1,100)
y =  np.linspace(-1,1,100)
X, Y = np.meshgrid(x,y)

def f(x, y):
    s = np.hypot(x, y)
    phi = np.arctan2(y, x)
    tau = s + s*(1-s)/5 * np.sin(6*phi) 
    return 5*(1-tau) + tau

T = f(X, Y)
# Choose npts random point from the discrete domain of our model function
npts = 400
px, py = np.random.choice(x, npts), np.random.choice(y, npts)

fig, ax = plt.subplots(nrows=2, ncols=2)
# Plot the model function and the randomly selected sample points
ax[0,0].contourf(X, Y, T)
ax[0,0].scatter(px, py, c='k', alpha=0.2, marker='.')
ax[0,0].set_title('Sample points on f(X,Y)')

# Interpolate using three different methods and plot
i = 0
method = 'linear'
#for i, method in enumerate(('nearest', 'linear', 'cubic')):
ip = LinearNDInterpolator((px, py), f(px,py))
Ti = ip((X, Y))
r, c = (i+1) // 2, (i+1) % 2
ax[r,c].contourf(X, Y, Ti)
ax[r,c].set_title('method = {}'.format(method))

plt.show()

解决方案

EDIT - Sample code works for me minus commented out line that was in error

So I tested this myself.

Find the site-packages folder to find the scipy source files

In powershell

python -m site

For me this folder was C:Program Files (x86)Python37-32Libsite-packagesscipy

open the init.py file in pycharm and set your break point on the first line of code

__all__ = ['test'] <- breakpoint there

Run > Debug > Step Into My Code

这篇关于在 PyCharm 中,如何在导入的 Cython 扩展代码中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆