Python: Flask import failing - python

I'm trying to deploy a Flask app on Apache using mod_wsgi. Im using a virtual environment. But the below line is failing.
from flask import Flask
Error:
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] mod_wsgi (pid=6937): Target WSGI script '/var/www/sample_rest/sample_rest.wsgi' cannot be loaded as Python module.
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] mod_wsgi (pid=6937): Exception occurred processing WSGI script '/var/www/sample_rest/sample_rest.wsgi'.
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] Traceback (most recent call last):
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/sample_rest.wsgi", line 5, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from sample_rest import sample_rest as application
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/sample_rest.py", line 2, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from flask import Flask, request, jsonify, make_response
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/flask/__init__.py", line 16, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from werkzeug.exceptions import abort
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/__init__.py", line 15, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from .serving import run_simple
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/serving.py", line 51, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from .exceptions import InternalServerError
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/exceptions.py", line 68, in <module>
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] from .utils import escape
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] File "/var/www/sample_rest/lib/python3.6/site-packages/werkzeug/utils.py", line 189
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] "area",
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] ^
[Wed Sep 23 07:58:50 2020] [error] [client 10.192.73.242] SyntaxError: invalid syntax
As you can see the error is within the code of flask itself. I'm using Python-3.6.2 which is supported by flask. The source code of flask in which it's showing error is also correct. What could be the issue ?

Sorry for posting this rather then commenting:
Try to install Flask 1.1.1 which worked for me:
pip install Flask==1.1.1

Related

Why am I getting internal server error with my python code?

I'm experimenting and working on a Flask webpage what's gonna use some data from MySQL database and after a little coding I got internal server error and I don't know why.
After several days I cant work out the problem what can I do for it to work?
I'm using Python3, the server where is hosted is Ubuntu system and I can reach the site via LAN right now with the servers LAN domain (server's name) name that I host via Hyper-V cause I don't have a spare PC for that with Ubuntu on it.
The weird part abut it that when I test it locally on my laptop I wont get the 500 Error only if I want to connect the page what's hosted on the server virtual machine.
Because I don't know where is the problem I uploaded most of the files to GitHub, this gonna be changed of course and later deleted for security reasons but now its easier.
Link: https://github.com/Csabatron99/Webpage
Here is the apache2 error log
[Wed Jul 07 12:42:08.597500 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] mod_wsgi (pid=10179): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jul 07 12:42:08.597550 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] mod_wsgi (pid=10179): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jul 07 12:42:08.597762 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] Traceback (most recent call last):
[Wed Jul 07 12:42:08.597784 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>
[Wed Jul 07 12:42:08.597788 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] from FlaskApp import app as application
[Wed Jul 07 12:42:08.597793 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 11, in <module>
[Wed Jul 07 12:42:08.597795 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] from .dbconnect import connection_db
[Wed Jul 07 12:42:08.597799 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] File "/var/www/FlaskApp/FlaskApp/dbconnect.py", line 3, in <module>
[Wed Jul 07 12:42:08.597802 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] import mysql.connector
[Wed Jul 07 12:42:08.597811 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] ModuleNotFoundError: No module named 'mysql'
[Wed Jul 07 12:42:08.816124 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] mod_wsgi (pid=10178): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816173 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] mod_wsgi (pid=10178): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816386 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] Traceback (most recent call last):, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816409 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816413 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] from FlaskApp import app as application, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816418 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 11, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816426 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] from .dbconnect import connection_db, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816431 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] File "/var/www/FlaskApp/FlaskApp/dbconnect.py", line 3, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816433 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] import mysql.connector, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816443 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] ModuleNotFoundError: No module named 'mysql', referer: http://tikva-server-hv/
and this is the code from the dbconnect.py
import mysql.connector
def connection_db():
conn1 = connector.connect(host="localhost",
user = "root",
passwd = "pass",
db = "db")
c1 = conn1.cursor()
return c1, conn1
Do not download the module I mistook content_management as pip module. you can uninstall the module pip uninstall contentful_management
The error comes because you are importing module in init.py.
try this:
from name_of_the_parent_directory import content_management
You an try using
from . import content_management
name_of_the_parent_directory - means the parent directory, in your github it will be webpage.

Scipy Crashes on Apache Server

I have a Python 2.7 script using NLTK that runs fine on command prompt. When I ran it in a local Apache server on the same machine, it crashes and the error log shows "NotImplementedError: cannot determine number of cpus" while loading scipy. More detailed messages are shown below. Anyone else have the same problem?
.....
[Thu Aug 09 18:07:50 2018] [error] [client ::1] File "interpnd.pyx", line 1, in init scipy.interpolate.interpnd\r, referer: http://localhost/index.html
[Thu Aug 09 18:07:50 2018] [error] [client ::1] File "C:\Anaconda2\lib\site-packages\scipy\spatial\__init__.py", line 95, in \r, referer: http://localhost/index.html
[Thu Aug 09 18:07:50 2018] [error] [client ::1] from .ckdtree import *\r, referer: http://localhost/index.html
[Thu Aug 09 18:07:50 2018] [error] [client ::1] File "ckdtree.pyx", line 31, in init scipy.spatial.ckdtree\r, referer: http://localhost/index.html
[Thu Aug 09 18:07:50 2018] [error] [client ::1] File "C:\Anaconda2\lib\multiprocessing\__init__.py", line 136, in cpu_count\r, referer: http://localhost/index.html
[Thu Aug 09 18:07:50 2018] [error] [client ::1] raise NotImplementedError('cannot determine number of cpus')\r, referer: http://localhost/index.html
[Thu Aug 09 18:07:50 2018] [error] [client ::1] NotImplementedError: cannot determine number of cpus\r, referer: http://localhost/index.html

