pdf2image on windows keeps producing error - python

I am trying to write some code to extract text from pdf and followed the directions on pypi to install pdf2image and poppler on windows. I also changed Path. Well, the suggested command on python does not work, it keeps producing the following error:
== RESTART: C:\Users\Elisabeth\AppData\Local\Programs\Python\Python39\test.py ==
Traceback (most recent call last):
File "C:\Users\Elisabeth\AppData\Local\Programs\Python\Python39\lib\site-packages\pdf2image\pdf2image.py", line 441, in pdfinfo_from_path
proc = Popen(command, env=env, stdout=PIPE, stderr=PIPE)
File "C:\Users\Elisabeth\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\Elisabeth\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] Das System kann die angegebene Datei nicht finden
The code I used:
from pdf2image import convert_from_path
from pdf2image.exceptions import (
PDFInfoNotInstalledError,
PDFPageCountError,
PDFSyntaxError
)
images = convert_from_path(r'C:\Users\Elisabeth\Documents\Anleitungen\C:/Users/Elisabeth/Documents/Anleitungen/t490s_x390_ug_de.pdf')
What I did to solve the problem, but did not work:
uninstalling poppler and pdf2path and installing it again. I also used different versions of poppler.
I am just stuck, is there the possibility for some help?
Thank`s
Elaisa

I solved the problem myself - since I am a beginner at Python it took a while. I followed the mentioned instructions to install Poppler on Windows. I tried out different things: What did not work was changing path, even after restarting the system. I tried to install different versions of Poppler, that did not help either. So I tried this: [How to install Poppler on Windows?][1]link. That worked very well, you just have to really watch out where the folder "bin" within the Poppler folder is. This seems to differ in the different versions of Poppler.

Related

Run Powershell script from Python?

At the end of some processing in Python in Windows I am trying to eject an USB SD card.
Researching here it seems there are two ways in which to do it; call a PowerShell program or run PowerShell within Python.
Can anyone offer me any guidance. Please keep it simple; learning Python is my new year project.
So I have written a PowerShell script (ejectusb.ps1) which works perfectly:
$drive = New-Object -ComObject Shell.Application
$drive.Namespace(17).Parsename("J:").InvokeVerb("Eject")
I then call it from Python using subprocess:
subprocess.run(["E:\Data\Computing\Software\MicroSoft\Programming\Powershell\ejectusb.ps1"])
The SD card is not ejected and I get the error messages:
Traceback (most recent call last):
File "E:/Data/Computing/Software/Scripts/SdCardPlayEachVideo06.py", line 91, in <module>
subprocess.run(["E:\Data\Computing\Software\MicroSoft\Programming\Powershell\ejectusb.ps1"])
File "C:\Users\David\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 548, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Users\David\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1024, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\David\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1493, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
OSError: [WinError 193] %1 is not a valid Win32 application
I don't understand this error message.
So I tried running PowerShell inside Python using:
os.system('powershell $driveEject = New-Object -comObject Shell.Application;
$driveEject.Namespace(17).ParseName("J:").InvokeVerb("Eject")')
An empty PowerShell screen and also what looks like a Windows command screen briefly flash up, but the SD card is not ejected. No error messages.
Can anyone offer me any guidance. Please keep it simple; learning Python is my new year project.
Hamed's solution uses the argument 'powershell' to launch PowerShell. i.e
subprocess.run(["powershell", "-File", "E:\Data\Computing\Software\MicroSoft\Programming\Powershell\ejectusb.ps1"])
I have been using the full path to PowerShell as the argument, i.e
subprocess.run(["C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", etc
This path is correct (if I type it into the address bar of Windows File Explorer it launches PowerShell. But is causes a 'file not found' error.
So I dont know what the problem is with the full path but I am grateful for Hamed's workaround.

Python: FileNotFoundError: [WinError 2] The system cannot find the file specified

I have a little experience with python, and I am currently attempting to try some machine learning video generation via pix2pix through this guide.
I am currently at the part where I need to extract frames from the video. I am using the same video, but moved it into the same directory as extract_frames.py for convenience.
I keep getting a WinError 2: the system cannot find the file specified:
C:\Users\cadou\OneDrive\Desktop\ml\pix2pixHD>python extract_frames.py -video fire.mp4 -name fire_dataset -p2pdir . -width 1280 -height 736
creating the dataset structure
ffmpeg -v 16 -i C:\Users\cadou\OneDrive\Desktop\ml\pix2pixHD\fire.mp4 -q:v 2 -vf "scale=iw*736/ih:736, crop=1280:736" C:\Users\cadou\OneDrive\Desktop\ml\pix2pixHD/datasets/fire_dataset/train_frames/frame-%06d.jpg -hide_banner
extracting the frames
Traceback (most recent call last):
File "C:\Users\cadou\OneDrive\Desktop\ml\pix2pixHD\extract_frames.py", line 32, in <module>
video_utils.extract_frames_from_video(
File "C:\Users\cadou\OneDrive\Desktop\ml\pix2pixHD\video_utils.py", line 82, in extract_frames_from_video
p = subprocess.Popen(shlex.split(command), shell=False)
File "C:\Users\cadou\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\cadou\AppData\Local\Programs\Python\Python39\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
I have looked up this error and tried a few things, but none of them seem to fix my issue. I suspect it has something to do with the Path, but I have tried changing that too. I installed all the libraries requested from the tutorial as well (dominate, ffmpeg, and pyTorch).
You can try the following command with replacing "fire.mp4" to "./fire.mp4".
python extract_frames.py -video ./fire.mp4 -name fire_dataset -p2pdir . -width 1280 -height 736
Erorr doesn't show what file it can't find.
Maybe it can't find fire.mp4 but I expect it needs ffmpeg to extract video.
I checked source code for subprocess in video_utils.py and it runs ffmpeg - so you needs to install it from https://ffmpeg.org/ .
It may need also to change video_utils.py and use C:\full\path\to\ffmpeg.exe.
There is also hardcoded path to Ubuntu font (in two places: see code) which shows that this code was created for Linux Ubuntu. This path also will need changes to other font on Windows.

OSError: [WinError 193] %1 is not a valid Win32 application error using GeckoDriver and Firefox through Selenium and Python on Windows

I created script with selenium in Ubuntu and works just fine there, but when I moved it to windows10, I get lots of error and I tried to fix it one by one until I see this error. I've been looking for the solution to this problem but I am unable to resolve this error.
Traceback (most recent call last):
File "D:/Users/b/Documents/Python/Bolt/GUI.py", line 180, in start
driver = l.start_chime() # start chime
File "D:\Users\b\Documents\Python\Bolt\Login.py", line 87, in start_chime
self.chime_driver = webdriver.Firefox(executable_path=self.PATH)
File "D:\Users\b\Documents\Python\Python3.8\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "D:\Users\b\Documents\Python\Python3.8\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "D:\Users\b\Documents\Python\Python3.8\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\Users\b\Documents\Python\Python3.8\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
File "C:\Program Files\JetBrains\PyCharm 2020.1.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_monkey.py", line 551, in new_CreateProcess
return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
OSError: [WinError 193] %1 is not a valid Win32 application
This is happen when I tried to open webdriver using selenium.
self.myday_driver = webdriver.Firefox(executable_path=self.PATH)
and is there any method to move script from Ubunto to Windows without getting error?
I will try to help you answer your last question:
and is there any method to move script from Ubunto to Windows without
getting error?
Yes, have you heard about docker? https://www.docker.com/
Essentially, docker will create isolated environments that will run in every machine that has docker installed. These environments are configurable inside a dockerfile, basically, you need to follow these steps:
Install docker on both machines. I have used on Windows and RH to automate all this process and minimize erros.
Create a docker file, the structure will be something like:
FROM ubuntu:18.04
COPY . /app
RUN make /app
CMD python /app/app.py
So it will create an environment based on a ubuntu image
copy all files in your current directory (.) to /app (remember this will be a ubuntu image, so you have a standard folder structure with /etc /home, etc.)
run command make (in your case could be install some dependency using pip)
run python command.
You also can find python images ready for usage, so instead of ubuntu:latest you could get a image linux with python installed and then you just install your dependencies.
This is a great tool for a developer, I recommend looking into it, read documentation to understand concepts and it will ease your life.
Hope it helps.
This error message...
OSError: [WinError 193] %1 is not a valid Win32 application
...implies that the underlying OS doesn't recognizes %1 i.e. the system variable PATH as a valid Win32 application i.e. a executable binary.
To initiate a Selenium driven GeckoDriver controlled Firefox session you need to:
Download the latest version of GeckoDriver binary version, place it in your system.
Next in your code block you need to mention the absolute path of the binary through the Key executable_path as follows:
from selenium import webdriver
self.myday_driver = webdriver.Firefox(executable_path=r'C:\path\to\geckodriver.exe')

The system cannot find the file specified with ffmpeg

In the process of using the ffmpeg module to edit video files i used the subprocess module
The code is as follows:
#trim bit
import subprocess
import os
seconds = "4"
mypath=os.path.abspath('trial.mp4')
subprocess.call(['ffmpeg', '-i',mypath, '-ss', seconds, 'trimmed.mp4'])
Error message:
Traceback (most recent call last):
File "C:\moviepy-master\resizer.py", line 29, in <module>
subprocess.call(['ffmpeg', '-i',mypath, '-ss', seconds, 'trimmed.mp4'])
File "C:\Python27\lib\subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
After looking up similar problems i understood that the module is unable to pick the video file because it needs its path, so i took the absolute path. But in spite of that the error still shows up.
The module where this code was saved and the video file trial.mp4 are in the same folder.
The WindowsError you see does not refer to the video file but to the ffmpeg executable itself. The call to subprocess.call has no idea that trimmed.mp4 is a filename you are passing. Windows knows that the first parameter ought to be an executable file and reports back to the interpreter that it can't find it.
Double-check that ffmpeg can be executed in the environment your interpreter is running in. You may either add it to your PATH or specify the full path to ffmpeg.exe.
None of the above answers worked for me.
I got it working, by opening Anaconda Navigator > CMD prompt.
conda install ffmpeg
This answer is directed at Windows users of the ffmpeg-python library since this question is the first search result for a stricter instance of the same issue.
Adding on to user2722968's answer because neither of the existing answers solved the problem for me.
As of this post, ffmpeg-python uses subprocess.Popen to run ffmpeg. Per this issue, subprocess does not look in Path when resolving names, so even if FFmpeg is installed and in your Path and you can run it from CMD/PowerShell, ffmpeg-python may not be able to use it.
My solution was to copy ffmpeg.exe into the Python environment where python.exe is. This workaround seems far from ideal but it seems to have solved the problem for me.
Most of the answers didn't work. Here is what worked for me using conda env:
pip uninstall ffmpeg-python
conda install ffmpeg
pip install ffmpeg-python
Just the conda installation gives library not found error. Didn't try uninstalling conda library either but this works.
I had the same issue!
And I FOUND A SOLUTION.
I realised, viewing all these answers that I need to install ffmpeg. I tried 'pip install ffmpeg' but that didn't work. What worked was copying the ffmpeg.exe file to the folder that python.exe is in.
Thats what someone up here mentioned. Thank you!
To download the ffmpeg.exe file, download the zip file from https://github.com/GyanD/codexffmpeg/releases/tag/2022-06-06-git-73302aa193
First Uninstall all pip libraries
pip uninstall ffmpeg
pip uninstall ffmpeg-python
Then install using conda
conda install ffmpeg
If you are going to use 'ffmpeg.exe' or its linux binary, you need this
conda install ffmpeg
Then you will call it via subprocess or os.system() in your code.
And make sure you will run a different thread to run ffmpeg code otherwise there will be a huge bottleneck in your main thread.
class SaveAll():
def init(self):
super(SaveAll, self).__init__()
def save(self):
try:
command = "ffmpeg -i {} -c copy -preset ultrafast {}"format(...)
os.system(command)
except ffmpeg.Error as e:
print (e.stout, file=sys.stderr)
print (e.stderr, file=sys.stderr)
def run(self):
self.thread= threading.Thread(target=self.save)
self.thread.start()
self.thread.join(1)
...
saver = SaveAll()
saver.start()

How to python subprocess.check_output linux command on mac os

I am trying to run a linux executable on Max OS X 10.11.6 via python2.7
I would like to use subprocess.check_output.
The command, which works via the terminal is:
mosel -c "exec PATH/TO/SCRIPT arg1='value1', arg2='value2'"
However, when I try:
subprocess.check_output(['mosel','-c',cmd])
where
cmd="exec PATH/TO/SCRIPT arg1='value1', arg2='value2'"'
I get:
File "/usr/local/lib/python2.7/site-packages/subprocess32.py", line 629, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/local/lib/python2.7/site-packages/subprocess32.py", line 825, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python2.7/site-packages/subprocess32.py", line 1574, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 2] No such file or directory: 'mosel'
I have been able to get it to "echo" the command to an output file, but I cannot run "which mosel" via python, which leads me to believe that it has to do with check_output using "bin/sh"as the executable.
So, do I need to use "Popen" instead and set
executable=path/to/mosel
?
If so, how do use Python to get the user's path to mosel (i.e. get the output of "which mosel")?
Thanks!
UPDATE:
PyCharm was not seeing the system paths, which I fixed using this answer:
Setting environment variables in OS X?
Now, it appears that
subprocess.check_output(['mosel','-c',cmd])
Is sending the square brackets to the command line, because it now returns:
dyld: Library not loaded: libxprm_mc.dylib
Referenced from: /usr/local/opt/xpress/bin/mosel
Reason: image not found
Traceback (most recent call last):
File "/Users/nlaws/projects/sunlamp/sunlamp-ss/RunScenarios/run.py", line 70, in <module>
run(1)
File "/Users/nlaws/projects/sunlamp/sunlamp-ss/RunScenarios/run.py", line 44, in run
out = check_output(['mosel', '-c', cmd])
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 219, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '['mosel', '-c', cmd]' returned non-zero exit status -5
Or is there still a path issue?! (I can run mosel -c cmd via the mac terminal, but not in pycharm via python, nor in the mac terminal via python).
The problem is you're using check_output's arguments incorrectly. Unless you pass it shell=True, check_output expects a list of parameters as its input, in the form:
check_call(['binary_name','arg1','arg2','arg3', ....])
So in this case, you should do:
subprocess.check_call(['mosel', '-c', "exec PATH/TO/SCRIPT arg1='value1', arg2='value2'"])
The root of the issue turns out to be the DYLD_LIBRARY_PATH:
The new OS X release 10.11 "El Capitan" has a "security" feature that
prevents passing DYLD_LIBRARY_PATH to child processes. Somehow, that
variable is stripped from the environment. - (quoted from https://www.postgresql.org/message-id/20151103113612.GW11897#awork2.anarazel.de)
The security feature is called SIP or "System Integrity Protection". Unfortunately, it seems that no one has come up with a solution to this issue (other than work-arounds that must be tailored to each situation).
Here is another example of this issue:
https://github.com/soumith/cudnn.torch/issues/111
Google "mac os inherit dyld_library_path" and you will find many other examples.

Categories

Resources