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.
Related
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.
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.
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')
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.
I'm running a webservice which operates on wav-files, but I don't want to allow just any upload, so I check the duration of the uploaded file first with the following code:
os.chdir("/home/me/bin")
proc = subprocess.Popen(['duration',wav],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
out, errors = proc.communicate()
time = int(float(out.strip()))
if time > MAX_TIME:
sys.exit(1)
This has been working fine for several months, but recently (after a migration, I should add) I get the following error:
Traceback (most recent call last):
File "/home/me/webservice.py", line 100, in <module>
proc = subprocess.Popen(['duration',wav],stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=False)
File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
This error seems to be mostly caused by the use of a string for the command instead of a list, but that's not the case here. When I try to reproduce this error in a separate script, I can't.
Anyone have any idea what might be the cause here?
Commands without a path are always only looked up on the PATH. You cannot cd to a directory and have subprocess (or your shell for that matter) find a command located in that directory without specifying at least a relative path. This is standard command lookup behaviour.
If you want to run a command in the current working directory, you must specify a path of ./ to make it explicit the PATH search semantics are not to be used.
Relative paths in the PATH environment variable are also not supported everywhere; adding . to PATH is considered a security risk and some systems explicitly filter out . from the PATH. You must have migrated away from a system that allowed this, to a system that does disallow . in the PATH.
The error might not be pertaining to the executable you're feeding to Popen, but to an another executable that is subsequently called and is not found in the PATH of the new environment.
This exact situation happened to me, and the problem was that I had not installed the interpreter for the script I was calling. Frustrating to debug because the error message does not tell you which file it could not find.
Consider doing something like the following at the top of you file then there should be no need to chdir unless you have to for another reason.
import os
cmd = os.path.expanduser('~/bin/duration')
if not os.path.isfile(cmd):
raise IOError("command '%s' not found" % cmd)
proc = subprocess.Popen([cmd,wav ...