ImportError: No module named cx_Oracle in apache - python

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.

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.

Django AWS Elastic Beanstalk error ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I know this error have come to many people and I have tried different solutions and none of them worked.
I am using aws eb cli.
I am using following command eb deploy to deploy my application to server.
Following are the configuration for my Django.
under .ebextensions directory, I have following 2 files:
1: 01_packages.config
packages:
yum:
git: []
python27-devel: []
mysql: []
mysql-devel: []
and another file is
2: 02_django.conf
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "settings.development"
"PYTHONPATH": "/opt/python/current/app/src:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: src/wsgi.py
NumProcesses: 3
NumThreads: 20
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
Following is my requirements.txt file after pip freeze in my local virtual environment.
requirements.txt
asn1crypto==0.24.0
awsebcli==3.17.1
backports.ssl-match-hostname==3.5.0.1
botocore==1.14.17
cement==2.8.2
cent==2.1.0
centrifuge==0.8.4
certifi==2017.11.5
cffi==1.11.2
chardet==3.0.4
colorama==0.3.9
cryptography==2.1.4
Django==1.8.18
django-colorfield==0.1.14
django-countries==5.0
django-debug-toolbar==1.9.1
django-environ==0.4.4
django-multiselectfield==0.1.8
django-simple-history==1.9.1
django-sslserver==0.20
docutils==0.15.2
enum34==1.1.6
future==0.16.0
google-api-python-client==1.6.4
hiredis==0.2.0
html5lib==1.0b8
httplib2==0.10.3
icalendar==4.0.0
idna==2.6
ipaddress==1.0.18
jmespath==0.9.4
jsonschema==2.4.0
mysqlclient==1.4.6
oauth2client==2.0.0
oauthclient==1.0.3
olefile==0.44
pathspec==0.5.9
paypalrestsdk==1.13.1
pdfcrowd==4.0.1
phonenumbers==8.8.6
Pillow==4.3.0
pyasn1==0.3.7
pyasn1-modules==0.1.5
pycparser==2.18
PyJWT==1.5.3
pyOpenSSL==17.5.0
PyPDF2==1.26.0
pypiwin32==219
pytesseract==0.1.7
python-dateutil==2.6.1
pytz==2017.3
PyYAML==5.2
reportlab==3.4.0
requests==2.18.4
rsa==3.4.2
semantic-version==2.5.0
six==1.11.0
sockjs-tornado==1.0.1
sqlparse==0.2.4
termcolor==1.1.0
toredis-fork==0.1.4
tornado==4.2.1
toro==0.8
twilio==6.9.1
uritemplate==3.0.0
urllib3==1.22
wcwidth==0.1.8
webencodings==0.5.1
xhtml2pdf==0.2.2
I kept this in my root directory.
When I run eb deploy
it deploys successfully.
but when I run the browser to my url.
I get this Internal Server Error page.
So I tried to look in to the log files on the server under /var/log/httpd/error_log
i get the following error:
[Mon Feb 24 17:56:57.227427 2020] [:error] [pid 8054] [remote 101.50.93.65:188] mod_wsgi (pid=8054): Target WSGI script '/opt/python/current/app/src/wsgi.py' cannot be loaded as Python module.
[Mon Feb 24 17:56:57.227450 2020] [:error] [pid 8054] [remote 101.50.93.65:188] mod_wsgi (pid=8054): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 17:56:57.227466 2020] [:error] [pid 8054] [remote 101.50.93.65:188] Traceback (most recent call last):
[Mon Feb 24 17:56:57.227483 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/current/app/src/wsgi.py", line 17, in <module>
[Mon Feb 24 17:56:57.227585 2020] [:error] [pid 8054] [remote 101.50.93.65:188] application = get_wsgi_application()
[Mon Feb 24 17:56:57.227599 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Feb 24 17:56:57.227627 2020] [:error] [pid 8054] [remote 101.50.93.65:188] django.setup()
[Mon Feb 24 17:56:57.227634 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
[Mon Feb 24 17:56:57.227645 2020] [:error] [pid 8054] [remote 101.50.93.65:188] apps.populate(settings.INSTALLED_APPS)
[Mon Feb 24 17:56:57.227651 2020] [:error] [pid 8054] [remote 101.50.93.65:188] File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 78, in populate
[Mon Feb 24 17:56:57.227662 2020] [:error] [pid 8054] [remote 101.50.93.65:188] raise RuntimeError("populate() isn't reentrant")
[Mon Feb 24 17:56:57.227676 2020] [:error] [pid 8054] [remote 101.50.93.65:188] RuntimeError: populate() isn't reentrant
So I kept Googling, and found a post suggested to change wsgi file, and I did and then this error updated to the following lines of code.
[Mon Feb 24 18:23:12.169850 2020] [mpm_prefork:notice] [pid 9081] AH00169: caught SIGTERM, shutting down
[Mon Feb 24 18:23:13.289449 2020] [suexec:notice] [pid 10286] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Feb 24 18:23:13.305290 2020] [so:warn] [pid 10286] AH01574: module wsgi_module is already loaded, skipping
[Mon Feb 24 18:23:13.307373 2020] [http2:warn] [pid 10286] AH10034: The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more demands in this regard and the currently selected mpm will just not do. This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.
[Mon Feb 24 18:23:13.307384 2020] [http2:warn] [pid 10286] AH02951: mod_ssl does not seem to be enabled
[Mon Feb 24 18:23:13.307990 2020] [lbmethod_heartbeat:notice] [pid 10286] AH02282: No slotmem from mod_heartmonitor
[Mon Feb 24 18:23:13.308050 2020] [:warn] [pid 10286] mod_wsgi: Compiled for Python/2.7.13.
[Mon Feb 24 18:23:13.308057 2020] [:warn] [pid 10286] mod_wsgi: Runtime using Python/2.7.16.
[Mon Feb 24 18:23:13.311200 2020] [mpm_prefork:notice] [pid 10286] AH00163: Apache/2.4.41 (Amazon) mod_wsgi/3.5 Python/2.7.16 configured -- resuming normal operations
[Mon Feb 24 18:23:13.311217 2020] [core:notice] [pid 10286] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'
[Mon Feb 24 18:23:16.367182 2020] [:error] [pid 10293] [remote 127.0.0.1:0] mod_wsgi (pid=10293): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:16.367240 2020] [:error] [pid 10293] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:23:17.744228 2020] [:error] [pid 10291] [remote 127.0.0.1:0] mod_wsgi (pid=10291): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:17.744288 2020] [:error] [pid 10291] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:23:19.116825 2020] [:error] [pid 10292] [remote 127.0.0.1:0] mod_wsgi (pid=10292): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:19.116892 2020] [:error] [pid 10292] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:23:20.493432 2020] [:error] [pid 10418] [remote 127.0.0.1:0] mod_wsgi (pid=10418): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:23:20.493489 2020] [:error] [pid 10418] [remote 127.0.0.1:0] RuntimeError: response has not been started
[Mon Feb 24 18:36:44.987693 2020] [:error] [pid 10443] [remote 95.105.12.68:0] mod_wsgi (pid=10443): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:36:44.987766 2020] [:error] [pid 10443] [remote 95.105.12.68:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:28.298121 2020] [:error] [pid 10468] [remote 101.50.93.65:0] mod_wsgi (pid=10468): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:28.298180 2020] [:error] [pid 10468] [remote 101.50.93.65:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:30.126198 2020] [:error] [pid 10499] [remote 101.50.93.65:0] mod_wsgi (pid=10499): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:30.126260 2020] [:error] [pid 10499] [remote 101.50.93.65:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:31.671293 2020] [:error] [pid 10973] [remote 101.50.93.65:0] mod_wsgi (pid=10973): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:31.671358 2020] [:error] [pid 10973] [remote 101.50.93.65:0] RuntimeError: response has not been started
[Mon Feb 24 18:55:32.858757 2020] [:error] [pid 11606] [remote 101.50.93.65:0] mod_wsgi (pid=11606): Exception occurred processing WSGI script '/opt/python/current/app/src/wsgi.py'.
[Mon Feb 24 18:55:32.858821 2020] [:error] [pid 11606] [remote 101.50.93.65:0] RuntimeError: response has not been started
and this is the new wsgi.py file.
import os
import logging
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.development")
from django.core.wsgi import get_wsgi_application
# application = get_wsgi_application()
env_variables_to_pass = ['DJANGO_SETTINGS_MODULE', ]
def application(environ, start_response):
if environ['mod_wsgi.process_group'] != '':
import signal
os.kill(os.getpid(), signal.SIGINT)
return ["killed"]
and following was the old wsgi.py file
import os
import logging
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.development")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
env_variables_to_pass = ['DJANGO_SETTINGS_MODULE', ]
So then I tried to run application on the server using python manage.py runserver as there was nothing else to do to fix this and I was not sure where the problem is.
So I did ssh and I did used the virtualenvironment of server that was already there created by EB.
after running python manage.py runserver.
I get this following error.
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
So I tried to follow any advice that I could see available on google.
I tried pip install mysqlclient
I get this error.
Collecting mysqlclient
Using cached https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz
Complete output from command python setup.py egg_info:
sh: mysql_config: command not found
sh: mariadb_config: command not found
sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-MxFUOd/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config()
File "setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "setup_posix.py", line 29, in mysql_config
raise EnvironmentError("%s not found" % (_mysql_config_path,))
EnvironmentError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-MxFUOd/mysqlclient/
I tried both
pip install mysqlclient --no-cache-dir
and
pip install mysqlclient
It's Centos server so I cannot do the sudo apt-get commands.
Only yum commands work.
I tried this following command with sudo
sudo pip install mysql-connector-python
But I think this will be installed globally rather than env.
So I tried without sudo, and it gave permission error.
I used other commands to install mysql both with sudo and not sudo.
pip install pymysql
sudo yum install python-mysqldb
No matter what I do I get this MySQL error.
I don't want to move to other database as I would have to move data as well.
UPDATE
from given suggession of #Arun K i ran this following command
which mysql_config
i got this following response.
/usr/bin/which: no mysql_config in (/opt/python/run/venv/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/.local/bin:/home/ec2-user/bin)
in my case on on 64bit Amazon Linux 2/3.0.1
i had to install the following via *.config inside .ebextensions
packages:
yum:
python3-devel: []
mariadb-devel: []
gcc: []
Try running
sudo yum install mysql-devel gcc python-devel
Then
pip install mysqlclient
Why don't you use docker and run the Django app as a container? The elastic beanstalk also supports the docker type. There are so many articles on the internet detailing the steps. you do not have to worry about centos.
Reference:
Django on Docler

