Can't run a linux .sh script with python subprocess? - python

I am trying:
import subprocess
subprocess.call(["file.sh"])
But I keep getting:
Traceback (most recent call last):
File "project.py", line 85, in <module>
subprocess.call(["file.sh"])
File "/usr/local/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/local/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
However when I try and run the script from the shell bash file.sh it works. So I'm confused as to why it doesn't work?
I am not committed to using subprocess so if there are other options please let me know.

The call function of the subprocess package runs the command specified in arg as a list of strings (to simplify).
To call your file you have put in your script:
import subprocess
subprocess.call(["sh", "file.sh"])

Related

FileNotFoundError: [Errno 2] No such file or directory: 'python'

I'm trying to to write a simple python app and launch it using TorchX following this Quickstart guide. Following commands are executed in my home directory:
pip3 install "torchx[dev]"
Write a simple python program using vim editor vi my_app.py
# my_app.py
%%writefile my_app.py
import sys
print(f"Hello, {sys.argv[1]}!")
Execute the app via torchx run as torchx run --scheduler local_cwd utils.python --script my_app.py "your name".
And then I get a long error:
Log files located in: /var/folders/h7/qrsyzj5s7hvbkwmv83by9wgc0000gn/T/torchx_hykfxkz3/torchx/torchx_utils_python-vxm9hhpt3k7wtd/python/0
Traceback (most recent call last):
File "/usr/local/bin/torchx", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.9/site-packages/torchx/cli/main.py", line 110, in main
run_main(get_sub_cmds(), argv)
File "/usr/local/lib/python3.9/site-packages/torchx/cli/main.py", line 106, in run_main
args.func(args)
File "/usr/local/lib/python3.9/site-packages/torchx/cli/cmd_run.py", line 271, in run
self._run(runner, args)
File "/usr/local/lib/python3.9/site-packages/torchx/cli/cmd_run.py", line 232, in _run
app_handle = runner.run_component(
File "/usr/local/lib/python3.9/site-packages/torchx/runner/api.py", line 147, in run_component
return self.schedule(dryrun_info)
File "/usr/local/lib/python3.9/site-packages/torchx/runner/api.py", line 225, in schedule
app_id = sched.schedule(dryrun_info)
File "/usr/local/lib/python3.9/site-packages/torchx/schedulers/local_scheduler.py", line 748, in schedule
replica = self._popen(
File "/usr/local/lib/python3.9/site-packages/torchx/schedulers/local_scheduler.py", line 688, in _popen
proc = subprocess.Popen(
File "/usr/local/Cellar/python#3.9/3.9.13/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/Cellar/python#3.9/3.9.13/Frameworks/Python.framework/Versions/3.9/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'python'
I have Python 3.9.13 installed on macOS Monterey (version 12.4).
What could be a cause of this error?

Windows 10 Error: FileNotFoundError: The System cannot find the file specified subprocess while executing an .exe

I get an error every time i want to start a exe build python. I cant change the code because its an exe.
The full error:
Traceback (most recent call last):
File "boltchecker.py", line 6897, in <module>
File "boltchecker.py", line 4819, in __init__
File "subprocess.py", line 411, in check_output
File "subprocess.py", line 489, in run
File "subprocess.py", line 854, in __init__
File "subprocess.py", line 1307, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified
[18104] Failed to execute script boltchecker
I hope someone can help me with this :)

"environment can only contain strings" error while creating a Django CMS project on Windows

I am getting this error when trying to build a project with
djangocms -p . mysite
I have gone through all the answers in StackOverflow but couldn't solve the problem.
Traceback (most recent call last):
File "C:\Users\PC4\Desktop\site14\env\Scripts\djangocms-script.py", line 9, in <module>
load_entry_point('djangocms-installer==0.8.1', 'console_scripts', 'djangocms')()
File "c:\users\pc4\desktop\site14\env\lib\site-packages\djangocms_installer\main.py", line 41, in execute
django.setup_database(config_data)
File "c:\users\pc4\desktop\site14\env\lib\site-packages\djangocms_installer\django\__init__.py", line 404, in setup_database
env=env
File "c:\python27\Lib\subprocess.py", line 535, in check_call
retcode = call(*popenargs, **kwargs)
File "c:\python27\Lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "c:\python27\Lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "c:\python27\Lib\subprocess.py", line 958, in _execute_child
startupinfo)
TypeError: environment can only contain strings
Changing to Linux is not an option.

Popen report error: WindowsError: [Error 2] The system cannot find the file specified

cmd = ['scons', '-c', 'SHARED=yes', 'STATIC=yes']
when I tried to execute subprocess.Popen(cmd), just got the error prompt as below:
scons -c SHARED=yes STATIC=yes
Traceback (most recent call last):
File "setup.py", line 37, in <module>
if Popen(cmd).wait():
File "D:\Python27\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "D:\Python27\lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
I was completely confused about this, is there anyone who could help me figure out? I was just a rookie on Python, thanks for a bunch!

Error while running Plone 4.2 instance fg on Window

While running buildout on window with Python 2.6 for Plone 4.2, All went fine. but while running instance on foreground it give me this window specific (presume) error about some file missing.
I remember on older version of Plone for installing PyWin type libary, I had to copy some dll file to window system file and I also done same for this. still I am having this error.
Any suggestion/advise would great help.
Thank for taking your time.
D:\rcseng\buildout>bin\instance fg
Traceback (most recent call last):
File "D:\rcseng\buildout\bin\instance-script.py", line 258, in <module>
+ sys.argv[1:])
File "d:\rcseng\buildout\eggs\plone.recipe.zope2instance-4.2.8-py2.6.egg\plone
\recipe\zope2instance\ctl.py", line 614, in main
c.onecmd(' '.join(options.args))
File "D:\Python26\lib\cmd.py", line 219, in onecmd
return func(arg)
File "d:\rcseng\buildout\eggs\zdaemon-2.0.7-py2.6.egg\zdaemon\zdctl.py", line
588, in do_fg
self.do_foreground(arg)
File "d:\rcseng\buildout\eggs\plone.recipe.zope2instance-4.2.8-py2.6.egg\plone
\recipe\zope2instance\ctl.py", line 550, in do_foreground
self._exitstatus = subprocess.call(command, env=env)
File "D:\Python26\lib\subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "D:\Python26\lib\subprocess.py", line 623, in __init__
errread, errwrite)
File "D:\Python26\lib\subprocess.py", line 833, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

Categories