Python Modules Import www-data - python

perhaps my question is very simple, but I was not able to find a solution for my problem.
Presumably I have to level up my skills in searching. ;-)
I want to use a notificationservice (webservice: zmeventnotification) for my IP-cameras. This service needs opencv for objectdetection with yolo. Therefore I compiled opencv from source on a Ubuntu 20.04 LTS machine. The installation path for opencv was /usr/local/lib/.
After the installation a folder named "cv2" was created in /usr/local/lib/python3.8/site-packages/.
But unfortunately I have problems using this module and very poor python skills.
When I start python3 on the commandline as default user:
import cv2 -> works fine
print(cv2.__version__) -> following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'cv2' has no attribute '__version__'
When I start python3 as root, everything works fine:
import cv2 -> works fine
print(cv2.__version__) -> works fine
4.6.0-dev
As user www-data (sudo -u www-data python3) I'm even not able to import the module:
import cv2 -> following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
When I use the following lines everything works well (even for www-data).
import sys
sys.path.append('/usr/local/lib/python3.8/site-packages')
import cv2
print(cv2.__version__)
4.6.0-dev
So I tried to attach the line
export PYTHONPATH=${PYTHONPATH}:/usr/local/lib/python3.8/site-packages to /etc/profile.
This works for the default user but not for www-data.
So what can I do to give www-data access to the module without editing the source code of the notification service.
Any help would be greatly appreciated.
Thanks in advance
Fritz

Related

Python script using PIL works in IDE doesn't when run by cmd and gives 'PIL' has no attribute 'Image'

When I run the following in the Spyder IDE on Win10:
import PIL
import os
Inboundfile = "C:/Test/test/Overlay.png"
image = PIL.Image.open(os.path.abspath(Inboundfile))
image.show()
It works fine. Opens the picture and outputs:
runfile('C:/Users/bob/Anaconda3/envs/NewEnvironment/bob/test3.py', wdir='C:/Users/bob/Anaconda3/envs/NewEnvironment/bob')
But when I call the same script from a cmd prompt:
C:\Users\bob\Anaconda3\envs\NewEnvironment\bob>python test3.py
It results in:
Traceback (most recent call last):
File "test3.py", line 14, in <module>
image = PIL.Image.open(os.path.abspath(Inboundfile))
AttributeError: module 'PIL' has no attribute 'Image'
I have verified that the environment path is pointing to the same python environment as the IDE. ( I think ) Here is my Path variable from OS
C:\Users\bob\Anaconda3\envs\NewEnvironment\
It's probably something very simple but I'm very new at this and any help would be appreciated.
I tried adding the line:
from PIL import Image
It still runs fine in the IDE but the error message changes to:
C:\Users\bb\Anaconda3\envs\NewEnvironment\bob>python test3.py
Traceback (most recent call last):
File "test3.py", line 11, in <module>
from PIL import Image
File "C:\Users\bob\Anaconda3\envs\NewEnvironment\lib\site-packages\PIL\Image.py", line 69, in <module>
from . import _imaging as core
ImportError: DLL load failed: The specified module could not be found.
This is even more confusing as I can follow that path and see Image.py sitting in the directory. What is the IDE doing that CMD is not?
All of my problems vanished when I simply closed and restarted the console I was using to call the python script. I have learned that the console saves things like the path variables when it starts up and must be restarted to get changes.

RaspberryPi: ImportError: No module named cv2 in Geany

I installed opencv in RaspberryPi (using this link)and import in termial and python3 (idle). It shows no error in these two as shown in below images.
But, when I tried to run import cv2 in Geany, it shows error given below:
Traceback (most recent call last):
File "import.py", line 1, in <module>
import cv2
ImportError: No module named cv2
How I can solve this problem?
I solved this problem by changing python to python3 in Geany.
For this, Go to Build->Set Build Commands in Geany menu. In Set Build Commandswindow, under Python commands label in compile line python m py_compile will be there. Similarly, under Executive commands label in executive line python "%f" will be there. I changed python m py_compile into python3 m py_compile and python "%f" into python3 "%f" (as shown in images 1 and 2) and above-mentioned problem get solved.
Image 1.
Image2.
Thanks.

ImportError with different python installations.

I am getting below ImportError while running a python file.
ImportError: No module named osgeo.gdal_array
!/usr/bin/env python
from osgeo.gdal_array import BandReadAsArray
However, if i try to import same from command line, it runs fine.
$ which python
/home/hduser/anaconda2/bin/python
$ python
>>> from osgeo.gdal_array import BandReadAsArray
>>>
Also, please see the below where i am getting the same ImportError.
$ /usr/local/bin/python2.7
>>> from osgeo.gdal_array import BandReadAsArray
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named osgeo.gdal_array
I figured out that there is something going on between different versions of python. But, i do not want to change the original source code.
How do i make my program run without changing anything within the code of calling python installed in anaconda explicitly?

Error in running my Python Script

I just updated my OS from Snow Leopard to LION 10.7.3, and I usually run this python script using IDLE. However, when I try running it, I get this error..
Traceback (most recent call last):
File "/Users/Brian/Desktop/BitmapBuilder.py", line 1, in
import os, shutil, Image, brianutils, sys
ImportError: No module named Image
Any help would be much appreciated, thanks in advance!
It is saying that Image is not a module. Either it doesn't exist or it isn't in Python's path or the directory in which your script is.

No module named _core when using mailer.py on Windows/Python 2.7

I'm trying to configure and run SVN post-commit hook sending mails. I've downloaded class mailer.py, installed Python 2.7 and svn-win32 bindings for svn. The machine is Windows-7 64 bit, the Python is 32 bit. Now the mailer.py ends with error, which is caused by import problem.
When I in python console type "import svn.core" I have following error:
>>> import svn.core
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\tools\Python27\lib\site-packages\svn\core.py", line 19, in <module>
from libsvn.core import *
File "c:\tools\Python27\lib\site-packages\libsvn\core.py", line 5, in <module>
import _core
ImportError: No module named _core
while in directory site-packages/libsvn are files such as: _core.dll
I've installed other bindings, pysvn, that was installed correctly, but as far as I've noticed, it's the totally other API so I can't use that for python.py
Does someone had similar problem and knows how to deal with it?
The Python bindings need to load the native Subversion libraries (DLL's). If your Python is 32-bit then you would need 32-bit versions of the native Subversion libraries on PATH.
I have problem like this. Trouble was that python just can not import this library (svn.core and other).
I just make:
import sys
sys.path.append("C:\csvn\lib\svn-python").
My file core.pyc was in C:\csvn\lib\svn-python\svn. Hope it helps somebody. Such behacior for me is strange because there is no "init.py" or "init.pyc" file in svn-python directory. But it works.

Categories