ZXing in python FileNotFoundError Problem - python

I have written a simple Python code to detect qrCode. Code:
import zxing
reader = zxing.BarCodeReader()
barcode = reader.decode('../images/QR_CODE-easy.png')
print(barcode)
Now, when I run it, I get the following error:
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have check this file location is valid by using cv.imread command. Please let me know if someone has a solution to this problem.

You need to install Java Development Kit.
The readme of ZXing Python wrapper says the following:
Dependencies and installation
Use the Python 3 version of pip (usually invoked via pip3) to install: pip3 install zxing
You'll neeed to have a recent java binary somewhere in your path. (Tested with OpenJDK.)
pip will automatically download the relevant JAR files for the Java ZXing libraries (currently v3.4.1)

You appear to be on Windows (as the error code suggests), which uses the backslash for file paths.
It's not good practice as it won't be widely compatible, but if you're in a hurry and know you won't want to use the code on Mac or Linux, you can use double backslashes:
reader.decode('..\\images\\QR_CODE-easy.png')
Otherwise you should use os.path.join or pathlib (assuming your using Python 3)
import os.path
qr_file = os.path.join("..", "images", "QR_CODE-easy.png")
Or
from pathlib import Path
qr_file = Path("../images/QR_CODE-easy.png")
There are further details of a few options here:
https://medium.com/#ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f
Edit: it is also worth confirming that your relative path is indeed correct when starting in your current working directory. You can check the current working directory with: cwd = os.getcwd() . You may want to try an absolute path to your file too, just to confirm whether it works with that first.
More details on cwd here: https://stackoverflow.com/a/5137509/142780

Related

How to unrar files

I am trying to unrar files uploaded by users in django web application. I have gone through different approaches but none of them are working for me.
The below methods work fine in my local OS (Ubuntu 18.04), but n't working in the server.
I am using Python 3.6.
import rarfile - here 'rarfile' is imported from python3.6/site-packages/rarfile. It is giving this error -
bsdtar: Error opening archive: Failed to open '--'
Following the steps suggested here, I installed 'unrar'.
from unrar import rarfile - here it's being imported from python3.6/site-packages/unrar/
But this gave the error - LookupError: Couldn't find path to unrar library.
So to solve the issue, I followed the steps mentioned here, and created UnRaR
executable, and also set the path in my environment. This works fine in local, but unable to understand how to do the same in 'production environment'.
import patoolib - importing from patool
I also tried implementing patool, which worked fine in local but not in production and I am getting this error - patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),
This has become a big blocker in my application, can anyone please explain how to solve the problem.
Code snippet for rarfile -
import rarfile
.....
rf = rarfile.RarFile(file_path)
rf.extractall(extract_to_path)
Code snippet for patool -
import patoolib
...
patoolib.extract_archive(file_path, outdir=extract_to_path)
Patool is the good python library it is simple and easy. A simple example of it is here:
pip install patool
Use pip install patool to install the library
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
Now, in your case the problem is probably that you don't have your unraring tool in the path, indicating it can not be called from the command line (which is exactly what patool does). Just put your rar file folder to the path and you're fine or give the absolute path to the rar file.

Transferring Django from windows to ubuntu

I have written a project in windows with Django, It works well in windows, I wanted to transfer it to Ubuntu, first I installed the python3 and then made a virtualenv,
activated it and installed django and required packages, I got some errors but the main irritating one is that, I have a INI file, I use the configobj to read it, it works well in windows but in Ubuntu it give 'Key error' here is my code:
from configobj import ConfigObj
ConfigPath = os.path.join(settings.MEDIA_ROOT, 'Vars.ini')
ConfigParser = ConfigObj(ConfigPath)
print(ConfigParser['FileNames']['MotionName'])
and here is the file:
[FileNames]
MotionName = test1
I also tried to use only lowercase words but still no chance.
I can't believe it, my file was Vars.INI, windows would recognize it since it would take INI as extension but Ubuntu was looking for Vars.ini and it couldn't find it so I change the name from Vars.INI to Vars.ini.

Langdetect Issue With pyinstaller