ImportError: No module named types

I am getting below error when I start the httpd server and access the app url -
[Tue Feb 21 22:01:51 2017] [error] [client 10.209.33.139] mod_wsgi (pid=15425): Target WSGI script '/scratch/gdudwadk/python-projects/newblog/app/wsgi.py' cannot be loaded as Python module.
[Tue Feb 21 22:01:51 2017] [error] [client 10.209.33.139] mod_wsgi (pid=15425): Exception occurred processing WSGI script '/scratch/gdudwadk/python-projects/newblog/app/wsgi.py'.
[Tue Feb 21 22:01:51 2017] [error] Traceback (most recent call last):
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/app/wsgi.py", line 7, in <module>
[Tue Feb 21 22:01:51 2017] [error] from app import app as application
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/app/app.py", line 1, in <module>
[Tue Feb 21 22:01:51 2017] [error] from flask import Flask
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/venv/lib/python2.6/site-packages/flask/__init__.py", line 17, in <module>
[Tue Feb 21 22:01:51 2017] [error] from werkzeug.exceptions import abort
[Tue Feb 21 22:01:51 2017] [error] File "/scratch/gdudwadk/python-projects/newblog/venv/lib/python2.6/site-packages/werkzeug/__init__.py", line 17, in <module>
[Tue Feb 21 22:01:51 2017] [error] from types import ModuleType
[Tue Feb 21 22:01:51 2017] [error] ImportError: No module named types
This started happening after my machine was replaced with new one. Previously app was working fine.
I found one reference to python2.7 in /etc/httpd/conf.d/wsgi.conf and that's what was causing the issue. After removing that, the app was up and running.. Thanks!

Configuration file for Flask application

I'm new to Python, so please bear with me.
I am attempting to create a file in which to store my configuration settings in a Flask project. However, I seem to be getting errors when I attempt to import the file.
Here's my configuration file (location: app/config.py):
database_uri = 'something here'
secret_key = something here"
And here's where I'm using it (location: app/models.py):
from app import config
...
app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
However, I seem to be getting this error when launching the application:
[Sat Aug 08 19:00:15.539773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Target WSGI script '/var/www/pwforum/pwforum.wsgi' cannot be loaded as Python module.
[Sat Aug 08 19:00:15.540014 2015] [:error] [pid 29784] [client 188.183.57.54:64122] mod_wsgi (pid=29784): Exception occurred processing WSGI script '/var/www/pwforum/pwforum.wsgi'.
[Sat Aug 08 19:00:15.540146 2015] [:error] [pid 29784] [client 188.183.57.54:64122] Traceback (most recent call last):
[Sat Aug 08 19:00:15.540250 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/pwforum.wsgi", line 7, in <module>
[Sat Aug 08 19:00:15.540448 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app import app as application
[Sat Aug 08 19:00:15.540537 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/__init__.py", line 12, in <module>
[Sat Aug 08 19:00:15.540685 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app import views, models
[Sat Aug 08 19:00:15.540773 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/views.py", line 3, in <module>
[Sat Aug 08 19:00:15.541061 2015] [:error] [pid 29784] [client 188.183.57.54:64122] from app.models import db, User, Category, Topic, Post
[Sat Aug 08 19:00:15.541154 2015] [:error] [pid 29784] [client 188.183.57.54:64122] File "/var/www/pwforum/app/models.py", line 11, in <module>
[Sat Aug 08 19:00:15.541333 2015] [:error] [pid 29784] [client 188.183.57.54:64122] app.config['SQLALCHEMY_DATABASE_URI'] = config.database_uri
[Sat Aug 08 19:00:15.541413 2015] [:error] [pid 29784] [client 188.183.57.54:64122] AttributeError: 'module' object has no attribute 'database_uri'
Your config file should look like this:
SQLALCHEMY_DATABASE_URI = '<your-db-driver>://<user>:<pw>#<db-url>'
SECRET_KEY = '<your-very-secret-key>'
Then you can do:
app = Flask(__name__)
app.config.from_object('config')

apache python error

I am getting this in my error log file. Ive been searching for asolution but have found nothing. I am trying to deploy django with mod_wsgi.
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] ImportError: /usr/lib/python2.6/lib-dynload/_functools.so: wrong ELF class: ELFCLASS32
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] mod_wsgi (pid=20219): Target WSGI script '/home/dbs/public_html/test.wsgi' cannot be loaded as Python module.
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] mod_wsgi (pid=20219): Exception occurred processing WSGI script '/home/dbs/public_html/test.wsgi'.
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] Traceback (most recent call last):
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/home/dbs/public_html/test.wsgi", line 6, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] import django.core.handlers.wsgi
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/home/dbs/sites/domain.com/django/core/handlers/wsgi.py", line 2, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] from threading import Lock
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/usr/lib/python2.6/threading.py", line 13, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] from functools import wraps
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] File "/usr/lib/python2.6/functools.py", line 10, in <module>
[Wed Jun 13 12:15:53 2012] [error] [client 207.46.13.117] from _functools import partial, reduce
You have the wrong version of the functional module installed. Either install the correct binary version, or use the pure-python version.

Categories