Python - execute sequential commands in same windows command prompt - python-3.x

I am very new to Python (been learning for a few days) . Can anyone help me with the following issue please?
I am trying to write a program to automate the testing of built in commands of a product which is installed on windows and linux.
Before any of the commands can run, the profile needs to be set using profile.cmd.
The commands ( command1.cmd, command2.cmd etc) then need to run in the same windows command prompt as profile.cmd as they rely on the environment which this sets.
The problem I find is that after profile.cmd has been run, python closes the windows command prompt and opens a new one for command1.cmd.
command1.cmd therefore fails to run properly as it is in a new command prompt which has not had the environment set by profile.cmd.
My code is shown below. I am currently testing on windows but will also need to test on linux.
Any suggestions would be much appreciated. Thanks - Kevin
import subprocess
rc = subprocess.run (["profile.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command1.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command2.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))
rc = subprocess.run (["command3.cmd"],stdout=subprocess.PIPE)
print(rc.stdout.decode("ascii"))

Related

Subprocess.wait() in Python 2.7 vz Pthon 3.x

I'm just migrating my script from Python27 to Python 394 to run my build process like this:
p = subprocess.Popen(_make_clean, creationflags = subprocess.CREATE_NEW_CONSOLE)
p.wait()
p = subprocess.Popen(_make_rebuild, creationflags = subprocess.CREATE_NEW_CONSOLE)
p.wait()
Was working pretty nice in Python 27 but now I can see in Python 394 the Python script is not waiting for subprocesses. Of course, here it is necessary to wait until the first one (clean) is finished to start the second one (rebuild). I was able to make it work without the flag 'CREATE_NEW_CONSOLE'. Unfortunately, I need to separate the outputs for users to see what is the progress. It is just a mess if all scripts are running in the same console.
Why it is not working any more?
Ok, this is an interesting finding. I'm using ConEmu as my main terminal and it is fully integrated into Windows. In the past, I was NOT running my scripts over this emulated terminal and that seems to be the reason why I have this issue today. ConEmu is opening a new terminal window for each subprocess but in a different way than the native Windows Command-Line. When the script is called over Windows CMD everything is fine.

automation of ubuntu terminal and commands using selenium python

Can someone give me a code where I can automate.
Open the linux terminal using Ctrl+ALT+T.
Run small terminal commands using python and selenium .
I am a beginner and I want to know If i can do the above things using automation or Is there another way?

Execute python executable and script inlucing full path

Im trying to create a python script with executes a series of python and sql scripts.
Some need to be executed using one python executable and some using another.
I've tried
from subprocess import call
call([r"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
And
call([r"cd C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3", "pythonw.exe", r"C:\Path\to\python\file\blabla.py"])
But the script isnt being executed.
I think the problem might be "How to execute an .exe file in windows terminal including full path to it"?
Please check my code. It is working well in my side.
from subprocess import call
call([r"C:\Python38\python.exe", r"E:\python\hello.py"])
As i think, the problem is that you use pythonw.exe.
if you use pythonw.exe, you can see any log in terminal.
Pythonw is useful for running script with GUI.

Running two shell commands using Python 3.6 on Ubuntu 18.02

In order to get some software running I need to 1. Run a script that will execute a remote license manage, 2. Execute a shell script to start the software. I can do this by opening a command window in the directory with the rlm , and then type ./rlm to run the Linux executable. Then I can go into the directory that contains the shell script, open a terminal in that location and run ./myshell.sh. This opens the GUI for my software.
I would like to run these steps using a single Python script. I have tried:
#change the working directory...
os.chdir(r'/opt/mysoftwarelocation')
#confirm location change...
print(os.getcwd() )
#run ./rlm...
os.system('./rlm')
At this point I can see from a python terminal that the rlm is running.
I would then like to run the code below to run the shell script...
os.chdir(r'/opt/mysoftwarelocation/sumsubdirectory')
print(os.getcwd() )
os.system('./some.sh')
Unfortunately, after os.system('./rlm') finishes the script stalls and will not execute further and without an errors.
How to I get the second part of my script to run within a single Python script?
Have you tried to run the rlm command in the background?
subprocess module gives a nice interface for that

How to check if a script is already running

How do you check if your script is already running to prevent it from running/opening multiple times.
I am aiming for it to either quit and allow the script to open again or stop the new one from running.
Using:
Python 3.8
Windows 10
File type .pyw
Ide: Thonny
I have looked and haven't been able to find a answer.

Resources