Matlab script not found from django views.py

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have a user-made Matlab script in the same directory as my views.py page, but when it is called I receive the error: Undefined function 'getRecs' for input arguments of type 'double'. Basically the script wasn't found.

When I test the script with a test.py script in the same directory everything works fine, but when called through the views.py via my browser and the development server the error appears.

tools.py:

def get_recs(passed_list):    eng = matlab.engine.start_matlab()    recs = eng.getRecs(passed_list)    return recs

test.py: (works fine and is in same directory as views.py and getRecs.m) (Ran using Pydev's run as: Python run)

from tools import *  test_ratings = [5, 0, 3, 2, 1, 5] conv_rates = matlab.double(initializer=test_ratings) new_recs = get_recs(conv_rates) print_list(new_recs)

views.py: (throws error)

from tool import * test_ratings = [5, 0, 3, 2, 1, 5] def new_user(request):     conv_rates = matlab.double(initializer=test_ratings)     new_recs = get_recs(conv_rates)     return render_to_response('new_user.html', { 'ratings_list' :new_recs}, context_instance=RequestContext(request)) 

I'm using Python 2.7, Django 1.9, and Matlab R2015b

回答1:

For anyone having this problem in the future: like Selcuk mentioned the development server defines its own directory so you need to add the path to the scripts you want to use to the engine. I did this by addding

eng.addpath(r'C:\path\to\my\scripts\')

after initializing and starting the engine.



回答2:

Django development server (or WSGI server) defines a different default directory than the one your user script resides. Try adding the location of your user script to the search path using addpath option:

eng = matlab.engine.start_matlab("-addpath /var/django/myproject/myapp/")


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!