【发布时间】:2020-09-18 21:28:33
【问题描述】:
django 和 Cython 的新手。我正在 django 中创建一个应用程序,需要从 cythonized 模块导入 views.py 中的函数。以下是我的应用程序中的views.py。
from django.shortcuts import render
import sys
import numpy as np
import random
import math
from cython_node_val import node_val
def home(request):
return render(request,'Home.html',{"name":"user"})
def shortest_path1(request):
K=int(request.POST['number of layers'])
if ((K%2!=0) or (K < 0)):
return render(request,"shortest_path1.html",{'shortest_path1':"K must be an even integer"})
else:
......
Node_val=node_val(Hash,C,K) #node_val is from cython_node_val which is a .pyx file, Hash C and K
are defined in body after else statement.
sPath=np.zeros((K,3))
sPath[K-1,:]=Node_val[n-1,:]
for m in range(K-2,-1,-1):
sPath[m,:]=Node_val[int(sPath[m+1,1])]
return render(request,"shortest_path1.html",{'shortest_path1':sPath[:,3]})'''
我的项目目录如下:
我的应用程序目录如下所示
cython_node_val.pyx 在导入普通 .py 文件时工作正常,但在我的应用程序中执行相同的内部 views.py 时,它会引发以下错误
File "C:\Users\amit\projects\application_shortest_path\shortest_path\DS2P\urls.py", line 9, in <module>
from . import views
File "C:\Users\amit\projects\application_shortest_path\shortest_path\DS2P\views.py", line 6, in <module>
from cython_node_val import node_val
ModuleNotFoundError: No module named 'cython_node_val'
我相信如果views.py 是一个python 文件并且我们可以进行操作,它应该拉取cython_node_val 和相关的函数。我哪里错了?
感谢您的宝贵时间。
【问题讨论】:
-
您显示的“view.py”的第 6 行是 not
import cimport。这表明您正在运行的代码与问题中的代码不同。我认为您只是对cimport感到困惑-它是一种用于在编译时导入 Cython 模块的 Cython 机制。因此,在 Cython 中,您可以使用cimport module。除非你有一个名为cimport的模块,否则你所做的一切都是毫无意义的 -
感谢@DavidW。我编辑了错误消息。我错误地输入了错误信息。没有关于 cimport 的内容。我正在尝试导入未被识别为不是 .py 文件的 cython_node_val.pyx。所以我的问题是如何使用 cython_node_val.pyx 中的函数。谢谢。