烧瓶程序给出模块未找到

时间:2018-02-07 01:29:57

标签: python nginx flask uwsgi

我正在建立一个带有烧瓶和uWSGI的python web应用程序,遵循这个可爱的指南https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-centos-7,它可以创造奇迹。我想说我已经在项目文件中安装了每个模块和依赖项。我现在尝试构建工作脚本,现在我的 init .py文件如下所示:

from flask import Flask
import pylab as pl
import numpy as np
import pandas as pd

from sklearn import svm
from sklearn import tree
import matplotlib.pyplot as plt
from sklearn import linear_model
from sklearn.pipeline import Pipeline
from sklearn.metrics import confusion_matrix
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import SGDClassifier
from mlxtend.plotting import plot_decision_regions
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer

app = Flask(__name__)

@app.route("/")


def hello():
    data = pd.read_csv('test1.csv', error_bad_lines=False, delimiter=',')
        numpy_array = data.as_matrix()
        #print numpy_array

        #text in column 1, classifier in column 2.
        X = numpy_array[:,0]
        Y = numpy_array[:,1]
        Y=Y.astype(np.str)

        #divide the test set and set the variable to their correct label/text
        X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.4, random_state=42)

        #MultinomialNB
        text_clf = Pipeline([('vect', CountVectorizer(stop_words='english')), ('tfidf', TfidfTransformer()),('clf', MultinomialNB()),])

        text_clf = text_clf.fit(X_train.astype('U'),Y_train.astype('U'))
        predicted = text_clf.predict(X_test)
        # print the actual accuracy
        print "MNB accuracy: ", np.mean(predicted == Y_test)

        #make the confusion matrix
        y_actu = pd.Series(Y_test, name='Actual')
        y_pred = pd.Series(predicted, name='Predicted')
        df_confusion = pd.crosstab(y_actu, y_pred)
        print df_confusion

        print"-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------$

       #SVM
    vect = CountVectorizer(min_df=0., max_df=1.0)
       X = vect.fit_transform(X_train.astype('U'))
       min_frequency = 22

       text_clf_svm = Pipeline([('vect', CountVectorizer(min_df=min_frequency, stop_words='english')), ('tfidf', TfidfTransformer()),('clf-svm', SGDClassifier(loss='hinge', penalty='l2', alpha=1e-03, n_iter=1000, random_state=21))])

       text_clf_svm = text_clf_svm.fit(X_train.astype('U'),Y_train.astype('U'))
       predicted_svm = text_clf_svm.predict(X_test)
       # print the actual accuracy
       print "svm accuracy: ", np.mean(predicted_svm == Y_test)

       #make the confusion matrix
       y_actu = pd.Series(Y_test, name='Actual')
       y_pred = pd.Series(predicted_svm, name='Predicted')
       df_confusion = pd.crosstab(y_actu, y_pred)

       print df_confusion


if __name__ == "__main__":
   app.run()

一切都很好,就我而言,确保安装所有依赖项和模块sin我运行代码的文件夹。但是当我运行它时,我得到以下错误

[root@python-political-bias-app fyp]# semodule -i mynginx.pp
[root@python-political-bias-app fyp]# env/bin/uwsgi --socket 127.0.0.1:8080 -w WSGI:app &
[1] 1710
[root@python-political-bias-app fyp]# *** Starting uWSGI 2.0.15 (64bit) on [Wed Feb  7 01:16:21 2018] ***
compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-16) on 06 February 2018 20:03:13
os: Linux-3.10.0-693.17.1.el7.x86_64 #1 SMP Thu Jan 25 20:13:58 UTC 2018
nodename: python-political-bias-app
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /root/fyp
detected binary path: /root/fyp/env/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3807
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 127.0.0.1:8080 fd 3
Python version: 2.7.5 (default, Aug  4 2017, 00:39:18)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x74bba0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "./WSGI.py", line 1, in <module>
    from app import app
  File "./app/__init__.py", line 2, in <module>
    import pylab as pl
ImportError: No module named pylab
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***

我很丢失为什么,任何指针都会真正帮助,代码本身在本地运行得非常好,所以我不确定最新情况。

2 个答案:

答案 0 :(得分:1)

我认为您正在尝试使用为python3制作的python2模块。

自python3以来,某些模块名称已更改,python2

无法找到

尝试使用python3运行代码。您将不得不对代码进行少量更改(python2中的print"..."必须在python3中为print(...))但我认为这是您正在阅读的指南使用的版本。

答案 1 :(得分:0)

由于极其过时的软件或旧库,您很可能会遇到2/3转换编译器翻译问题。您看到的错误很可能已经深深嵌入,但GCC编译显示为程序表面附近的表面错误。因此,问题比仅由用户cdrom和回溯错误报告指示的2/3转换问题更深入。

确保使用pip或conda

将python及所有依赖的库更新为最新版本

我可以看到以下软件/库已经过时,可能会导致您的错误:

  

Python版本:2.7.5(2013年5月) - &gt; 2.7.14

     

GCC 4.8.5(2013/2015) - &gt; 7.3

     

uWSGI 2.0.15 - &gt; 2.0.16

正如您所看到的,回溯被剪裁/损坏只是为了显示核心问题。当代码在python中运行时,回溯中通常应该有大量的行来显示它弹出的位置,但事实并非如此。这可能是因为GCC编译器无法正确处理它。

  

追踪(最近一次通话):     文件“./WSGI.py”,第1行,in       来自app导入应用     文件“./app/init.py”,第2行,in       将pylab导入为pl

删除pylap import statement,因为它未在您提供的脚本代码中使用,因此将它保留原样是没有意义的。

提示:为了清楚起见,减少import语句并显示默认的编码懒惰:

from sklearn import svm
from sklearn import tree

应该是:

from sklearn import svm, tree

我希望您通过更新软件来清除错误。享受; - )

相关问题