Celery 4.2.1 (windowlicker) not throwing exception in Linux - python

In local environment [windows], celery seems to be working fine when the following is executed:
try:
from .redis_repo import redis_store
except ImportError:
from redis_repo import redis_store
This is point of entry for my celery task, in local environment its working because try catch seems to working fine. But in linux environment it doesn't .
Any ideas on this?

Related

Module not found error when running script as wsgi within Apache

I have a developed Server application using pywps on a Flask server and now im trying to migrate it to an apache server.
Tech-stack :
OS: Windows Server
Python 3.9.6
Apache 2.4.48 (Win64)
mod-wsgi 4.8.0
I can open the configured URL but receive a status code 500. Error log says the following:
mod_wsgi (pid=7212, process='', application='127.0.0.1:8008|/wps'): Loading Python script file 'C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi'.
mod_wsgi (pid=7212): Failed to exec Python script file 'C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi'.
mod_wsgi (pid=7212): Exception occurred processing WSGI script 'C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi'.
Traceback (most recent call last):\r
File "C:/Apache24/wps_env/pywps-flask-master/pywps.wsgi", line 9, in <module>\r
import pywps\r
ModuleNotFoundError: No module named 'pywps'\r
The wsgi file in question is :
from pathlib import Path
import pywps
from pywps import Service
from processes.sayhello import SayHello
from processes.csv_input import CSVInputs
processes = [
SayHello(),
CSVInputs()
]
application = Service(processes, [Path(r'C:\Users\Jdoe\wps_env\pywps-flask-master\pywps.cfg')])
Now comes the strange thing, i am able to execute this exakt same script from the powershell with no errors at all.
I would rule out, path or enviroment related issues, because i have installed all pip packages which i used in the virtaul enviroemnt i have to develop in the global namespace aswell, so both interpreters know the same packages. I know this is not best practice, but i'm currently working in a sandbox anyway and this more a POC then anything else.
Since the wsgi application tries to run, i also assume that my apache conf is correct.
What am I missing here?
Thank you for your help
ModuleNotFoundError means that a module is missing and that you have to install it. In your case its PyWPS.
So open a terminal and type
pip install pywps
or
pip3 install pywps
then if you rerun your code it should work.
P.S. You can find the package on PyPI

No application is registered as handling this file in bundled Python program

