Flask blueprint import error : no module named - python

Hi Today I started using flask
I m trying to configure two blueprints
my project structure is as image below
Here is all my project codes
init.py
from flask import Flask
app = Flask(__name__)
from mod_image.controllers import mod_image
from mod_home.home import mod_home
#the app config
#app.config.from_object('config')
#declaring image registration module/blueprint
#from app.mod_image.controllers import mod_image as image_module
# Register blueprint(s)
app.register_blueprint(mod_home)
app.register_blueprint(mod_image)
if __name__ == "__main__":
app.run()
controllers.py
from flask import Blueprint
mod_image = Blueprint('mod_image', __name__)
#mod_image.route('/register')
def register():
return "This is an example app"
home.py
from flask import Blueprint
mod_home = Blueprint('mod_home', __name__)
#mod_home.route('/')
def showHome():
return "This is a home"
Here is the error log
[Mon Jul 06 17:24:05.338680 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): Failed to exec Python script file '/var/www/wanasissmarteye/wanasissmarteye.wsgi'.
[Mon Jul 06 17:24:05.338731 2020] [wsgi:error] [pid 15407] [client ::1:38506] mod_wsgi (pid=15407): Exception occurred processing WSGI script '/var/www/wanasissmarteye/wanasissmarteye.wsgi'.
[Mon Jul 06 17:24:05.338764 2020] [wsgi:error] [pid 15407] [client ::1:38506] Traceback (most recent call last):
[Mon Jul 06 17:24:05.338805 2020] [wsgi:error] [pid 15407] [client ::1:38506] File "/var/www/wanasissmarteye/wanasissmarteye.wsgi", line 7, in
[Mon Jul 06 17:24:05.338860 2020] [wsgi:error] [pid 15407] [client ::1:38506] from wanasissmarteye import app as application
[Mon Jul 06 17:24:05.338889 2020] [wsgi:error] [pid 15407] [client ::1:38506] File "/var/www/wanasissmarteye/wanasissmarteye/init.py", line 11, in
[Mon Jul 06 17:24:05.339028 2020] [wsgi:error] [pid 15407] [client ::1:38506] from mod_home.home import mod_home
[Mon Jul 06 17:24:05.339063 2020] [wsgi:error] [pid 15407] [client ::1:38506] ImportError: No module named mod_home.home

An __init__.py file is required for python to recognize your mod_home and mod_image as
modules and import submodules from them.
Add one in the root of each folder you want to import from.
See this post: Importing files from different folder

OK
since I m a newbie to Python. addind init.py fixed the issue
after I googled
The init.py files are required to make Python treat the directories as containing packages;

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.

ImportError: No module named cx_Oracle in apache

I deploy flask with apache on CentOS7
And I want Oracle database to insert or select when user access.
So I install cx_Oracle
pip install cx_Oracle
And run flask on virtual env.
flask run
It work with no error.
But I access to localhost:80 using apache, It occur Internal Server Error 500.
In error_log
[Fri Oct 23 09:12:34.623473 2020] [mpm_prefork:notice] [pid 475] AH00170: caught SIGWINCH, shutting down gracefully
[Fri Oct 23 09:12:35.678302 2020] [lbmethod_heartbeat:notice] [pid 510] AH02282: No slotmem from mod_heartmonitor
[Fri Oct 23 09:12:35.681622 2020] [mpm_prefork:notice] [pid 510] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations
[Fri Oct 23 09:12:35.681665 2020] [core:notice] [pid 510] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Fri Oct 23 09:12:38.074902 2020] [:error] [pid 513] [client 172.17.0.1:33640] mod_wsgi (pid=513): Target WSGI script '/var/www/hitme/wsgi.py' cannot be loaded as Python module.
[Fri Oct 23 09:12:38.074946 2020] [:error] [pid 513] [client 172.17.0.1:33640] mod_wsgi (pid=513): Exception occurred processing WSGI script '/var/www/hitme/wsgi.py'.
[Fri Oct 23 09:12:38.074973 2020] [:error] [pid 513] [client 172.17.0.1:33640] Traceback (most recent call last):
[Fri Oct 23 09:12:38.074994 2020] [:error] [pid 513] [client 172.17.0.1:33640] File "/var/www/hitme/wsgi.py", line 10, in <module>
[Fri Oct 23 09:12:38.075058 2020] [:error] [pid 513] [client 172.17.0.1:33640] from app import app as application
[Fri Oct 23 09:12:38.075070 2020] [:error] [pid 513] [client 172.17.0.1:33640] File "/var/www/hitme/app/__init__.py", line 4, in <module>
[Fri Oct 23 09:12:38.075138 2020] [:error] [pid 513] [client 172.17.0.1:33640] import cx_Oracle
[Fri Oct 23 09:12:38.075162 2020] [:error] [pid 513] [client 172.17.0.1:33640] ImportError: No module named cx_Oracle
I followed this article for deploy flask with apache.
https://dev.to/sm0ke/flask-deploy-with-apache-on-centos-minimal-setup-2kb7
and install cx_Oracle and I copied it where I thought I needed it.
Here my tree
/etc/httpd/modules/
|-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
|-cx_Oracle-8.0.1.dist-info
/var/www/hitme/lib/python3.6/site-packages
|-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
|-cx_Oracle-8.0.1.dist-info
/usr/local/lib/python3.6/site-packages
|-cx_Oracle.cpython-36m-x86_64-linux-gnu.so
|-cx_Oracle-8.0.1.dist-info
But it still gives the error.
Is there any other way?
Ensure that the required Oracle Instant Client is installed to the Apache user by downloading
https://download.oracle.com/otn_software/linux/instantclient/199000/instantclient-basic-linux.x64-19.9.0.0.0dbru.zip
and unzip ut to a folder Apache user owns or is accessible for read and execute - lets say
/apache/oracle
After that, ensure PATH, ORACLE_HOME and LD_LIBRARY_PATH ist set properly as Apache User - i.e.
export ORACLE_HOME=/apache/oracle
export PATH=$PATH:$ORACLE_HOME
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
If you start Apache as service then you may need to set the variables to root or user who starts apache.

