1

a newbie to django and Cython. I am creating an app in django and need to import function in views.py from cythonized module. following is views.py inside my app.

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]})'''

the directory of my project is like following: project directory Django

my app directory looks like this app directory with built cython code and supporting files highlighted

cython_node_val.pyx works fine when importing into a normal .py file, but when doing the same inside views.py in my app it throws me following error

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'

I believe if views.py is a python file and we can do operations, it should pull cython_node_val and associated functions. Where am i wrong?

Thanks for your time.

Amit Jha
  • 11
  • 4
  • line 6 of the "view.py" you show is __not__ `import cimport`. That suggests the code you're running is not the same as the code in the question. I think you've just got confused about `cimport` though - it is a Cython mechanism for importing Cython modules at compile-time. Thus in Cython you would do `cimport module`. Unless you have a module called `cimport` then what you have done is meaningless – DavidW Sep 19 '20 at 07:50
  • thanks @DavidW. i edited the error message. i wrongly put the error message. there is nothing about cimport. I am trying to import cython_node_val.pyx which is not recognized as its not a .py file. so my question is how to make use of functions in cython_node_val.pyx. thanks. – Amit Jha Sep 20 '20 at 18:46

1 Answers1

0

Use os.getcwd() to debug where you are running from your script views.py:

import os
print(os.getcwd()) 

ModuleNotFoundError: No module named 'cimport'

Then adjust the path to your needs

Is usually an error when you are trying to reference something that isn't in the python running path script + the path that you are giving inside your script.

Cslayer20
  • 67
  • 1
  • 9
  • thanks for the prompt response. actually the error msg i posted was wrong. the problem is with importing cython_node_val.pyx. So my question is why is it not working with views.py when it is normally working with other .py files. Is there something i have to do with model.py in django project directory or what changes i have to make so that views.py could identify cython_node_val. – Amit Jha Sep 20 '20 at 18:51
  • i have edited the post with correct error message. thanks. – Amit Jha Sep 20 '20 at 18:52
  • I found a solution which is to put the cython_node_val module put at project directory level and not at app directory level. so i had to move the file one level up. thanks. – Amit Jha Sep 23 '20 at 23:40