Import errors on flask with modules using .so and virtualenv - python

So I was trying to run my flask app in a virtualenv. But when trying to import mariadb which uses a .so file for the module I got this error:
File "(pathToEnv)/FlaskEnv/lib/python3.10/site- packages/mariadb/__init__.py", line 10$
[Mon Mar 21 14:52:43.074678 2022] [wsgi:error] [pid 27865] [client 192.168.1.128:42536] from ._mariadb import (
[Mon Mar 21 14:52:43.074723 2022] [wsgi:error] [pid 27865] [client 192.168.1.128:42536] ModuleNotFoundError: No module named 'mariadb._mariadb'
The same error happens for xxhash which also uses a .so file (possible correlation?) Every other module works fine under flask and when using either mariadb or xxhash in a normal python script they import fine.
Also notable is that this only started after I put Flask into a virtualenv. Before I had it run of the system wide ptyhon3.7 and now virtalenv python 3.10. I'm really puzzled what could be the cause of this, because it works fine in normal python scripts, but not on flask under apache

Related

Odd Syntax Error when trying to deploy app with apache and mod_wsgi

I'm learning about deploying flask apps using apache2.4 and mod_wsgi on an Ubuntu 16.04 server by building the simplest of "Hello, Word!" apps, but I encountered a weird syntax error related to my wsgi file and I'm stumped.
Apache seems to be running fine but when I hit the app's domain, I get apache's standard 500 error and the logs list the following:
[wsgi:error] [pid 28427:tid 140540431517440] mod_wsgi (pid=28427): Target WSGI script '/var/www/myapp.domain.com/myapp.wsgi' cannot be loaded as Python module.
[wsgi:error] [pid 28427:tid 140540431517440] mod_wsgi (pid=28427): Exception occurred processing WSGI script '/var/www/myapp.domain.com/myapp.wsgi'.
[wsgi:error] [pid 28427:tid 140540431517440] Traceback (most recent call last):
[wsgi:error] [pid 28427:tid 140540431517440] File "/var/www/myapp.domain.com/myapp.wsgi", line 5, in <module>
[wsgi:error] [pid 28427:tid 140540431517440] exec(file_.read(), dict(__file__=activate_this))
[wsgi:error] [pid 28427:tid 140540431517440] File "<string>", line 4
[wsgi:error] [pid 28427:tid 140540431517440] deactivate () {
[wsgi:error] [pid 28427:tid 140540431517440] ^
[wsgi:error] [pid 28427:tid 140540431517440] SyntaxError: invalid syntax
The syntax error referenced in the logs are pointing to code in my virtual environment’s activation script located at ./venv/bin/activate
I'm wondering if my use the venv module vs. virtualenv is causing this issue
Here is the content of my app's wsgi file (test is the name of the module were flask is instantiated):
#!/usr/bin/python3
activate_this = '/var/www/myapp.domain.com/venv/bin/activate'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import sys
sys.path.insert(0, '/var/www/myapp.domain.com')
from test import app as application
Using the activate_this.py script is not recommended even if you had got the name correct. For how to use a Python virtual environment with mod_wsgi see the documentation at:
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
Ensure you are also using daemon mode of mod_wsgi.
The details in that document are too much to replicate here and what method you use depends on how using mod_wsgi.
I have solved this problem by change the virtual environment which I built by 'python3 -m venv'. now I create the venv by virtualenv, and then re-install the necessary modules.
The reason is that the venv created by python3 hasn't the script of activate_this.py which is important in writing the myapp.wsgi file.
In the professional document, the reason is following:
When needing to activate the Python virtual environment from within the
WSGI script file as described, it is preferred that you be using the either
virtualenv or virtualenvwrapper to create the Python virtual environment.
This is because they both provide the activate_this.py script file which
does all the work of setting up sys.path. When you use either pyvenv or
python -m venv with Python 3, no such activation script is provided.
so good luck!
No matter what follows the curly brace (a set or dict display or dict comprehension) deactivate () { will not be legal Python code. I do not see anything odd or mysterious about getting a SyntaxError when you try to run it.

ImportError: No module named web - python in MAMP

I am running an apache server with MAMP in my Macbook pro. And I have added a python file named File.py to the CGI-Executable directory.
Contents of File.py:
#!/usr/bin/python
import web
import json
from xml.etree.ElementTree import Element, SubElement, Comment, tostring
urls = (
'/json', 'get_json'
)
app = web.application(urls, globals())
class get_json:
def GET(self):
pyDict = {'one':1,'two':2}
web.header('Content-Type', 'application/json')
return json.dumps(pyDict)
if __name__ == "__main__":
app.run()
I have installed web.py (by running pip install web.py) in my Mac. Now when I go to browser and enter http://localhost:8888/cgi-bin/File.py/json it is showing error 500 internal error, I checked the log file, log is
[Thu Mar 17 00:31:20 2016] [error] [client ::1] Traceback (most recent call last):
[Thu Mar 17 00:31:20 2016] [error] [client ::1] File " path-to-CGI-Executables/File.py", line 3, in <module>
[Thu Mar 17 00:31:20 2016] [error] [client ::1] import web
[Thu Mar 17 00:31:20 2016] [error] [client ::1] ImportError: No module named web
[Thu Mar 17 00:31:20 2016] [error] [client ::1] Premature end of script headers: File.py
Now I tried running the python file from terminal like
$ python File.py 127.0.0.1
Then in browser http://127.0.0.1:8080/json its working fine and showing {"two": 2, "one": 1} as result.
Whats happening if I add this to MAMP CGI? How to make installed modules available within MAMP?
UPDATE:
MAMP version : Version 3.5 (3.5)
Python version : Python 2.7.11 Mac
OS X version : 10.10.5 (14F1605)

ImportError: No module named django.core.wsgi in Elastic Beanstalk

I was getting started with AWS' Elastic Beanstalk.
I am following this tutorial to deploy a Django/PostgreSQL app.
I did everything before the 'Configuring a Database' section. The deployment was also successful but I am getting an Internal Server Error.
Here's the traceback from the logs:
mod_wsgi (pid=30226): Target WSGI script '/opt/python/current/app/polly/wsgi.py' cannot be loaded as Python module.
[Tue Sep 15 12:06:43.472954 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] mod_wsgi (pid=30226): Exception occurred processing WSGI script '/opt/python/current/app/polly/wsgi.py'.
[Tue Sep 15 12:06:43.474702 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] Traceback (most recent call last):
[Tue Sep 15 12:06:43.474727 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] File "/opt/python/current/app/polly/wsgi.py", line 12, in <module>
[Tue Sep 15 12:06:43.474777 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] from django.core.wsgi import get_wsgi_application
[Tue Sep 15 12:06:43.474799 2015] [:error] [pid 30226] [remote 172.31.14.126:53947] ImportError: No module named django.core.wsgi
Any idea what's wrong?
Have you created a requirements.txt in the root of your application? Elastic Beanstalk will automatically install the packages from this file upon deployment. (Note it might need to be checked into source control to be deployed.)
pip freeze > requirements.txt
(You will probably want to do that from within a virtualenv so that you only pick up the packages your app actually needs to run. Doing that with your system Python will pick up every package you've ever installed system-wide.)
The answer (https://stackoverflow.com/a/47209268/6169225) by carl-g is correct. One thing that got me was that requirements.txt was in the wrong directory. Let's say you created a django project called mysite. This is the directory in which you run the eb command(s) --> make sure requirements.txt is in this directory.
If you forget the .ebextensions folder you will get the same error.
I was following along with a good simple (non Elastic Beanstalk) tutorial and missed step 3 & 4 of Elastic Beanstalk.
I was using Django 1.11 and Python 2.7

ImportError: No module named parse

I am trying to run web application using mongodb and pymongo to serve data from database.
The error I am getting is ImportError: No module named parse. Please see below error.log from apache2 web server:
mod_wsgi (pid=18824): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[:error] [pid 18824:tid 139967053518592] mod_wsgi (pid=18824): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[:error] [pid 18824:tid 139967053518592] Traceback (most recent call last):
File "/var/www/FlaskApp/flaskapp.wsgi", line 12, in <module>
[:error] [pid 18824:tid 139967053518592] from ABC import app as application
[:error] [pid 18824:tid 139967053518592] File "var/www/FlaskApp/ABC/__init__.py", line 1, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo import MongoClient
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/__init__.py", line 92, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo.connection import Connection
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/connection.py", line 39, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo.mongo_client import MongoClient
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/mongo_client.py", line 46, in <module>
[:error] [pid 18824:tid 139967053518592] from pymongo import (auth,
[:error] [pid 18824:tid 139967053518592] File "/var/www/FlaskApp/ABC/venv/lib/python3.4/site-packages/pymongo/uri_parser.py", line 18, in <module>
[:error] [pid 18824:tid 139967053518592] from urllib.parse import unquote_plus
[:error] [pid 18824:tid 139967053518592] ImportError: No module named parse
I have virtual environment for Python 3.4, Flask, and pymongo. I am using mongodb 2.6.7.
Any ideas what causes issue?
It looks like you are running your app with python 2.x, but the modules your app uses are from python 3.x. In particular, pymongo is trying to import the module urllib.parse, which was called urlparse in python 2.x. As a result, executing import urllib.parse with python 2.x causes an ImportError.
I have virtual environment for Python 3.4,
How did you activate your virtual environment?
Response to comment:
I think I did not use virtual environment to install Mongodb
That's fine. The pymongo code inside your virtual env is what connects to your mongodb server (using a specified port).
After installation of Flask and pymongo I deactivated virtual environment.
Have you followed the instructions in the Flask docs with regards to mod_wsgi, virtualenv, and setting the activate_this variable?
Response to comment #2:
My web-site works in a static mode, only when I start using database
it stops working due to this problem with parse module
Yes, your site works fine while being executed with python 2.x, but when you start using the db, you are using modules that try to import libraries inside python 3.x. As a result, if you continue to use python 2.x to execute your site, then you are not going to be using a db.
I could not figure out what I need to put inside activate_this.py.
Try this:
1) Go to the directory containing your virtual environment:
$ cd /some/path/to/venv
2) List all the files:
$ ls
3) Change into the bin directory:
$ cd bin
4) List all the files:
$ ls
5) Open the file activate_this.py and read the comments at the top, e.g.
$ vi activate_this.py
6) Click on the link to the Flash docs I posted in my previous response and read the material there again.
Python2.7:
from urlparse import urlparse
Python3:
from urllib.parse import urlparse
python2 urlparse

