Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Flask program giving module not found

Im building a python web application with flask and uWSGI following this lovely guide https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-centos-7 and it worked marvels, the basic hello does indeed appear in the website hen loaded. I have installed every single module and dependency in the project file. Im trying now to build on the working script and I now my init.py file looks like this:


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()
'''''

All is good with this as far as im concerned made sure to install all dependencies and module sin the folder from which im running the code. but when I run it I get the following error

'''''
[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 dir