I'm trying to compile my code in oython that uses the library langdetect into a .app with pyinstaller.
as answered in this question : https://github.com/pyinstaller/pyinstaller/issues/2680
I modified the .spec file to include this :
a = Analysis(
# your other stuff here...
datas=[
('langdetect/utils', 'langdetect/utils'), # for messages.properties file
('langdetect/profiles', 'langdetect/profiles'), # don't forget if you load langdetect as a submodule of your app, change the second string to the relative path from your parent module. The first argument is the relative path inside the pyinstaller bundle.
]
# the rest of your analysis spec...
)
But pyinstaller stil does not find utils. I think that I didn't enter the right path in the tuple, but I don't know what I'm supposed to do (the program is supposed to already 'be' in the libraries directory, so shouldn't it work in theory ?)
I used anaconda anaconda to install langdetect. The problem is that I'm not using my laptop to build the .app file, so I would refer if it's a generic path rather than specific.
thanks !
I had the same issue. The issue was with the langdetect library. I used textblob library instead and it worked perfectly.

Not managing to extract RAR archive using rarfile module

I have been trying to make a script that extracts *.rar files for but am receiving errors. I've been struggling to understand the documentation of the module to no avail (I'm new to programming so sometimes get a bit lost in all the docs).
Here is the relevant part of my code, and the error received.
Snippet from my code:
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.rarfile.extract_all()
unrar(rarpath)
Error received:
File "unrarer.py", line 26, in unrar
rf.rarfile.extract_all()
AttributeError: 'str' object has no attribute 'extract_all'
I have installed rarfile2.8 and unrar0.3 using pip (note sure if the later was necessary).
Thanks in advance for any assistance correcting my function or helping understand the package's documentation.
Support for RAR files in general is quite poor, this experience is par for the course.
In order to get the rarfile Python module to work, you have to also install a supported tool for extracting RAR files. Your only two choices are bsdtar or unrar. Do not install these with Pip, you have to install these with your Linux package manager (or install them yourself, if you think that the computer's time is more valuable than your time). For example on Debian-based systems (this includes Ubuntu) run,
sudo apt install bsdtar
Or,
sudo apt install unrar
Note that bsdtar does not have the same level of support for RAR files that Unrar does. Some newer RAR files will not extract with bsdtar.
Then your code should look something like this:
import rarfile
def unrar(file):
rf = rarfile.RarFile(file)
rf.extract_all()
unrar('/home/maze/Desktop/test.rar')
Note the use of rf.extract_all(), not rf.rarfile.extract_all().
If you are just doing extract_all then there is no need to use a rarfile module, though. You can just use the subprocess module:
import subprocess
path = '/path/to/archive.rar'
subprocess.check_call(['unrar', 'x', path])
The rarfile module is basically nothing more than a wrapper around subprocess anyway.
Of course, if you have a choice in the matter, I recommend migrating your archives to a more portable and better supported archive format.
if you are in windows, it worked for me. You need to go to https://www.rarlab.com/rar_add.htm download UnRAR for Windows - Command line freeware Windows UnRAR, execute it, extract it to a folder and add the executable path in your code after importing rarfile:
rarfile.UNRAR_TOOL = r"C:\FilePath\UnRAR.exe"
rf.rarfile is the name of the file which you can see by printing its value. Remove that and check out help(rarfile.RarFile) for the method you want.
import rarfile
rarpath='/home/maze/Desktop/test.rar'
def unrar(file):
rf=rarfile.RarFile(file)
rf.extractall()
unrar(rarpath)
try this
import fnmatch
from rarfile import RarFile
path = r'C:\Users\byqpz\Desktop\movies\rars'
destinationPath = r'C:\Users\byqpz\Desktop\movies\destination'
for root, dirs, files in os.walk(path):
for filename in fnmatch.filter(files, '*.rar'):
fullPath = os.path.join(root, filename)
RarFile(fullPath).extract(destinationPath)

Getting Python to use the ActiveTcl libraries

Is there any way to get Python to use my ActiveTcl installation instead of having to copy the ActiveTcl libraries into the Python/tcl directory?
Not familiar with ActiveTcl, but in general here is how to get a package/module to be loaded when that name already exists in the standard library:
import sys
dir_name="/usr/lib/mydir"
sys.path.insert(0,dir_name)
Substitute the value for dir_name with the path to the directory containing your package/module, and run the above code before anything is imported. This is often done through a 'sitecustomize.py' file so that it will take effect as soon as the interpreter starts up so you won't need to worry about import ordering.

Categories