Send commands to the terminal(Ubuntu) from ipython notebook - python

Currently I'm looking to send the following commands to the terminal.
cd ~/path/folder
./a-opt -i a.i
They HAVE to go to the terminal because I've modified my bashrc file to source certain program dependencies. Basically I'm running an executable a-opt with the options -i a.i
I've searched around the internet a bit on "running executables in terminal from ipython" and mostly what I get is how to create an executable from my python script. I don't want to do this. I want to use my script to run a string of executables. I've looked into
import os
but that does not seem to solve my issue.
Thanks!

Regarding to the python problem:
I think what you want is the commands lib:
[Python Documentation Page1
from commands import getoutput as cmd
then you can run
cmd("ls;ps;touch myfile")
And for what I've tested here, this module does NOT load the .bashrc.

I was able to solve this question by doing the following
import os
import subprocess
os.chdir('path')
subprocess.call('command',shell=True)
This does access the .bashrc file as intended.

Related

How can I execute multiple cmd commands using Python code

Hello I am having this problem where i want to automatically check the system32 file folder with the sfc cmd commands like sfc /verifyonly but the problem is that I can execute only one line in the cmd but I need to execute two and don't know how to execute a second one
import os
os.system("start /B start cmd.exe #cmd /k sfc")
I need another command for sfc /verifyonly so the program would work fully automatic can somebody help with anything pls
I tried a lot of things but nothing seemed to work for me or i am just really stupid and can't find the exact command i should be using
If you look at the docs for os.system they mention the subprocess module as being a more recent development within python that supports multiple systems level processes

Is there a way to reference an "ipconfig" file in Windows

So for a little context. In linux the "ifconfig" command is actually executing a "ifcfg-eth0" file found "/etc/sysconfig/network-scripts."
In windows, do command line (or powershell) commands correspond to a specific file? If so where? If it exists I having a hard time finding it.
Reason:
I am trying to execute commands from a program I am writing in Python. I know there are other ways to accomplish this ie. "import os, import subproccess." I am trying to brainstorm a simpler way to execute these commands before my program gets to heavy.
Basically I would like to tell python to execute a file ie. "ifcfg-eth0" in linux but in windows. Also, I'm just using "ipconfig" as an example There are a lot of commands I want to add.
In Windows, an easy way to find the path of a program is to use Where.exe.
where.exe ipconfig
Usually cmd commands are located in system32.
There's an executable ipconfig.exe in C:\Windows\system32

How can I run multiple shell commands at once through a Python subprocess on Windows?

Firstly, please excuse my lack of knowledge; I am a complete beginner at python.
I would like to run several command line commands from within a python script, using the subprocess module. My python script is supposed to be an application that downloads files from a url. These commands are shown below:
set /P _inputname= my-url-here
wget %_inputname%
(wget is in the same directory as my python script)
I have tried using this method:
cmds = ['set /P _inputname=', 'my-url-here', 'wget %_inputname%']
subprocess.run('cmds', shell=True)
However, it fails to run wget.
I understand that my question may seem very similar to this question and this question. However, the solutions in the aforementioned posts have not worked for me. Is there an alternative method?
The posts you mentioned were meant for systems with bash installed (such as most linux distros and other unix like oses)
Though I do not own a windows machine, I presume this will work for you:
(use the & sign to seperate commands)
cmds = ['set', "/P", '_inputname={}'.format('my-url-here'), '&','wget', '%_inputname%']
subprocess.run(cmds, shell=True)
note also that cmds is the variable cmds, not the string
source: How do I run two commands in one line in Windows CMD?

I have python script which included some bash commands in os.system(). Will this work in Windows?

I have a python script which included some bash commands in os.system() method .
If I convert this python script to exe using Pyinstaller, will this exe file work properly in windows OS or will I face any issues since Windows can't run bash commands ?
the bash commands include pdftk utility.
Example : pdftk input_pdf output output_pdf userpw password
Should I install pdftk utility also in Windows.
What should I do or install to make it work in Windows ?
Please help me..
Thank you
It won't work, os.system is os specific, in Windows it will just spawn a cmd process and try to execute that command and cmd != bash.
Edit: powershell has a lot of common bash commands implemented on windows, you could try to figure out in the code what os are you running on and if powershell supports your bash commands you could use the subprocess module to spawn powershell processes
It probably will not work, from what I have seen when using bash commands inside of code on windows.
Solutions:
Change the commands to commands that work on Windows.
Use some kind of python api (if you know of one post it in the comments and I will put it here.) that allows you to use the commands you need.
Simply run the script using the bash terminal on windows, but you won't be able to make it a exe as far as I know.

How to execute a system call in python using PyCharm on Debian Linux?

I am not able to run a system call using PyCharm and can't figure out what variables or environment settings to change.
Given this simple script:
import os
cmd = 'ifconfig -a'
os.system(cmd)
...which runs fine at the command line in terminal, yields the following error:
sh: ifconfig: command not found
This is happening with really any process I'm trying to run such as CSVSQL, PSQL, etc.
I have tried: Displaying my python interpreter paths dispayed at the command line, I tried adding them to the PyCharm interpreter paths, to no avail.
There are several other threads out there describing similar problems, but there doesn't seem to be a good solution that I have come across.
I'm running Linux Mint 19, though this works on my Windows installation (PATH output is much different).
My apologies if this is really simple... Thank you!
Run printenv on both Python and terminal, and check the PATH variable. Use os.environ['PATH'] = 'My path' to set it to what you saw on the terminal.
For future issues (That I've run into):
A quick way to check if it's an exported environment variable is to run os.system("/bin/sh -c \"MYCMD\""), and then run the same "/bin/sh -c \"MYCMD\"" string in your terminal. If there's still a problem, then it must be an export (And this is the likely issue).
To resolve this, try printenv in both python and the terminal to see the list of exports. You should see a discrepancy. The format is simple as you can simple copy the output of the terminal's printenv (Which should be a series of declares), and paste it into python so python will get the same variables. Then your "/bin/sh CMD" calls should align.
The wrapped /bin/sh is in case they're running different shells or have different local variables. echo $SHELL can confirm this, at which point you can compare sets and printenvs and copy paste in the same way. Wrapped you only have to compare exports, as that's what get passed to child processes.
Looks like pycharm isn't getting the PATH from your profile or rc. Try giving the absolute path of the command.
import os
cmd = '/sbin/ifconfig -a'
os.system(cmd)
You can also verify your path using following.
print(os.environ['PATH'])
And use following to add your custom path to current env path.
os.environ['PATH'] += ':/sbin'

Categories

Resources