I am trying to write a python script which will run a run.ps1 PowerShell script. The run.ps1 file is located in different location entirely so I will need to cd into that folder then run the script. In Windows power shell I just need to cd into the file directory then run ./run.ps1. I am trying to simulate that process in Python.
So far I have got:
import subprocess
subprocess.run("C:/..pathtofile/run.ps1")
This opens up that script in notepad. I want to actually run the script like I would in the terminal.
Related
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.
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
I have two files :
run.sh
train.py
Content of both file
in run.sh
export TRAINING_DATA=test1
export FOLD=test2
python.exe train.py
in train.py
from os import environ
print(environ.get("TRAINING_DATA"), environ.get("FOLD"))
when I run the bash file through command line
bash run.sh
Instead of getting test1 & test2, I am getting None, None
Both the files are placed in the Scripts folder of my virtualenv. I am using Windows 10.
The issue you are hitting is that you are running the windows version of python.exe rather than the linux/bash version.
In the run.sh i see that you are refering to python.exe, this is the windows exe and as such when it runs it will not be in the same shell as the .sh script used to invoke it.
you can prove this to yourself by doing the following from within WSL
➜ which python.exe
/mnt/c/Python27/python.exe
➜ which python
/usr/bin/python
So as long as you have python installed in WSL you can simply swap out the .exe in the .sh script so that you are using the bash/linux version thus:
export TRAINING_DATA=test1
export FOLD=test2
python train.py
You should try disconnect and log back in on the session.
Happened to me and it's how I solved it.
On my side it was on a ssh console.
Have a great day/night-
I have created selenium testng.xml file and make windows batch file for my local window machine and Jenkins build now function perfectly working
But now client expectation is run jenkins projects for same selenium script in Linux server machine.
I m not aware about how can i start for my testng file for linux server?
You created batch file while working with testng.xml on Windows. You would create an equivalent shell file while working with testng.xml on Linux.
Once done, Go to Jenkins Job configuration build section and select Execute Shell option.
Put you shell script in command window or execute shell file directly from command window itself.
To execute shell file, use below syntax:
./<file>
I am trying to launch a .sh script from Python 3.3 in Ubuntu 13.10.
The script is supposed to shutdown the computer. I have already marked the sh script as executable through the terminal. I have tried to run the sh script through: os.system("script.sh"), subprocess.Popen("Script.sh"), and subprocess.call([script.sh]).
They keep returning the: OSError Exec format error.
Any help would be greatly appriciated!
I assume that script.sh isn't in your PATH but in your current working directory.
By default os.system and subprocess look in your path for the requested executable. So to execute something in your current working directory you need to specify the executable like this:
subprocess.call("./script.sh")
The ./simply says that the executable that should be executed is in the current working directory.