Import python package in the module - python

I am beginner in Python. I am using set python librarys and I want to take the part of my code in .py module. Where should I write "import" of the set of libraries, in the module, or in main file? If I dont write it in the module, program doesn't work.
#mainfile.py
import cv2
import faceResearch
faceResearch.mn()
#faceResearch.py
import cv2
def mn():
image = cv2.imread("Smiling/3--1873301-Smiling woman looking at camera.jpg")
cv2.imshow("im", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#so, in wich file should I write "import cv2"? in mainfile? in file of the module? or in both?

You should keep import cv2 in your module (faceResearch.py ?) not in main.py. With that all others scripts importing your module will automatically import cv2 ; if cv2 is installed. You can check if cv2 is installed, and if not display an error message : link

Related

How to convert a python file in which I import cv2 to a .so file by using cython

I have a python file in which I import cv2.When I do not import cv2,I can use cython to convert the python file to a .so file and I can use it in java by jni.But when I import cv2 and add relative code,the .so file produced by cython can't be use.When I use it in java,it will thrown a error which indicate progress can't import cv2.How to solve this problem?Thanks.
Here is error:
/root/miniconda3/lib/python3.8/site-packages/numpy/__init__.py:142: UserWarning: mkl-service package
failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
OpenCV bindings requires "numpy" package.
Install it via command:
pip install numpy
AttributeError: module '__main__' has no attribute 'JNI_API_testFunction'
error
I use python to run my Test.py file and it work out.It means that my python can import cv2 and numpy correctly.I use this command to convert my Test.py to a .so file:
python Setup.py build_ext --inplace
Here is Setup.py:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
sourcefiles = ['Test.pyx', 'main.c']
extensions = [Extension("Test", sourcefiles,
include_dirs=['/root/java/jdk-18.0.2/include/',
'/root/java/jdk-18.0.2/include/linux/',
'/root/miniconda3/include/python3.8/'],
library_dirs=['/root/miniconda3/lib/'],
libraries=['python3.8']),
]
setup(ext_modules=cythonize(extensions, language_level = 3))
I use System.load() function to load my .so file in java.If I do not import cv2 and numpy and delete code about cv2 and numpy,I can call testFunction().It means that my java code is correct.But if I import cv2 and numpy and I add code about cv2 and numpy,for example,cv2.imread(),I will get a error.
I think it can find cv2 and numpy because it have run numpy's init.py file.I think the problem result from cython because I get athis information in a Chinese community:
The directory level information of the code file will be lost in the file code processed by cython. There is no difference between the code after c.py conversion and the code after M / c.py conversion.In a.py or b.py code, if there is a reference to C Py module, the loss of directory information will cause the two to report an error when executing import M.C, because the corresponding module cannot be found.
the author say the problem can be solved by modifying the source code of cython,but he don't give the method.

ModuleError: No module named pptx found

I am writing a python code for merging ppts. It takes the location of the 2 ppts, merges them and puts the merged ppt formed in the folder given by user. The code used is:
import sys
from pptx import Presentation
#import Aspose.Words.License
#import aspose.slides as a_slides
#import os
#import win32com.client
def merge_powerpoint_ppts(pres_loc1, pres_loc2, output_loc):
p1 = open(pres_loc1)
pres1 = Presentation(p1)
p2 = open(pres_loc2)
pres2 = Presentation(p2)
for slide in pres2.slides:
for lide in pres1.slides:
if slide.shape.title.text == lide.shape.title.text:
pres1.slides.add_Clone(slide)
pres1.save(output_loc)
p1.close()
p2.close()
When I try to debug the code, I get the following:
[Error][1]
[1]: https://i.stack.imgur.com/yID2l.png
I have already installed the module pptx on my system and it is updated, but I am still getting this error.
First if you have a folder named Presentation or pptx change it cause this could happen because of naming confusion of files or folder and python modules
Secondly make sure you use the correct python interpreter or env where you install pptx in in
last Option uninstall pptx and write the following command
conda install -c conda-forge python-pptx

Python: Trouble With SKVideo

I've included this line in a script I'm writing:
from skvideo.io import VideoCapture
My script doesn't like this and gives me back:
ImportError: cannot import name 'VideoCapture'
However, when I go into the REPL for Python 3.6 (what I'm using to run my script), I can import skvideo.io just fine. So the trouble lies with VideoCapture, I'm just not sure why it can't import it...
Any ideas?
Thanks!

python: py2exe paramiko showing no results

I am using python 2.7.11 on Windows 7 64bit, I am facing a problem with py2exe when I import paramiko module, the .exe file runs but does not show any results, after executing it closes immediately knowing that i did not get any error during the conversion.
import paramiko
print raw_input("press enter to exit")
the setup.txt file:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
I have found that in the "dist" folder there are some dll files such as API-MS-Win-Core-ErrorHandling-L1-1-0.dll - API-MS-Win-Core-LibraryLoader-L1-1-0.dll - API-MS-Win-Core-LocalRegistry-L1-1-0.dll, such files I don't usually face them on another machine, so could anyone help please.
I was able to solve this after importing some modules:
import appdirs
import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements
hopefully that could be useful for someone.

Python error when importing image_to_string from tesseract

I recently used tesseract OCR with python and I kept getting an error when I was trying to import image_to_string from tesseract.
Code causing the problem:
# Perform OCR using tesseract-ocr library
from tesseract import image_to_string
image = Image.open('input-NEAREST.tif')
print image_to_string(image)
Error caused by above code:
Traceback (most recent call last):
file "./captcha.py", line 52, in <module>
from tesseract import image_to_string
ImportError: cannot import name image_to_string
I've verified that the tesseract module is installed:
digital_alchemy#roaming-gnome /home $ pydoc modules | grep 'tesseract'
Hdf5StubImagePlugin _tesseract gzip sipconfig
ORBit cairo mako tesseract
I believe that I've grabbed all the required packages but unfortunately I'm just stuck at this point. It appears that the function is not in the module.
Any help greatly appreciated.
Another possibility that seems to have worked for me is to modify pytesseract so that instead of import Image it has from PIL import Image
Code that works in PyCharm after modifying pytesseract:
from pytesseract import image_to_string
from PIL import Image
im = Image.open(r'C:\Users\<user>\Downloads\dashboard-test.jpeg')
print(im)
print(image_to_string(im))
Pytesseract I installed via the package management built into PyCharm
Is your syntax correct for the module you have installed? That image_to_string functions looks like it is from PyTesser per the usage example on this page:
https://code.google.com/p/pytesser/
Your import looks like it is for python-tesseract which has a more complicated usage example listed:
https://code.google.com/p/python-tesseract/
For windows followed below steps
pip3 install pytesseract
pip3 install pillow
Installation of tessaract-ocr is also required
https://github.com/tesseract-ocr/tesseract/wiki
otherwise you will get an error Tessract is not on path
Python code
from PIL import Image
from pytesseract import image_to_string
print ( image_to_string(Image.open('test.tif'),lang='eng') )
what works for me:
after I install the pytesseract form tesseract-ocr-setup-3.05.02-20180621.exe
I add the line
pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"
and use the code form the above this is all the code:
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd="C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe"
im=Image.open("C:\\Users\\<user>\\Desktop\\ro\\capt.png")
print(pytesseract.image_to_string(im,lang='eng'))
I am using windows 10 with PyCharm Community Edition 2018.2.3 x64

Categories