0

I am trying to automate my build process. typically what I do is I go to a certain folder where my make scripts are at, open a CMD, type a command which is something like this "Bootstrap_make.bat /fic = Foo"

Now I want to do this from inside my python code. simple question is How to do this, i.e. how to; using python code. go to a certain path > and open a CMD > and execute the command"

here is what I've tried

def myClick():
subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ])
subprocess.run(["bootstrap_make.bat","/fic= my_software_variant"])

However I git this error :

Traceback (most recent call last): File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call return self.func(*args) File "C:\E\ProductOne_Builder1.py", line 5, in myClick subprocess.run(["cd ..\Projects\ADAS\proj11\embedded_env\make_scripts" ]) File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 966, in init self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\aelkhate\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1435, in _execute_child hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file specified

2
  • What have you tried so far? I would start with the documentation for the subprocess module. Commented Sep 5, 2022 at 18:23
  • @larsks I updated my question, sorry for inconvenience Commented Sep 5, 2022 at 18:45

2 Answers 2

1

You could use os.system(). https://docs.python.org/3/library/os.html

This code is assuming you are on Windows because you are running a .bat file and trying to open a CMD window.

import os

directory = './path/to/make/scripts'
buildCommand = './Bootstrap_make.bat'
buildArgs = '/fic = Foo'
# if you don't want a new cmd window you could do something like:
os.system('cd {} && {} {}'.format(directory, buildCommand, buildArgs))

# if you want a new cmd window you could do something like:
os.system('cd {} && start "" "{}" {}'.format(directory, buildCommand, buildArgs))
Sign up to request clarification or add additional context in comments.

2 Comments

You sir are the best, just a note that os.system('cd {} && start "" "{}" '.format(directory, buildCommand)) resulted an error, however the first solution that uses the same command line works. thank you so much:) .
(it's mam or miss) I just realized my mistake. I forgot about arguments. Removing the second pair of quotes from around the {} should do the trick.
0

You can use subprocess.run, specifying the cwd (current working directory) kwarg.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.