django - Django + Cython 在 django 应用程序视图中导入 cython 模块
问题描述
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 和相关函数。我哪里错了?
谢谢你的时间。
解决方案
使用 os.getcwd() 从脚本views.py 调试您正在运行的位置:
import os
print(os.getcwd())
ModuleNotFoundError: No module named 'cimport'
然后根据需要调整路径
当您尝试引用不在 python 运行路径脚本中的内容 + 您在脚本中提供的路径时,通常是一个错误。
推荐阅读
- javascript - 在 Java Script 中使用 for 循环更新 JSON 对象值
- c - 如何在c中取消整个字符串?
- python - 无法使用 python 套接字实例化正确引发异常
- c++ - 解决 C++ 中 3 个类的循环包含问题
- sass - 在嵌套的每个循环中传递地图名称作为参数
- excel - 如何在 Excel 用户窗体中删除标题栏并调整标题栏剩余部分的大小?
- typescript - “B”类型中缺少属性“A”,但在“C”类型中是必需的
- jquery - 第二次 onclick 运行 jQuery 文档就绪功能?
- python - python 2.7和3.6的pybind11
- python - 如何检查 BeautifulSoup 标签是否是某个标签?