Make Python 2.6 see Django

In a harrowing attempt to get mod_wsgi to run on CentOS 5.4, I've added Python 2.6 as an optional library following the instructions here. The configuration seems fine except that when trying to ping the server the Apache log prints this error:
mod_wsgi (pid=20033, process='otalo', application='127.0.0.1|'): Loading WSGI script '...django.wsgi'.
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] mod_wsgi (pid=20033): Target WSGI script '...django.wsgi' cannot be loaded as Python module.
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] mod_wsgi (pid=20033): Exception occurred processing WSGI script '...django.wsgi'.
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] Traceback (most recent call last):
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] File "...django.wsgi", line 8, in <module>
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] import django.core.handlers.wsgi
[Sat Mar 27 16:11:45 2010] [error] [client 171.66.52.218] ImportError: No module named django.core.handlers.wsgi
when I go to my python2.6 install's command line and try 'import Django', the module is not found (ImportError). However, my default Python 2.4 installation (still working fine) is able to import successfully. How do I point Python 2.6 to Django?
You need to install Django specifically with/for the Python version that's meant to use it -- installs for 2.4 and 2.6 are always going to be separate (they have to be -- there are incompatibilities in binary and bytecode formats!). I don't know what, if any, possibilities CentOS offers for that -- I'd get Django's sources and install from sources (which boils down to: download, tar, cd, and finally a simple sudo python2.6 setup.py install -- or however else you run your 2.6 install of Python). Being a much better developer than I'm a system administrator, I always have a bias in favor of installing from sources for stuff that's really at the core of what I'm doing (especially Python versions and extensions).
I created a write up link text of my experiences of getting Django working on Redhat and and CentOS. I upgraded the version of python that I was using and started with init.d scripts.

Categories