【问题标题】:In PyCharm, how to break at a line in an imported Cython extension code在 PyCharm 中,如何在导入的 Cython 扩展代码中换行
【发布时间】:2019-01-06 13:44:01
【问题描述】:

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

from scipy.interpolate.interpnd import LinearNDInterpolator

我想运行一个调用函数LinearNDInterpolator 的Python 脚本并在line 304 of function LinearNDInterpolator 处设置一个断点。我该怎么做?

我正在使用 PyCharm。我无法“进入”LinearNDInterpolator 的代码。以下是我正在运行的示例脚本。

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

【问题讨论】:

  • 设置breakpoint() 有效吗?但它仅适用于 Python 3.7+。
  • @GeeTransit:我把breakpoint()放在哪里?
  • 你能访问LinearNDInterpolator的代码吗?您可以复制它并将其作为 python 文件导入,尽管这可能会破坏包。也许this 会有所帮助。
  • @GeeTransit:我尝试使用文件名“interpnd.pyx”将 Cython 文件复制到 PyCharm 中的本地目录中,并使用命令 from interpnd import LinearNDInterpolator 导入函数。我遇到了错误“ImportError:没有名为 interpnd 的模块”。 PyCharm 是否允许将扩展名为 .pyx 的 Cython 文件添加到本地目录?
  • 查看 Cython 文档的 debugging section

标签: python pycharm cython


【解决方案1】:

编辑 - 示例代码适用于我减去错误的注释行

所以我自己测试了这个。

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

在PowerShell中

python -m site

对我来说,这个文件夹是 C:\Program Files (x86)\Python37-32\Lib\site-packages\scipy

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

__all__ = ['test'] <- breakpoint there

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

【讨论】:

  • 你的__init__.py 是 scipy 的一部分吗?无论如何,您能否尝试我的导入脚本并在我在问题中指定的行设置断点?
  • 你使用 LinearNDInterpolator 的第一行代码是什么?
  • 我添加了一个调用 Cython 函数的示例 Python 脚本。看看你能不能运行它。
  • 说“NameError: name 'xi' is not defined”
  • 还没有。我试图让 PyC​​harm 识别 .pyx Cython 文件。显然,只有付费的专业版可以。
猜你喜欢
  • 1970-01-01
  • 2016-07-22
  • 2018-04-25
  • 2020-10-12
  • 2020-07-14
  • 1970-01-01
  • 2016-11-29
  • 2022-07-14
  • 1970-01-01
相关资源
最近更新 更多