Cant execute script in parent folder - python

I am trying to execute/call a python script that resides in another directory.
My Problem: When I attempt to open/call the file I get the error
'..' is not recognised as an internal or external command, operable
program or batch file
My python code to execute the python file is:
os.system("../test.py abc")
I have also tried this but I get the same error on a DIFFERENT part of the string:
os.system(os.getcwd()+"/../test.py abc")
# results in "c:/users/jim/work products/python/testdir/../test.py abc"
Error:
'c:/users/jim/work ' is not recognised as an internal or external command, operable
program or batch file

In windows you should use '..\\executeablename' to run a program or script in the parent directory, not '../' the unix style.
And, to ensure the script can run property, a 'python' is better to be added before the command.
So I think this situation should be:
os.system("python ..\\test.py abc")
It has not been tested since I am using linux, you can just try.
BTW, 'os.system' is kinda deprecated and the subprocess module is recommend to use when execute system level command.

A .py file isn't an executable, so that won't work on Windows. You have to run it with python.exe:
import subprocess
import sys
subprocess.call([sys.executable, '../test.py', 'abc'])
You could do this with system, but I figure this is easier because you don't have to quote the filename.

Related

Python and windows scheduler - run py script that invokes other scripts

I am trying to set up a windows scheduled task to run a python script that runs two other scripts. I use os package to do it with the code like below:
#mainScript.py
import os
os.system('script1.py 1')
os.system('scripts.py 1')
The script works in a scheduled task except when I add the above code. What I mean If use script1.py in my bat file it works, but if I use the mainScript.py it gives me the error code.
'script1.py' is not recognized as an internal or external command,
operable program or batch file.
'script2.py' is not recognized as an internal or external command,
operable program or batch file.
I know I can just move all the code into mainScript but wonder if there is anything I can do to have.
You need to specify wich program will run the script.
Try this :
os.system('python script1.py')

Using Espeak with os in python

When I open up my command prompt, i am able to type espeak and then the text i want to be said, however when i try to do this through my python code using os it says
'espeak' is not recognized as an internal or external command,
operable program or batch file.
import os
text = "Apples"
os.system('espeak "{}"'.format(text))
I've tinkered with the code a fair amount but there is not much to tinker
try /usr/bin/espeak instead of espeak.
That works for my machine.
In order to be sure it works for your host I suggest you open a console and type
type espeak
This will output the absolute path of espeak. Copy that one into your system command

Python 3.6 trying to use commands from another program

Part of my python program uses subprocess to open a vbs script.
path = os.sep.join(['C:','Users',getpass.getuser(),'Desktop','Program','build','exe.win32-3.6','vbs.vbs'])
subprocess.call([sys.executable, path])
But instead of executing my vbs script it tries to run it as a python code and gives me: NameError: msgbox is not defined.
And when i manually run vbs script it works.
I want python to normally execute the vbs script. Not run it as python code.
sys.executable points to the system's Python executable. In your case that'd probably be something like C:\Python27\python.exe. You should print it out and see for yourself.
To execute VBScripts, you'd want to use C:\Windows\system32\wscript.exe.
Additionally, using os.path.join() is more suited to the task than os.sep.join().
So you'd end up with:
system32 = os.path.join(os.environ['SystemRoot'], 'system32')
wscript = os.path.join(system32, 'wscript.exe')
path = os.sep.join(['C:','Users',getpass.getuser(),'Desktop','Program','build','exe.win32-3.6','vbs.vbs'])
subprocess.call([wscript, path])
This is exactly what you are telling subprocess to do. From the docs
sys.executable
A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is
unable to retrieve the real path to its executable, sys.executable
will be an empty string or None.

Path to executable added to PATH and executable runs using cmd but not from a python IDE

I have an .exe under %COPASIDIR%\bin and this path has been added to the PATH variable. When I open cmd and type the name of the exe (CopasiSE) I get the expected behaviour, shown in the screen shot below:
Additionally, when I start ipython or python from the command prompt and run os.system('CopasiSE') I also the expected behaviour. However, when using an IDE (spyder and pycharm) the I get:
import os
os.system('CopasiSE')
Out[9]: 1
'CopasiSE' is not recognized as an internal or external command,
operable program or batch file.
Does anybody have any idea why this is happening?
Edit
Based on comments I tried using subprocess.call with the shell=True switch and got the following:
Edit2
After seeing #Arne's comment I compared the path that I get from os.system('echo %PATH%') command from the IDE (path_ide) to what I get directly from the shell (path_cmd) and the specific path pointing to the directory containing my exe is in both outputs.

Stop invoking of new Shell/cmd prompt , python

i have a python script which should invoke a .exe file to get some result. That .exe file is invoking a new windows command prompt(shell) . i dont need any output from the .exe file.
i 'm using
os.system('segwin.exe args') in my script where segwin is an executable.
now my question is : i need to stop invoking cmd prompt
kudos to all
sag
Try this (untested):
import subprocess
CREATE_NO_WINDOW = 0x08000000
args = [...]
subprocess.check_call(["segwin.exe"] + args, creationflags=CREATE_NO_WINDOW)
Note that check_call checks the return code of the launched subprocess and raises an exception if it's nonzero. If you don't want that, use call instead.
In general, avoid os.system() and use the subprocess module whenever possible. os.system() always starts a shell, which is nonportable unnecessary on most cases.
This is actually specific to Windows. Windows has decided that segwin.exe is a console-based application (uses the Console subsystem from the Windows C interface).
I know how to invoke an prompt for apps that don't necessarily want one, but not the reverse, you could try using this, or this.

Categories