-4

I try to run several simple command line commands on my system and although the code run i cannot see my window opening.

For example:

command = 'cmd'
os.system(command)

Why i cannot see my cmd window ? all i can see in the console window (i am using pycharm) is this:

Microsoft Windows [Version 10.0.18362.449]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\blabla\tmp>

I also try to open appium server using command line and i have .bat file that call appium (this is what i put into the .bat file and this work fine manually):

This is the path to my .bat file:

C:\tmp\scripts\start_appium.bat

Command

p = subprocess.Popen("start_appium.bat", cwd=r"C:\tmp\scripts")
stdout, stderr = p.communicate()

And i received this error:

Traceback (most recent call last): File "C:\Python37\lib\subprocess.py", line 775, in init restore_signals, start_new_session) File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child startupinfo) File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.2.4\helpers\pydev_pydev_bundle\pydev_monkey.py", line 536, in new_CreateProcess return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args) FileNotFoundError: [WinError 2] The system cannot find the file specified

falukky
  • 1,099
  • 2
  • 14
  • 34
  • Possible duplicate of [Run a .bat file using python code](https://stackoverflow.com/questions/5469301/run-a-bat-file-using-python-code) – metatoaster Nov 19 '19 at 10:56
  • But this is exactly what i try to do – falukky Nov 19 '19 at 10:58
  • Try providing the full path of the batch file, and since this is a shell script, shell=True may be needed (as indicated by the second answer). `p = subprocess.Popen(r"C:\tmp\scripts\start_appium.bat", shell=True, cwd=r"C:\tmp\scripts")` – metatoaster Nov 19 '19 at 11:20
  • OK so now i received 'appium' is not recognized as an internal or external command, operable program or batch file, appium is the command inside my .bat file that run fine manually – falukky Nov 19 '19 at 11:50

1 Answers1

0

Python documentation clearly mentions cwd param is not the directory to search the executives.

If cwd is not None, the child’s current directory will be changed to cwd before it is executed. Note that this directory is not considered when searching the executable, so you can’t specify the program’s path relative to cwd.

You can refer the documentation here. https://docs.python.org/2/library/subprocess.html#popen-constructor

Also, try something like this

import subprocess
p = subprocess.Popen("C:\tmp\scripts\start_appium.bat")
  • Very strange, this is what i received: 'appium' is not recognized as an internal or external command, operable program or batch file. – falukky Nov 19 '19 at 11:43
  • My .bat file contains only appium command which work fine manually using cmd window or manually run this .bat file, what could cause this ? – falukky Nov 19 '19 at 11:44
  • The new error looks like you need to appium's path in environment variables. – Shashank Singh Nov 20 '19 at 05:20