Target WSGI script myapp.wsgi' cannot be loaded as Python module, ModuleNotFoundError: No module named 'flask'

I a trying to run a "hello world" python application from the server using Apache web server port 80. Here, I have setup python virtual environment compacting with python version 3.8. created virtual host, wsgi file and now trying to call the URL on browser, I am getting 500 internal server error. Apache error log file details provided here.
1. Ubuntu 18.04,
2. Python 3.8,
3. Apache 2.4
Virtual host configuration
#WSGIDaemonProcess MyApp python-home=/usr/local/env/myApp/venv threads=5i
WSGIDaemonProcess Myapp user=www-data group=www-data python-home=/usr/local/env/myApp/venv python-path=/usr/local/env/myApp threads=5
WSGIScriptAlias /project_wsgi /usr/local/env/myApp/myapp.wsgi
#WSGIProcessGroup myApp
WSGIApplicationGroup Myapp
#WSGIImportScript %{GLOBAL}
#WSGIPythonHome /usr/local/env/myApp/venv
<VirtualHost *:80>
<Directory /usr/local/env/myApp>
Require all granted
Order allow,deny
Allow from all
LogLevel info
</Directory>
</VirtualHost>
WSGI file
import os
import sys
sys.path.insert(0, '/usr/local/env/myApp/')
python_home = '/usr/local/env/myApp/venv'
#activate_this = '/usr/local/env/myApp/venv/bin/activate_this.py'
#with open(activate_this) as file_:
# exec(file_.read(), dict(__file__=activate_this))
from app.myApp import app as application
apache error logs
[Fri Jan 31 16:00:28.990978 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Starting process 'Myapp' with uid=33, gid=33 and threads=5.
[Fri Jan 31 16:00:28.991819 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Python home /usr/local/env/myApp/venv.
[Fri Jan 31 16:00:28.991863 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Initializing Python.
[Fri Jan 31 16:00:28.991926 2020] [mpm_event:notice] [pid 102854:tid 140712013437888] AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Fri Jan 31 16:00:28.991937 2020] [mpm_event:info] [pid 102854:tid 140712013437888] AH00490: Server built: 2019-09-16T12:58:48
[Fri Jan 31 16:00:28.991952 2020] [core:notice] [pid 102854:tid 140712013437888] AH00094: Command line: '/usr/sbin/apache2'
[Fri Jan 31 16:00:28.993170 2020] [wsgi:info] [pid 58591:tid 140712013437888] mod_wsgi (pid=58591): Initializing Python.
[Fri Jan 31 16:00:28.993874 2020] [wsgi:info] [pid 58590:tid 140712013437888] mod_wsgi (pid=58590): Initializing Python.
[Fri Jan 31 16:00:29.061111 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Attach interpreter ''.
[Fri Jan 31 16:00:29.088271 2020] [wsgi:info] [pid 58589:tid 140712013437888] mod_wsgi (pid=58589): Adding '/usr/local/env/myApp' to path.
[Fri Jan 31 16:00:29.089075 2020] [wsgi:info] [pid 58591:tid 140712013437888] mod_wsgi (pid=58591): Attach interpreter ''.
[Fri Jan 31 16:00:29.089966 2020] [wsgi:info] [pid 58590:tid 140712013437888] mod_wsgi (pid=58590): Attach interpreter ''.
[Fri Jan 31 16:02:22.375687 2020] [wsgi:info] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] mod_wsgi (pid=58591, process='', application='Myapp'): Loading WSGI script '/usr/local/env/myApp/myapp.wsgi'.
[Fri Jan 31 16:02:22.381101 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] mod_wsgi (pid=58591): Target WSGI script '/usr/local/env/myApp/myapp.wsgi' cannot be loaded as Python module.
[Fri Jan 31 16:02:22.381132 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] mod_wsgi (pid=58591): Exception occurred processing WSGI script '/usr/local/env/myApp/myapp.wsgi'.
[Fri Jan 31 16:02:22.381374 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] Traceback (most recent call last):
[Fri Jan 31 16:02:22.381400 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] File "/usr/local/env/myApp/myapp.wsgi", line 10, in <module>
[Fri Jan 31 16:02:22.381405 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] from app.myApp import app as application
[Fri Jan 31 16:02:22.381411 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] File "/usr/local/env/myApp/app/myApp.py", line 1, in <module>
[Fri Jan 31 16:02:22.381414 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] from flask import Flask
[Fri Jan 31 16:02:22.381436 2020] [wsgi:error] [pid 58591:tid 140711785658112] [client 146.176.178.57:50395] ModuleNotFoundError: No module named 'flask'
I have installed flask on the virtual environment and trying to activate from the WSGI file execution. However, this is not working for me.
FYI : The commented lines on virtual host is already I have tried.

Why Apache server throws syntax error with snowflake ingest module in python

I have a flask application running on apache server in the Linux box(ec2 instance), which calls the python script that does the work of moving the files from s3 to snowflake. The script works fine if executed directly. It throws the below error for the snowflake's ingest python module when trying to host it on apache using virtualhost and mod_wsgi. The module is installed in the default site packages.
Apache server version: 2.4.41
Python Version: 3.7
snowflake: snowflake_connector_python-2.1.3-py3.7-nspkg.pth
OS: AMAZON linux 2
 
Below is the apache error log, when i try to restart:
[Fri Jan 10 15:47:53.703083 2020] [:error] [pid 19755] [remote 67.79.202.36:20] mod_wsgi (pid=19755): Target WSGI script '/var/www/FLASKAPPS/snowflakeingestapp/snowflakeingestapp.wsgi' cannot be loaded as Python module.
[Fri Jan 10 15:47:53.703113 2020] [:error] [pid 19755] [remote 67.79.202.36:20] mod_wsgi (pid=19755): Exception occurred processing WSGI script '/var/www/FLASKAPPS/snowflakeingestapp/snowflakeingestapp.wsgi'.
[Fri Jan 10 15:47:53.703129 2020] [:error] [pid 19755] [remote 67.79.202.36:20] Traceback (most recent call last):
[Fri Jan 10 15:47:53.703142 2020] [:error] [pid 19755] [remote 67.79.202.36:20]  File "/var/www/FLASKAPPS/snowflakeingestapp/snowflakeingestapp.wsgi", line 4, in
[Fri Jan 10 15:47:53.703176 2020] [:error] [pid 19755] [remote 67.79.202.36:20]    from snowflakeingestapp import app as application
[Fri Jan 10 15:47:53.703181 2020] [:error] [pid 19755] [remote 67.79.202.36:20]  File "/var/www/FLASKAPPS/snowflakeingestapp/init.py", line 6, in
[Fri Jan 10 15:47:53.703206 2020] [:error] [pid 19755] [remote 67.79.202.36:20]    import snowflake_ingest
[Fri Jan 10 15:47:53.703210 2020] [:error] [pid 19755] [remote 67.79.202.36:20]  File "/var/www/FLASKAPPS/snowflakeingestapp/snowflake_ingest/init.py", line 1, in
[Fri Jan 10 15:47:53.703225 2020] [:error] [pid 19755] [remote 67.79.202.36:20]    import ingest_ecomm_json_snowpipe
[Fri Jan 10 15:47:53.703230 2020] [:error] [pid 19755] [remote 67.79.202.36:20]  File "/var/www/FLASKAPPS/snowflakeingestapp/snowflake_ingest/ingest_ecomm_json_snowpipe.py", line 20, in
[Fri Jan 10 15:47:53.703296 2020] [:error] [pid 19755] [remote 67.79.202.36:20]    from snowflake.ingest import SimpleIngestManager
[Fri Jan 10 15:47:53.703300 2020] [:error] [pid 19755] [remote 67.79.202.36:20]  File "/usr/local/lib/python3.7/site-packages/snowflake/ingest/init.py", line 1, in
[Fri Jan 10 15:47:53.703315 2020] [:error] [pid 19755] [remote 67.79.202.36:20]    from .simple_ingest_manager import SimpleIngestManager, StagedFile
[Fri Jan 10 15:47:53.703342 2020] [:error] [pid 19755] [remote 67.79.202.36:20]  File "/usr/local/lib/python3.7/site-packages/snowflake/ingest/simple_ingest_manager.py", line 65
[Fri Jan 10 15:47:53.703345 2020] [:error] [pid 19755] [remote 67.79.202.36:20]    def init(self, account: Text, user: Text, pipe: Text, private_key: Text,
[Fri Jan 10 15:47:53.703347 2020] [:error] [pid 19755] [remote 67.79.202.36:20]                              ^
[Fri Jan 10 15:47:53.703350 2020] [:error] [pid 19755] [remote 67.79.202.36:20] SyntaxError: invalid syntax
It looks like when you're executing this from mod_wsgi, you're using Python 2 instead of Python 3. The error message you see is happening because Snowflake's simple ingest manager uses type hints (via the typing module), which are not available in Python 2.
Here's a simple fragment that fails with the same error in Python 2 but succeeds in Python 3:
from typing import Text
def foo(bar: Text):
print(bar)
foo('huh')

wsgi.py' cannot be loaded as Python module - Python3

I am making use of python3 for building the application
[Tue Jan 16 19:07:13.587669 2018] [wsgi:error] [pid 22631:tid 140432910448384] [remote 172.19.217.162:48145] mod_wsgi (pid=22631): Target WSGI script '/u0/shsathya/html/captool/captool/wsgi.py' cannot be loaded as Python module.
[Tue Jan 16 19:07:13.587752 2018] [wsgi:error] [pid 22631:tid 140432910448384] [remote 172.19.217.162:48145] mod_wsgi (pid=22631): Exception occurred processing WSGI script '/u0/shsathya/html/captool/captool/wsgi.py'.
[Tue Jan 16 19:07:13.587979 2018] [wsgi:error] [pid 22631:tid 140432910448384] [remote 172.19.217.162:48145] Traceback (most recent call last):
[Tue Jan 16 19:07:13.588026 2018] [wsgi:error] [pid 22631:tid 140432910448384] [remote 172.19.217.162:48145] File "/u0/shsathya/html/captool/captool/wsgi.py", line 14, in <module>
[Tue Jan 16 19:07:13.588036 2018] [wsgi:error] [pid 22631:tid 140432910448384] [remote 172.19.217.162:48145] from django.core.wsgi import get_wsgi_application
[Tue Jan 16 19:07:13.588059 2018] [wsgi:error] [pid 22631:tid 140432910448384] [remote 172.19.217.162:48145] ImportError: No module named 'django.core'
Please make sure that django is in PYTHONPATH.
To test import django from a python shell
Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
>>>import django
>>>
You need to setup mod_wsgi which has been compiled against python 3 version you use.
Please refer here to verify what version of python your mod_wsgi is compiled for-> http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html

Categories