consider portion of Python code(Python 2.7) on Win 7 machine,
toolPath="C:\\Program Files (x86)\\Target Compiler Technologies\\adsp2-12R2\\bin\\WINbin"
This is executed from python script under
C:\dev\bin\toplevel\python
Now, I need to execute a command that runs(compiles and builds a DSP library, .prx is a project file for the library)
as
C:\Program Files (x86)\Target Compiler
Technologies\adsp2-12R2\bin\WINbin\chessmk.exe
".\..\..\..\dev\lib\adsp2\mylibs.prx -r -s
I am able to do that in cmd.exe shell as
%toolPath%\chessmk.exe "..\..\..\dev\lib\adsp2\mylibs.prx" -r -s
I can do the same in Python as
cmd = '"C:\\Program Files (x86)\\Target Compiler Technologies\\adsp2-12R2\\bin\\WINbin\\chessmk.exe" "C:\\SVN\\ASROmni\\trunk1\\\dev\\lib\\adsp2\\mylibs.prx" -r'
subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=False)
But, I have not been able to write similar code for Python, with relative paths and using toolpath variable.
for example,
cmd = 'toolPath+"\\chessmk.exe" ".\\..\\..\\..\\dev\\lib\\adsp2\\mylibs.prx" -r -s'
subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=False)
gives error:
WindowsError: [Error 2] The system cannot find the file specified
following did not work too:
cmd = '"C:\\Program Files (x86)\\Target Compiler Technologies\\adsp2-12R2\\bin\\WINbin\\chessmk.exe" ".\\..\\..\\..\\dev\\lib\\adsp2\\mylibs.prx" -r'
subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=False)
Update:
from one of the comments, I tried
cmd = os.path.join(toolPath,"chessmk.exe")+' C:\\SVN\\ASROmni\\trunk1\\\dev\\lib\\adsp2\\mylibs.prx -r'
it works,but the relative path one
cmd = os.path.join(toolPath,"chessmk.exe")+' .\\..\\..\\..\\dev\\lib\\adsp2\\mylibs.prx -r'
still dosn't.
any help.( This is my first day with python, so bear with me)
please note that the cmd should be run from within Python, not invoking shell=True.
Thanks
sedy
It may be how you're passing the arguments to check_output(), try this instead:
toolPath = ('"C:\\Program Files (x86)\\Target Compiler Technologies\\'
'adsp2-12R2\\bin\\WINbin\\chessmk.exe"')
cmd = [toolPath, '"..\..\..\dev\lib\adsp2\mylibs.prx"', '-r' '-s']
subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False)
However the relative path for the location of mylibs.prx may still be a problem. You might be able to make it work by calling os.chdir() right before calling check_output().
Related
When I try to run a makefile (type "make test" in terminal) which runs a batch file I get the equivalent error:
cd directory_path && test.bat
/bin/sh: test.bat: command not found
make: *** [makefile:58: test] Error 127
The makefile is:
.PHONY: test
test:
cd directory_path && test.bat
python path/test.py
(I changed names of the batch file, the directory path, etc. to try and make things more generic)
I can manually type "cd directory_path" and then "test.bat" and those both work. This makefile works on other systems. It only doesn't work on mine. I think it is an issue with how I installed Cygwin and how I run "make.exe".
I can use make to compile C code, but I also get an error trying to use make to run python scripts. The makefile from before also has a command for python path/test.py. This also isn't working on my system. If I delete the batch file line, but keep the python command it throws the error:
python path/test.py
make: python: No such file or directory
make: *** [makefile:59: test] Error 127.
I don't understand why it throws the error of no such file or directory since the path leads to the file, and if i put the python script in the same working directory as the makefile then it still can't find it.
Any ideas/solutions on these problems? Thank you!
EDIT:
Doug Henderson:
I started a cmd prompt at the place with the make file and entered all of these commands in
uname -a
CYGWIN_NT-10.0 james-mobl2 3.1.6(0.340/5/3) 2020-07-09 08:20 x86_64 Cygwin
which make
/usr/bin/make
make -v
GNU Make 4.3
Built for x86_64-pc-cygwin
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
which python
which: no python in
(/cygdrive/c/Program Files/AdoptOpenJDK/jdk-8.0.252.09-hotspot/bin:
/cygdrive/c/windows/system32:
/cygdrive/c/windows:
/cygdrive/c/windows/System32/Wbem:
/cygdrive/c/windows/System32/WindowsPowerShell/v1.0:
/cygdrive/c/windows/System32/OpenSSH:
/cygdrive/c/Program Files/Git/cmd:
/cygdrive/c/Users/james/AppData/Local/Microsoft/WindowsApps/python:
/cygdrive/c/Program Files/PuTTY:
/usr/bin:
/cygdrive/c/Program Files/AdoptOpenJDK/jdk-8.0.252.09 hotspot/bin:
/cygdrive/c/Users/james/AppData/Local/Microsoft/WindowsApps)
(I cleaned up the output a little to make it more legible). This and which test.bat are the only commands that failed.
python -V
Python 3.8.5
The change directory changed correctly
which test.bat
which: no test.bat in
(/cygdrive/c/Program Files/AdoptOpenJDK/jdk-8.0.252.09-hotspot/bin:
/cygdrive/c/windows/system32:
/cygdrive/c/windows:
/cygdrive/c/windows/System32/Wbem:
/cygdrive/c/windows/System32/WindowsPowerShell/v1.0:
/cygdrive/c/windows/System32/OpenSSH:
/cygdrive/c/Program Files/Git/cmd:
/cygdrive/c/Users/james/AppData/Local/Microsoft/WindowsApps/python:
/cygdrive/c/Program Files/PuTTY:
/usr/bin:
/cygdrive/c/Program Files/AdoptOpenJDK/jdk-8.0.252.09 hotspot/bin:
/cygdrive/c/Users/james/AppData/Local/Microsoft/WindowsApps)
I added cmd /c test.bat to the makefile and it worked, but I've also had the batch file run on different systems without that addition. I also have a lot batch files in the actual makefile so this solution works but isn't ideal. It also didn't fix the python script not running.
I didn't initially have cmd.exe in my path, but I added it before entering those commands.
I'm not sure what you mean by starting my PATH in bash with /usr/bin;/bin;THE_REST
MadScientist:
I was manually entering it into a Windows Terminal. Is it possible to have Cygwin invoke a Windows command.com shell?
What I did was go to directory that contained the makefile. Open a windows terminal by typing 'cmd' in the address bar. I would then type 'make test'. That obviously led to the issues. I then went through and manually entered the commands I thought the makefile was supposed to invoke. So I entered 'cd directory_path && test.bat'. This ran.
Matzeri:
I don't really know. I'm completely new to POSIX, etc. I'm guessing I'm using Windows style, and judging from other responses, I'm trying to use Cygwin to do a non-POSIX style.
Thank you, everyone for responses!
Make sure cygwin is installed with basic packages --> Some basic packages not installed with cygwin try below link to install some basic package, uninstall the cygwin and install it again with following basic package :
https://wiki.usask.ca/display/MESH/Running+Python+from+the+Cygwin+Terminal#RunningPythonfromtheCygwinTerminal-Cygwin
make sure cygwin's bin folder is added to your environment variable (e.g., C:\cygwin64\bin)
If cmd /c test.bat works then . is probably not in your PATH. Always do ./test.bat when you want to run something in the current directory. Never count on the PATH unless you have set it yourself. Also make sure the file is executable. chmod +x test.bat
Using Python 3.7, I am trying to invoke g++ to compile and build a C++ file via
#!/usr/bin/env python3
import subprocess
if __name__ == '__main__':
subprocess.run(
executable="/usr/bin/g++",
args=["/some/path/source.cpp", "-std=c++17"],
shell=True
)
When I run the script, the executable builds. I then chmod u+x it. However, when I try to execute the executable, it fails and says:
-bash: ./a.out: cannot execute binary file: Exec format error
I've read some other posts regarding this error however none are applicable. For some reason, this method fails, however when I run g++ natively in my terminal, it works as expected.
Edit: When I invoke file a.out, the output is
a.out: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), not stripped
Appreciate any help, thanks!
The executable parameter to subprocess is only rarely needed. With shell=False, it overrides args[0] as the program to run (allowing argv[0] to be customized, as for a login shel). With shell=True (which should be avoided when possible, partly because it doesn’t do what you think with your carefully separated args list), it replaces the implicit /bin/sh invoked to run the command. The standard option to run one command is -c, so you ran
/usr/bin/g++ -c /some/path/source.cpp -std=c++17
which indeed produces a relocatable (i.e., a .o file). a.out is not the normal name for such, but perhaps it’s a fallback when the directory containing the source is not writable.
I want to run the exe file with command line arguments in Mac terminal
p1.exe -f input.txt
But im getting error -bash: p1: command not found
I have converted python file p1.py into p1.exe using
pyintsaller p1.py --onefile
And running the python file with arguments works
python p1.py -f input.txt
This isn't to do with Python, but is a basic command shell issue. To run an executable from the current directory, you need to use the ./ prefix.
./p1.exe -f input.txt
Note, it's a bit odd to use a .exe extension for a Linux executable.
Note that on Unix like systems (Linux/Unix/Solaris/MacOS). scripts can be run without explicitly invoking interpreter, if two conditions are meet:
script file starts with this line (or similar): #!/usr/bin/env python
file has executable attribute flag is set
Then you can run script like this:
./p1.py --onefile
./ means run thing from local directory. If this is not pressent the it tries to run things located by PATH variable, that is why you can run interpreter python
I am using py2app to bundle a python script, that uses the anaconda python distribution.
Since py2app doesn't play well with terminal scripts that need user input, I have a Tkinter file that py2app's setup.py launches, which then further launches my .py script using popen.
Locally, this works fine:
import subprocess as sub
command = "~/anaconda/bin/python -u myscript.py " + str(groups_count)
process = sub.Popen(command, shell=True, stdout=sub.PIPE, stderr=sub.PIPE, bufsize=1, universal_newlines=True)
But when I want to distribute this, I need to replace hardcoded paths and run this using the distribution contained within
import subprocess as sub
command = sys.executable + " -u myscript.py " + str(groups_count)
process = sub.Popen(command, shell=True, stdout=sub.PIPE, stderr=sub.PIPE, bufsize=1, universal_newlines=True)
This results in an error:
/bin/sh: /Users/username/projectname/appname/dist/MyOSXapp.app/Contents/MacOS/python: Permission denied
If I look at libpython2.7.dylib within /MyOSXapp.app/Contents/Frameworks it doesn't seem to be executable, but is readable by everyone. This is all assembled by py2app.
I need to run popen on my anaconda python distributed within pyapp. How do I do this?
I worked around this by adding executable permission to the python file saved by py2app within the osx .app file.
$ chmod +x ./dist/MyOSXapp.app/Contents/MacOS/python
Additionally, a fix for this was also added in py2app via issue 228.
I am trying to compile & execute my hw cpp file under the python script file which we are given by lecturer. the how-to-manual.pdf he sent us it says use:
c:\>python ./submit.pyc problemID -u username -p password -b //submit.pyc is already given to us
and here is the manifest.txt we are given:
[main]
problem = gc
build =
g++ main.cpp -o solver
run =
./solver %f
my cpp file works normally like this:
./solver input_file
However, I am trying (I have to) to do this under the windows OS. I have Python 2.7.x installed and python.exe is in the Command PATH. I can't run it under the linux ssh sytem because there is 2.4.x python installed and I can't touch it (school's system).
Anyway, when I execute the line above, it returns me:
Command execution failed:
g++ solver.cpp -o solver
I think I told everything I can. So, any idea that what I have to do else? except asking to lecturer:)
For the above to work it needs to be able to find g++ so you need to add the directory that it resides in to the PATH environment variable. This can be done from within your python script or on the command line with:
path=Where\g++\lives;%path%
This will only apply within the current DOS session.
Or you can add it permanenty through system settings->advanced settings->environmental variables
You could also look at using a python virtual environments on the schools linux system.