So I've been using subprocess and pexpect
to try to interact with a separate program running in the terminal. I need to feed it a command, with arguments, and be able to receive it's response and potentially send it more commands.
With subprocess, I have only been able to launch a terminal, but not feed it commands. Or I can pass ONE line of command to an emulated terminal within python. The issue it that it's one-and-done and I can't really interact with it.
pexpect seems to only be able to initiate one command, and then respond to the terminal in an automated fashion, I couldn't find relevant and up to date documentation that went over what I needed.
Are there better modules to use for this? Or am I using them the wrong way?
-Thanks,
-Sean
pexpect is your best candidate, as far as I'm aware.
It's documentation matches version on pypi - 3.2 as for now.
If you would like to run bunch of commands one after another you can try to divide commands with ";" or "&", depends on your usage.
Btw. please take a look at example section.
Related
I'm making my first steps in macOS app development.
I'm trying to write an app on Swift that would keep python interactive console open.
Sometimes I would like to send to python commands and return the results back to swift, but not closing python to keep all variables for the next command I will send.
Is there any way to do that?
As far as I understand, I can't use the Process() because the input pipe automatically closes when I run the task.
I probably need to use pseudo terminals with pty and tty, but I don't fully understand the idea and where to learn about it. (or, maybe, I'm wrong and there is another way)
If you actually want to use python code from swift, I would strongly advise you to avoid using this method. It is very bug prone and potentially limiting and inefficient. You better use some wrapper of the python-c-api, or write some small server in python to receive requests from swift.
If you still want to do that, an easy way to go about it would be to use python itself to spawn python inside a pty:
python -c "import pty, sys; pty.spawn(sys.argv[1:])" python
This will start a python console that reads and writes to stdio instead of of /dev/tty.
I am developing FUSE filesystem with python. The problem is that after mounting a filesystem I have no access to stdin/stdout/stderr from my fuse script. I don't see anything, even tracebacks. I am trying to launch pdb like this:
import pdb
pdb.Pdb(None, open('pdb.in', 'r'), open('pdb.out', 'w')).set_trace()
All works fine but very inconvenient. I want to make pdb.in and pdb.out as fifo files but don't know how to connect it correctly. Ideally I want to type commands and see output in one terminal, but will be happy even with two terminals (in one put commands and see output in another). Questions:
1) Is it better/other way to run pdb without stdin/stdout?
2) How can I redirect stdin to pdb.in fifo (All what I type must go to pdb.in)? How can I redirect pdb.out to stdout (I had strange errors with "cat pdb.out" but maybe I don't understand something)
Ok. Exactly what I want, has been done in http://pypi.python.org/pypi/rpdb/0.1.1 .
Before starting the python app
mkfifo pdb.in
mkfifo pdb.out
Then when pdb is called you can interact with it using these two cat commands, one running in the background
cat pdb.out & cat > pdb.in
Note the readline support does not work (i.e. up arrow)
I just ran into a similar issue in a much simpler use-case:
debug a simple Python program running from the command line that had a file piped into sys.stdin, meaning, no way to use the console for pdb.
I ended up solving it by using wdb.
Quick rundown for my use-case. In the shell, install both the wdb server and the wdb client:
pip install wdb.server wdb
Now launch the wdb server with:
wdb.server.py
Now you can navigate to localhost:1984 with your browser and see an interface listing all Python programs running. The wdb project page above has instructions on what you can do if you want to debug any of these running programs.
As for a program under your control, you can you can debug it from the start with:
wdb myscript.py --script=args < and/stdin/redirection
Or, in your code, you can do:
import wdb; wdb.set_trace()
This will pop up an interface in your browser (if local) showing the traced program.
Or you can navigate to the wdb.server.py port to see all ongoing debugging sessions on top of the list of running Python programs, which you can then use to access the specific debugging session you want.
Notice that the commands for navigating the code during the trace are different from the standard pdb ones, for example, to step into a function you use .s instead of s and to step over use .n instead of n. See the wdb README in the link above for details.
I know it might sound weird because I can do it using PowerShell, but I'd like to use Python to find what Windows Features are enabled.
I'll explain my reason and maybe you'll be able to guide me in a different direction, because from a quick google search, what I'm looking for is not possible.
I have a script that should be able to run on both Windows and Linux. It checks if a path to a directory exists, and if I have permissions to it. It does a bunch of other things and since it should work on both OSes, I need it to check that Windows Features thing. I don't want to run two different scripts, I'd rather have it all in one place and I'm not sure if maybe I'm able to call PowerShell inside my script.
Any idea what I can do?
I tried to search myself how to get the enabled windows features and found nothing, but i have some solution for you.
In my opinion you can run a powershell script inside your python script and get the result in a variable in you python script using subprocess module:
import subprocess
p = subprocess.Popen(["powershell.exe",
r"C:\Path\powershellscript.ps1"],
stdout=subprocess.PIPE)
output, err = p.communicate()
print(output)
The output variable will contain the data i believe you desire
I wish to run some commands which are specifically made for command line interface in idle environment.
There is a library in python called "Ezflix" which is for streaming torrent videos.
It runs properly on command line interface but does not work when I run it on python idle.
I know that command line commands cant be used in idle but I just wish to find if there is any possibility or any hack to make it run on idle.
According to https://pypi.org/project/ezflix/, ezflix is a "command line utility", one which happens to be written in Python. At this level, it is intended to be run from a command line terminal/console.
Such a program, even if written in python, might not be a Python library module, meaning that it is not intended that you import it and directly access its functions. If this is true, it would not have a supported and documented application program interface (API). If so, one could read the code and import it anyway, but the private internal objects and names might change from version to version. So the best way to access it from a Python program would be to run it separately, for instance, with subprocess, as suggested in the comments.
It turns out the ezflix does have a documented API and so it is also a library module. The is briefly described at the bottom of the pypi page linked above.
from ezflix import Ezflix
ez = Ezflix(<arguments>)
...
I presume that the package itself contains more information on its usage.
None of the above has anything to do with whether you run your program directly with python or with IDLE or with any other IDE. What could matter is whether the ezflix user interface specifically requires that it be run connected to the system terminal/console. Noting I saw on its pypi page suggests this. It might also be that the movie player window somehow interferes with the IDLE GUI window, but I also do not expect this.
EDIT: I got it working, I went into the pycassa directory and typed python pycassaShell but the 2nd part of my question (at the bottom there) is still valid: how do I run a script in pycassaShell?
I recently installed Cassandra and pycassa and followed the instruction from here.
They work fine, except I cant get pycassaShell to load. When I type pycassaShell at the command prompt, I get
'pycassaShell' is not recognized as an internal or external command,
operable program or batch file.
Do I need to set up a path for it?
Also, does anyone know if you can run ddl scripts using pycassaShell? It is for this reason that I want to try it out. At the moment, I'm doing all my ddl in the cassandra CLI, I'd like to be able to put it in a script to automate it.
You probably don't want to be running scripts with pycassaShell. It's designed more as an interactive environment to quickly try things out. For serious scripts, I recommend just writing a normal python script that imports pycassa and sets up the connection pool and column families itself; it should only be an extra 5 or so lines.
However, there is an (undocumented, I just noticed) optional -f or --file flag that you can use. It will essentially run execfile() on that script after startup completes, so you can use the SYSTEM_MANAGER and CF variables that are already set up in your script. This is intended primarily to be used as a prep script for your environment, similar to how you might use a .bashrc file (I don't know of a Windows equivalent).
Regarding DDL statements, I suggest you look at the SystemManager class.