ModuleNotFoundError: No module named 'django' while deploying to ElasticBeanstalk

Tried multiple solutions online but couldnt get it working !
Basically my application is deployed but http response is "Internal Server Error".
On investigating EB logs i found the following traceback.
[Sat Jun 08 10:28:56.880823 2019] [:error] [pid 4776] [remote 172.31.4.251:180] Traceback (most recent call last):
[Sat Jun 08 10:28:56.880846 2019] [:error] [pid 4776] [remote 172.31.4.251:180] File "/opt/python/current/app/modelforms/modelforms/wsgi.py", line 12, in <module>
[Sat Jun 08 10:28:56.880863 2019] [:error] [pid 4776] [remote 172.31.4.251:180] from django.core.wsgi import get_wsgi_application
[Sat Jun 08 10:28:56.880878 2019] [:error] [pid 4776] [remote 172.31.4.251:180] ModuleNotFoundError: No module named 'django'
[Sat Jun 08 10:29:10.828039 2019] [:error] [pid 4776] [remote 172.31.42.54:180] mod_wsgi (pid=4776): Target WSGI script '/opt/python/current/app/modelforms/modelforms/wsgi.py' cannot be loaded as Python module.
[Sat Jun 08 10:29:10.828089 2019] [:error] [pid 4776] [remote 172.31.42.54:180] mod_wsgi (pid=4776): Exception occurred processing WSGI script '/opt/python/current/app/modelforms/modelforms/wsgi.py'.
My requirements.txt is placed in all directories and yet django module is not recognised. It seems it hasn't been installed at first place.
The file's contents are:
Django==2.2.1
Pillow==6.0.0
pytz==2019.1
sorl-thumbnail==12.5.0
sqlparse==0.3.0
Any help is appreciated.
EDIT1:
I dont have ebextentions setup, however the same config's are present when i run " eb config"
aws:elasticbeanstalk:container:python:
NumProcesses: '1'
NumThreads: '15'
StaticFiles: /static/=static/
WSGIPath: modelforms/modelforms/wsgi.py
aws:elasticbeanstalk:container:python:staticfiles:
/static/: static/
aws:elasticbeanstalk:environment:
EnvironmentType: LoadBalanced
ExternalExtensionsS3Bucket: null
ExternalExtensionsS3Key: null
LoadBalancerType: application
ServiceRole: aws-elasticbeanstalk-service-role
Try adding your DJANGO_SETTINGS_MODULE and PYTHONPATH to the config
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "modelforms.settings"
"PYTHONPATH": "/opt/python/current/app/modelforms:$PYTHONPATH"
Also, check the Health dashboard and make sure it shows as deployed before testing

Exception occurred processing WSGI script SyntaxError from item-catalog import app as application

I am getting an internal server error after trying to set up my python project with apache. There's something wrong with this line SyntaxError:
from item-catalog import app as application
But if I change it to this, it also doesn't work.
import app as application
work. My folder structure is
/var/www/html/itemcatalog/item-catalog
This is my itemcatalog.wsgi file
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/html/itemcatalog")
from item-catalog import app as application
application.secret_key = 'kGTdasfdasfdsa'
This is the apache error log:
[Tue Dec 13 02:12:09.149063 2016] [:error] [pid 29155:tid 139672369694464] [client 108.49.103.204:53087] mod_wsgi (pid=29155): Exception occurred processing WSGI script '/var/www/html/itemcatalog/item-catalog/itemcatalog.wsgi'.
[Tue Dec 13 02:12:09.149155 2016] [:error] [pid 29155:tid 139672369694464] [client 108.49.103.204:53087] File "/var/www/html/itemcatalog/item-catalog/itemcatalog.wsgi", line 7
[Tue Dec 13 02:12:09.149202 2016] [:error] [pid 29155:tid 139672369694464] [client 108.49.103.204:53087] from item-catalog import app as application
[Tue Dec 13 02:12:09.149244 2016] [:error] [pid 29155:tid 139672369694464] [client 108.49.103.204:53087] ^
[Tue Dec 13 02:12:09.149283 2016] [:error] [pid 29155:tid 139672369694464] [client 108.49.103.204:53087] SyntaxError: invalid syntax
Python module names cannot have hyphens in them

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

Categories