Python open new terminal

Opening terminal using python

I can replicate this error when i open a terminal as root user. change my desktop wallpaper, Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.

Opening terminal using python

Currently I’ve been creating just a small program. And one of the options is to start a counter.

from time import sleep number = 0 while 1 == 1: sleep(1) print number number += 1 

I want to be able to open this program in a new terminal window (Ubuntu) so I can keep the existing terminal open. Anyway to do this in python?

EDIT I think I figured out one way to run it in the same terminal so it works!

I simply used that, I thought if I were to do that before, when I used CTRL + C it would close down both programs but this seems to of worked. Thanks for the answers!

You could create a wrapper that launches your terminal and tells it to run your script. If you know which terminal you’re using, this isn’t too hard. For example, if you use multi-gnome-terminal :

#!/bin/sh multi-gnome-terminal --use-factory --command /usr/bin/python /path/to/my/script.py 

Now, every time you run that wrapper (with sh ./wrapper , or /usr/local/bin/wrapper if you install -m755 it, or by double-clicking it, or whatever), it will open a new window (launching a new terminal process only if necessary) and run your script there.

Powershell — How to open a new terminal within a, It’s not entirely clear what you’re asking, but here’s a few pointers for Windows:. If you’re using regular console windows (conhost.exe):. To open another cmd.exe console window from cmd.exe (run start /? for help):. start cmd; Or even just start by itself.; To open another PowerShell console window from …

Launching a python script in a new terminal from another python script

I’m using LXDE, I would like to Launch a python script in a new terminal from another python script.

I would like the new python script to be totally independent.

I have tried a lot of things. Calling xterm (or x-terminal-emulator) directly from python with the subprocess.call or subprocess.Popen with or without shell=True argument, it didn’t work. It gives me an error about display not being set.

I have also created a sh file which calls the other python script and tried to call it, same results.

import os os.system("xterm -e \"python christmaskittens.py\"") 
import os os.system("xterm -e \"python christmaskittens.py\" &") 

Opening a Python thread in a new console window, In Windows, you would use console = [«cmd.exe», «/c», «start»]. The start makes Windows open a new window. Also, I prefer to reference the COMSPEC environment variable and use capital letters, as in console = [os.environ.get («COMSPEC», «CMD.EXE»), «/C», «START»], but that’s just my style.

Читайте также:  Html код мой профиль

How to run a command in a new terminal/tab, but stay in original terminal/tab with cli on any OS?

Pretend this is my package.json

I want to npm run doStuff in a terminal

Script A will open a new terminal or tab and run a python simple server.

Script B will remain in the original terminal/tab and run tests on the served contents from script A.

I’m looking for a universal solution that works on any OS, even if that means running commands that don’t work on the target OS to get to the one that does.

Is this possible or am I only dreaming?

On Mac, you can use a tabbed terminal app like iTerm2 on macOS. You can also use AppleScript to launch terminal windows and run specific scripts. The specific implementation will depend on your operating system.

For example, this AppleScript opens a new tab in Terminal:

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' 

Another option is to use the Node.js package ttab , available on NPM.

ttab 'python -m SimpleHTTPServer 8888' 

Open 2 terminals and print 2 different message in Python, I am new python (ver 2.7) programmer and I have a question how to open 2 terminals and output different message. The main task of this program is open 2 terminals and output different message. Right now, I have 3 following files. test.py — it is the main file that suppose to open 2 terminals and call different …

How to make python open new Terminal as normal user and not as root user

Im trying to get python to run a terminal command which will change my desktop wallpaper. Running this command in a normal terminal will change my wallpaper. However when i try to make python run this command in terminal it doesnt work and gives me an error.

I can replicate this error when i open a terminal as root user. I logged in using su and typed my password. Typing in the command then gives me an error and it does not execute. The same when i try to execute the command with python using either the modules os or subprocess.

Is it because my command to change the wallpaper which starts with pcmanfm (which is the window manager) has probblems with its path and when i am root user the path is changed?

How can i make python open a terminal as «normal» user in my case «pi» on my raspberry pi, and run a command in it?

Читайте также:  Фрейм в java это

this is the terminal command which changes my desktop wallpaper:

pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg 

this is what happens when i run my python code to open a terminal:

root@raspberrypi:/home/pi/Desktop# 

