找不到烧瓶程序提供模块
flask program giving module not found
我正在按照这个可爱的指南 https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-centos-7 使用 Flask 和 uWSGI 构建一个 python Web 应用程序并且它取得了奇迹。我想说我已经在项目文件中安装了每个模块和依赖项。我现在正在尝试构建工作脚本,现在我的 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 ***
我完全不知道为什么,任何指针都会真正提供帮助,代码本身 运行 在本地非常好,所以我不确定发生了什么。
我认为您正在尝试 运行 使用为 python3 制作的 python2 模块。
某些模块名称自 python3 以来已更改,无法被 python2
找到
只需尝试 运行 您的代码 python3。您将不得不对代码进行少量更改(python2 中的 print"..."
必须是 python3 中的 print(...)
),但我认为这是您的指南使用的版本正在阅读。
您手头的 2/3 转换 编译器翻译 问题很可能是由极其过时的软件或旧库引起的。您看到的错误可能是深深嵌入的,但在 GCC 编译过程中显示为靠近程序表层的表面错误。因此,这个问题 运行 比用户 cdrom 和您的回溯错误报告所指出的 2/3 转换问题更深一点。
确保使用 pip 或 condapython 将 python 和所有依赖的库更新到最新版本
我看到以下 software/libs 已过时,可能会导致您的错误:
Python version: 2.7.5 (may 2013) --> 2.7.14
GCC 4.8.5 (2013/2015) --> 7.3
uWSGI 2.0.15 --> 2.0.16
如您所见,回溯 clipped/mangled 只是为了显示核心问题。当代码在 python 中为 运行 时,回溯中通常应该有大量的行显示它从哪里弹出,但事实并非如此。这可能是因为 GCC 编译器无法正确处理它。
Traceback (most recent call last):
File "./WSGI.py", line 1, in
from app import app
File "./app/init.py", line 2, in
import pylab as pl
删除 pylap import statement
,因为它未在您提供的脚本代码中使用,因此将其保留原样没有任何意义。
提示:trim-为了清晰起见,请在导入语句中下调并显示默认编码惰性:
from sklearn import svm
from sklearn import tree
应该是:
from sklearn import svm, tree
我希望您通过更新软件来消除错误。享受 ;-)
我正在按照这个可爱的指南 https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-centos-7 使用 Flask 和 uWSGI 构建一个 python Web 应用程序并且它取得了奇迹。我想说我已经在项目文件中安装了每个模块和依赖项。我现在正在尝试构建工作脚本,现在我的 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 ***
我完全不知道为什么,任何指针都会真正提供帮助,代码本身 运行 在本地非常好,所以我不确定发生了什么。
我认为您正在尝试 运行 使用为 python3 制作的 python2 模块。
某些模块名称自 python3 以来已更改,无法被 python2
找到只需尝试 运行 您的代码 python3。您将不得不对代码进行少量更改(python2 中的 print"..."
必须是 python3 中的 print(...)
),但我认为这是您的指南使用的版本正在阅读。
您手头的 2/3 转换 编译器翻译 问题很可能是由极其过时的软件或旧库引起的。您看到的错误可能是深深嵌入的,但在 GCC 编译过程中显示为靠近程序表层的表面错误。因此,这个问题 运行 比用户 cdrom 和您的回溯错误报告所指出的 2/3 转换问题更深一点。
确保使用 pip 或 condapython 将 python 和所有依赖的库更新到最新版本
我看到以下 software/libs 已过时,可能会导致您的错误:
Python version: 2.7.5 (may 2013) --> 2.7.14
GCC 4.8.5 (2013/2015) --> 7.3
uWSGI 2.0.15 --> 2.0.16
如您所见,回溯 clipped/mangled 只是为了显示核心问题。当代码在 python 中为 运行 时,回溯中通常应该有大量的行显示它从哪里弹出,但事实并非如此。这可能是因为 GCC 编译器无法正确处理它。
Traceback (most recent call last): File "./WSGI.py", line 1, in from app import app File "./app/init.py", line 2, in import pylab as pl
删除 pylap import statement
,因为它未在您提供的脚本代码中使用,因此将其保留原样没有任何意义。
提示:trim-为了清晰起见,请在导入语句中下调并显示默认编码惰性:
from sklearn import svm
from sklearn import tree
应该是:
from sklearn import svm, tree
我希望您通过更新软件来消除错误。享受 ;-)