passlib cannot be imported when using uwsgi - python

I'm using flask-restful and wanted to use bcrypt from passlib to hash users passwords stored in database, when I run the solution by python3 app.py, everything works perfectly and I can hash the passwords and validate them , but when I run the solution through uwsgi uwsgi uwsgi.ini I get internal server error when I try to authenticate.
When I review the log file from passlib.hash import bcrypt ImportError: No module named 'passlib' , although I can import it successfully from command line as well.
I tried to do one solution suggested in :
No module named 'passlib'
and to add "##UnresolvedImport" to import but still didn't work.

After not getting any answer , I was struggling for couple of days, afterward I just deleted the virtual environment , created a new one and re-installed all required libraries "flask , flask-restful , flask_bcrypt...etc , and even uwsgi ".
Afterward I could start the application with uwsgi without getting the ImportError.

Related

deamon celery fails to start "module blah was not found" or "ImportError: attempted relative import with no known parent package"

I have setup flask with apache (wsgi) under a venv running python 3.6 on ubuntu 18.04 lts and imported other modules with success. Including celery with rabbitmq.
I followed this guidance for Celery and Flask https://flask.palletsprojects.com/en/1.1.x/patterns/celery/ and am semi confident celery is called by flask. Because when I run
celery worker -A views.celery
it successfully runs and I can see the workers collecting the tasks from what I guess is the rabbitmq queue. (I'm still getting my head around all this).
When I follow the numerous guides online to use systemd or init.d or even the older supervisord I come in to the same problem, which mirrors this Python - Module Not Found but I have followed it without success.
So to confirm, my error is
ImportError: attempted relative import with no known parent package
and the line in question was from
views.celery
with line
from .static.py.forms import blah
where static is the usual flask directory, and py is a directory where I store a forms. py file.
So because this is using relative importing (So I'm to believe because of the period . ) I remove the period. Flask continues to work. But the new error I receive is
module static.py was not found
Following the guidance from the other stackoverflow question, I added a
Environment=PATH="/var/www/Flask/app/"
in the Unit celeryd file. I also have since added blank __init__. py files in the directories static and py.
Appreciate any assistance!!
I have also used these resources but suspect due to my lack of knowledge I have made an error somewhere.
https://medium.com/#MicroPyramid/celery-with-supervisor-12522ec397ed
https://github.com/celery/celery/blob/3.1/extra/supervisord/celeryd.conf
https://docs.celeryproject.org/en/4.4.2/userguide/daemonizing.html#daemon-systemd-generic
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units
https://stackoverflow.com/questions/21880360/daemonizing-celery
https://www.cyberciti.biz/faq/unix-create-user-account/
https://www.maketecheasier.com/add-remove-user-to-groups-in-ubuntu/
https://stackoverflow.com/questions/16981921/relative-imports-in-python-3
https://napuzba.com/a/import-error-relative-no-parent
fixed. It was because I had a folder named 'py'
-_- so annoying

How to 'pip install packages' inside Azure WebJob to resolve package compatibility issues

I am deploying a WebJob inside Azure Web App that uses Google Maps API and Azure SQL Storage.
I am following the typical approach where I make a WebJob directory and copy my 'site-packages' folder inside the root folder of the WebJob. Then I also add my code folder inside 'site-packages' and make a run.py file inside the root that looks like this:
import sys, os
sys.path.append(os.path.join(os.getcwd(), "site-packages"))
import aero2.AzureRoutine as aero2
aero2.run()
Now the code runs correctly in Azure. But I am seeing warnings after a few commands which slow down my code.
I have tried copying 'pyopenSSL' and 'requests' module into my site-packages folder, but the error persists.
However, the code runs perfectly on my local machine.
How can I find this 'pyopenSSL' or 'requests' that is compatible with the python running on Azure?
Or
How can I modify my code so that it pip installs the relevant packages for the python running on Azure?
Or more importantly,
How can I resolve this error?
#Saad,
If your webjob worked fine on Azure Web App, but you got inscuritywaring, I suggest you can try to disable the warning information via this configuration(https://urllib3.readthedocs.org/en/latest/security.html#disabling-warnings ).
Meanwhile,requests lib has some different with the high version, I recommend you refer to this document:
http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html
And Azure web app used the Python 2.7.8 version which is lower than 2.7.9. So you can download the requests lib as version 2.5.3
According the doc referred in the warning message https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning:
Certain Python platforms (specifically, versions of Python earlier than 2.7.9) have restrictions in their ssl module that limit the configuration that urllib3 can apply. In particular, this can cause HTTPS requests that would succeed on more featureful platforms to fail, and can cause certain security features to be unavailable.
So the easiest way fix this warning, is to upgrade the python version of the Azure Web Apps. Login the Azure manager portal, change the python version to 3.4 in Application settings column:
As I test in webjob task to use requests module to request a "https://" url, and since upgrade python version to 3.4, there are no more warnings.
I followed this article and kind of 'pip installed' the pymongo library for my script. Not sure if it works for you but here are the steps:
Make sure you include the library name and version in the requirements.txt
Deploy the web app using Git. The directory should include at least
requirements.txt (only to install whatever is in requirements.txt in the virtual environment, which is shared with Web App in D:\home\site\wwwroot\env\Lib\site-packages)
add this block of code to the Python code you want to use in the WebJob zip file.
import sys
sitepackage = "D:\home\site\wwwroot\env\Lib\site-packages"
sys.path.append(sitepackage)

'no module named' error even though I installed module in virtual environment and it works fine on localhost

I recently got a server through DigitalOcean and I'm playing around with a Flask App.
The app works perfectly fine on my computer when I run it locally. But it doesn't work when hosted on DigitalOcean. Both Virtual Environments are the same.
When I do 'pip freeze', the output is:
coverage==4.0.3
dominate==2.1.16
Flask==0.10.1
Flask-Bootstrap==3.3.5.7
Flask-Login==0.3.2
Flask-SQLAlchemy==2.1
Flask-WTF==0.12
gunicorn==19.4.1
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
SQLAlchemy==1.0.11
visitor==0.1.2
Werkzeug==0.11.3
wheel==0.24.0
WTForms==2.1
But I get an error when I look in Apache error log:
ImportError: No module named flask.ext.bootstrap
I'm not sure why it can't find it. Like I said, it works fine on my computer when I run it locally.
I also tried 'from flask_bootstrap import Bootstrap' instead of 'from flask.ext.bootstrap import Bootstrap'. Both works locally, but neither work on DigitalOcean server.
Any suggestions?
Please check how you configured Apache. It's probably using the system default Python without your virtual env.
When making Flask Apps using digitalocean, this tutorial is a lot more up to date and simpler than the one I posted above:
https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04
I did the process again, but this time using gunicorn and nginx and found the overall process a lot easier.

Port Virtualenv based app error

For the last couple of weeks I have programmed a flask server. I was adviced to use virtualenv to make sure that all my python dependencies are easily tracked. However, now I have deployed it on my test server and it gives me the following error:
from flask import Flask
ImportError: No module named flask
Which is about the first python module that I have installed. Is there something I am doing wrong here? Virtualenv should've taken care for this, right? How can I check for these kind of errors during development?

Can Sentry be run under passenger WSGI (dreamhost)

I am trying to get sentry running on my dreamhost server. Dreamhost uses passenger wsgi to serve python frameworks, like django. I am able to get django apps running.
I am using virtualenv and I install sentry using pip, so all the code for sentry sits under the virtualenv directory. The instructions given for sentry doesn't use the startproject to create a directory that you can place somewhere for the passenger_wsgi.py file to find.
The sentry website gives examples of the program running under Nginx and uWSGI, but I am limited to (in this case) to apache and passenger wsgi.
Is it possible to run sentry under dreamhost's configuration and if so how does one pass things in like the config file to get it working. I have been able to locally start and interact with sentry, using :
sentry --config=/home/user/.sentry/sentry.conf.py start
so I know that all the dependencies are present on the host system
OK it looks like I was over thinking it, I forgot from a python perspective the file wsgi.py (which is found in the sentry directory) is called as sentry.wsgi when imported as a module. I was confused by sentry being a module that was downloaded from pip and how to access it. This is the reduced solution that works:
passenger_wgsi.py
import sys, os
INTERP = "/home/user/.virtualenv/sentry2/bin/python"
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
os.environ['SENTRY_CONF'] = "/home/user/.virtualenv/sentry2/sentry.conf.py"
import sentry.wsgi
believe it or not, that's it. If you look at the wsgi file in the sentry directory under the virtualenv install you will see it does all the importing of the django.core.handlers.wsgi and kicks off the correct application.

Categories