I am using Ubuntu 21.04 with Python 3.8, Matplotlitb 3.4.3, pyinstaller 4.5.1 and I would like python program to open google.com page in default web browser. There is also matplotlib package (the reason why it's not working, without matplotlib everything is fine).
#!/usr/bin/python3
import webbrowser
import matplotlib.pyplot as plt
plt.plot([1, 2, 4, 8, 16])
plt.show()
webbrowser.open("https://www.google.com/")
When I run this program as ./main.py or python3 main.py, everything is ok and Google Chrome is opened. However when I bundle program into executable using pyinstaller:
pyinstaller --onefile main.py
And run this bundled program as ./dist/main, following error appears in terminal:
gio https://www.google.com/: No application is registered as handling this file
When I define BROWSER variable before running file, then it work again:
export BROWSER=$(which google-chrome) && ./dist/main
However, I would like to run program without declaring that variable. Default browser doesn't have to be Chrome and even OS doesn't have to be Linux, but eg. Windows. I found question with similar problem. But when I downgrade matplotlib from 3.4.3 to 3.0.3 is in the link, there is a lot of compatibility errors. So I would like to keep newest matplotlib.
Is there any way fix the gio error?
Seems like the problem is caused by various environment variables being messed up. More info here: https://github.com/pyinstaller/pyinstaller/issues/3668
As a workaround you might be able to use subprocess to open the https URL with its default handler, which should be the default browser:
import os
import platform
import subprocess
env = dict(os.environ)
lp_key = 'LD_LIBRARY_PATH'
lp_orig = env.get(lp_key + '_ORIG')
if lp_orig is not None:
env[lp_key] = lp_orig
else:
lp = env.get(lp_key)
if lp is not None:
env.pop(lp_key)
opener = {'Darwin': 'open', 'Windows': 'start'}.get(platform.system(), 'xdg-open')
subprocess.run((opener, 'https://www.google.com/'), env=env)

How to import personal modules to airflow

I tried to Add the S3ToRedShiftTransfer2 using the standard way
from .modules.S3ToRedShiftTransfer2 import S3ToRedShiftTransferC
I got :
×Broken DAG: [/c/AIRFLOW/dags/my_my_my_my_airflow.py] attempted relative import with no known parent package
Also tried to do under my Ubuntu subsystem where airflow webserver is running:
export $AIRFLOW_HOME=/c/AIRFLOW
export $PYTHONPATH = ${PYTHONPATH}:${AIRFLOW_HOME}
I Always got the same Error.

importing paramiko and mysql.connector packages not working when ran as cron job

Importing packages that are subsequently installed (not present by default in python distribution for rhel 7.6) not working when ran as cron job
Hi Team,
I have a python(2.7) script which imports paramiko package. The script can import the paramiko package successfully when ran as a user(root or ftpuser) after logging in but it cannot import it when ran from cron job. I have tried out various options as provided in the brilliant stack overflow pages like the below but unfortunately couldn't resolve the issue.
1) Crontab not running my python script
I have provided the path to the paramiko package and verified it is successfully received at the script end by logging it when run as cron job and also I have given chmod -R 777 permission to the paramiko folder in the /opt/rh/python27/root/usr/lib/python2.7/site-packages location. Still the import is not functioning when ran as cron job
I have created a shell script and tried to invoke python script from with in the script and configured the shell script in cron job but it seems python script was not invoked
I have verified that there is only one python installation present in the server and so I' am using the correct path
I have disabled selinux option and tried after rebooting but issue still persists
Please note the issue exists not for just paramiko package but for other packages as well that was installed subsequently like mysql.connector e t c
Update1
It has to be something to do with the way I install the paramiko package because the script can even import other packages in the same path as that of paramiko and the permissions for both of them look identical only difference is former comes with the python distribution that is deployed from using the url https://access.redhat.com/solutions/1519803. Cannot figure out what is wrong with the installation steps as I install it as root after doing sudo su and then setting umask to 0022. I do pip install of parmiko and python-crontab as mentioned in their sites and both have the same issue
Another interesting thing is though I have the code to log exception around the failing import statement it never logs the exception but script seems to halt/hang at the import statment
Please help to resolve this issue...
PYTHON CODE
#!/usr/bin/env python
import sys
import logging
import os
def InitLog():
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s',
filename=os.path.dirname(os.path.abspath(__file__)) + '/test_paramiko.log',
filemode='a'
)
logging.info('***************start logging****************')
InitLog()
logging.info('before import')
logging.info(sys.path)
try:
sys.path.append("/opt/rh/python27/root/usr/lib/python2.7/site-packages")
logging.info("sys path appended before import")
import paramiko
except ImportError:
logging.ERROR("Exception occured druing import")
logging.info('after import')
CRONTAB Entry
SHELL=/bin/bash
PATH=/opt/rh/python27/root/usr/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ftpuser/.local/bin:/home/ftpuser/bin
PYTHONPATH=/opt/rh/python27/root/usr/lib64/python27.zip:/opt/rh/python27/root/usr/lib64/python2.7:/opt/rh/python27/root/usr/lib64/python2.7/plat-linux2:/opt/rh/python27/root/usr/lib64/python2.7/lib-tk:/opt/rh/python27/root/usr/lib64/python2.7/lib-old:/opt/rh/python27/root/usr/lib64/python2.7/lib-dynload:/opt/rh/python27/root/usr/lib64/python2.7/site-packages:/opt/rh/python27/root/usr/lib/python2.7/site-packages
*/1 * * * * /opt/rh/python27/root/usr/bin/python /home/ftpuser/Ganesh/test_paramiko.py
#*/1 * * * * /home/ftpuser/Ganesh/test_cron.sh >> /home/ftpuser/Ganesh/tes_cron.txt 2>&1
#*/1 * * * * /home/ftpuser/Ganesh/test_cron.sh
Shell script
#!/opt/rh/python27/root/usr/bin/python
export PATH=$PATH:/opt/rh/python27/root/usr/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ftpuser/.local/bin:/home/ftpuser/bin
export PYTHONPATH=$PYTHONPATH:/opt/rh/python27/root/usr/lib64/python27.zip:/opt/rh/python27/root/usr/lib64/python2.7:/opt/rh/python27/root/usr/lib64/python2.7/plat-linux2:/opt/rh/python27/root/usr/lib64/python2.7/lib-tk:/opt/rh/python27/root/usr/lib64/python2.7/lib-old:/opt/rh/python27/root/usr/lib64/python2.7/lib-dynload:/opt/rh/python27/root/usr/lib64/python2.7/site-packages:/opt/rh/python27/root/usr/lib/python2.7/site-packages
python /home/ftpuser/Ganesh/test_paramiko.py
Expected result from my python script is to log the "after import" string
But currently it is printing only till "sys path appended before import" which also shows the normal python packages are getting imported successfully
This seems to be working now after adding one more environment variable to crontab as below
LD_LIBRARY_PATH=/opt/rh/python27/root/usr/lib64

ImportError when using web.py with new environment

I just recreated all my python environment, reinstalled python and setuptools, and installed virtualenv.
I started a test enviroment with virtualenv --no-site-packages test, activated it with Scripts\activate.bat and then easy_install web.py.
Then I create a code.py file:
import web
urls = (
'/.*', 'index',
)
app = web.application(urls, globals())
class index:
def GET(self):
return 'ok'
if __name__ == "__main__": app.run()
And I get the following error:
File "...\code.py", line 1, in <module>
import web
ImportError: No module named web
But if I use the interactive shell it works:
>>> import web
>>>
Everything done in the same cmd with the enviroment activated.
Does anyone know what is going on?
Edit:
It happens for every package installed within the environment. First it was web.py, now BeautifulSoup (same issue, cant find module, but import works in python shell)
Edit2:
The activate script is not setting the new python executable and the pythonpath print sys.executable gives C:\Python27\python.exe.
Solved.
Windows was configured to open .py files with C:\Python27\python.exe. I can even remember setting this mannualy some time ago so I wouldn't have to use python to run files (oh lazyness, what have you done to me?).
That's why it was working with the interactive shell, but not by executing the code.py file.
Running the file using python code.py works perfectly.

Categories