instead of normally when it works:

here is my python code which is meant to open a new terminal with the command which changes my desktop wallpaper, however i end up as ROOT user as described above and get an error and nothing happens:

import os,random import subprocess as sub sub.call('lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"', shell=True) 

executing the following from a normal terminal works perfectly:

lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash" 

this is the result after running the python code, which is the error i always get:

** Message: 12:11:08.734: x-terminal-emulator has very limited support, consider choose another terminal root@raspberrypi:/home/pi/Desktop# 

So how can i make python open a terminal as user «pi» not as root? or is this not the problem? thanks!

The problem was calling the python script as sudo. Opening the python script normally as: Python3 script.py Will open a terminal as a normal user and. change my desktop wallpaper, Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.

Python — How to run a command in a new terminal/tab, Script A will open a new terminal or tab and run a python simple server. Script B will remain in the original terminal/tab and run tests on the served contents from script A. I’m looking for a universal solution that works on any OS, even if that means running commands that don’t work on the target OS to get to …

Источник

pty — Pseudo-terminal utilities¶

The pty module defines operations for handling the pseudo-terminal concept: starting another process and being able to write to and read from its controlling terminal programmatically.

Pseudo-terminal handling is highly platform dependent. This code is mainly tested on Linux, FreeBSD, and macOS (it is supposed to work on other POSIX platforms but it’s not been thoroughly tested).

The pty module defines the following functions:

Fork. Connect the child’s controlling terminal to a pseudo-terminal. Return value is (pid, fd) . Note that the child gets pid 0, and the fd is invalid. The parent’s return value is the pid of the child, and fd is a file descriptor connected to the child’s controlling terminal (and also to the child’s standard input and output).

Open a new pseudo-terminal pair, using os.openpty() if possible, or emulation code for generic Unix systems. Return a pair of file descriptors (master, slave) , for the master and the slave end, respectively.

pty. spawn ( argv [ , master_read [ , stdin_read ] ] ) ¶

Spawn a process, and connect its controlling terminal with the current process’s standard io. This is often used to baffle programs which insist on reading from the controlling terminal. It is expected that the process spawned behind the pty will eventually terminate, and when it does spawn will return.

Читайте также:  Html radio изменить цвет

A loop copies STDIN of the current process to the child and data received from the child to STDOUT of the current process. It is not signaled to the child if STDIN of the current process closes down.

The functions master_read and stdin_read are passed a file descriptor which they should read from, and they should always return a byte string. In order to force spawn to return before the child process exits an empty byte array should be returned to signal end of file.

The default implementation for both functions will read and return up to 1024 bytes each time the function is called. The master_read callback is passed the pseudoterminal’s master file descriptor to read output from the child process, and stdin_read is passed file descriptor 0, to read from the parent process’s standard input.

Returning an empty byte string from either callback is interpreted as an end-of-file (EOF) condition, and that callback will not be called after that. If stdin_read signals EOF the controlling terminal can no longer communicate with the parent process OR the child process. Unless the child process will quit without any input, spawn will then loop forever. If master_read signals EOF the same behavior results (on linux at least).

Return the exit status value from os.waitpid() on the child process.

waitstatus_to_exitcode() can be used to convert the exit status into an exit code.

Raises an auditing event pty.spawn with argument argv .

Changed in version 3.4: spawn() now returns the status value from os.waitpid() on the child process.

Example¶

The following program acts like the Unix command script(1), using a pseudo-terminal to record all input and output of a terminal session in a “typescript”.

import argparse import os import pty import sys import time parser = argparse.ArgumentParser() parser.add_argument('-a', dest='append', action='store_true') parser.add_argument('-p', dest='use_python', action='store_true') parser.add_argument('filename', nargs='?', default='typescript') options = parser.parse_args() shell = sys.executable if options.use_python else os.environ.get('SHELL', 'sh') filename = options.filename mode = 'ab' if options.append else 'wb' with open(filename, mode) as script: def read(fd): data = os.read(fd, 1024) script.write(data) return data print('Script started, file is', filename) script.write(('Script started on %s\n' % time.asctime()).encode()) pty.spawn(shell, read) script.write(('Script done on %s\n' % time.asctime()).encode()) print('Script done, file is', filename) 

Table of Contents

Источник

Оцените статью