open a sourced terminal from python - python

I'm trying to run a script which opens a new terminal for the user which is sourced from a different file.
My code looks like this at the moment:
os.system("gnome-terminal 'bash -e -c \"source " + path_to_file + "\"'")
Yes it does open a console which also stays open but what I want is also that the console is sourced to the specific file.
I also did a test with running a command with the sourced information dirctly in the above line and it worked. But if I try to start it and do the same it dosn't work.
I hope someone can tell me how I can make it that the terminal will open to the user and that it is sourced.

Related

Run commands in newly opened terminal

I want to open a new terminal through python. In that newly spawned terminal I want to execute commands. I've checked multiple posts which are suggested through similar questions but I couldn't find the answer.
I've tried this:
import os
os.system("gnome-terminal -e 'cd /home/john/Documents; echo 'writing to file' > testfile.txt'")
The new terminal spawns but the commands don't get executed in the new terminal.
❯ python3 TestOpenTerminal.py
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# Error: Failed to execute child process “cd” (No such file or directory)
How can I do this? Thx
The cd command don't work with gnome-terminal command, you need to use the option --working-directory=/path/to/dir.
The -e option is deprecated, you need to use -- followed by the command, like gnome-terminal -- 'command'.
To manipulate files the only way I could make work is using bash and option -c before the command.
So the command you want to do is:
gnome-terminal --working-directory=/home/john/Documents -- bash -c "echo 'writing to file' > testfile.txt"
I really recommend you to use the fabric library to use with terminal commands, it works with all systems and returns the output to you.
I don't know if you really want to open a terminal, but you could just open the file through python and write with it. Try to use the open() and write() functions.
Look closely at your command. Your opening single quote is closed by the single quotes around 'writing to file' and that causes a mess.
os.system("gnome-terminal -e 'cd /home/john/Documents; echo 'writing to file' > testfile.txt'")
Also, the default behavior of gnome-terminal is to close when complete. You can keep it open by editing the profile as follows...
In gnome-terminal, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command

How to make Windows execute my Python script files when I click them

I have made a Python program that uses output.to_csv('results.csv'). When I run it in Spyder it creates that CSV file. But when I double click on it nothing happens. Cmd appears and closes but nothing on the folder, no results.csv.
What am I missing? What more do I have to do?
Run the program from the command line itself instead of double-clicking the .py file.
I assume you are on Windows since you mention CMD. First, cd into the directory containing your program. Then, run python <program>.py or python3 <program>.py depending on your installation.
This time, you will see any output or error messages that appear in CMD without it immediately closing.
If the .csv file really exists, you should be able to go to your File Explorer and find the file at the top of the "Quick Access" section. Right-click the file and hover over "Open With >". Then select Notepad and a notepad will open up showing your results.
If you do not see the file, then try running your program on the command prompt (for Windows):
Press the windows key and type "cmd" in the search bar.
Choose "Command Prompt"
Go to the dir of your program using the cd command
Type python <program name>.py
If there are no errors, follow the steps in the first paragraph.
Ok i guess windows is not recommended at all for this type of tasks. I mean running something simple as create such file is like trying to kill the Lernaean Hydra.
What i did is i just runned it with anaconda prompt and it worked sweet! Thanks for help. Thanks to all!
PS: I'm seriously considering changing to Linux after this
For anyone having the same problem, but have anaconda installed. 1) Open Anaconda Prompt, 2) use cd (1 space) then adress of the folder which contains your py program (eg. cd C:\Users\Bernie\Desktop\tasos) and hit enter, 3) on the next line that appears type: python program_name.py, 4)Hit enter, 5)success!

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?
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:
pi#raspberrypi:~ $
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.

Bash script: Open Python, execute some lines, then let user take control

I have a Python program, A.py, that creates binary data upon completion. To help users analyze the output, I want to add a small script, B.sh, to the output directory that fires up a Python console and executes some commands, C, that load the data and prepare them such that a user sees what is available. After executing C, the script B.sh should keep the Python console open.
First attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python
opens a Python console and keeps it open but doesn't execute anything within that console.
Second attempt at B.sh:
I figured out that
#!/bin/sh
xterm -e python -i C.py
executes C.py (I'd prefer not to have to write an additional file for the startup commands, but I could live with that) and keeps the window open, but doesn't show what was done. More specifically, the user would be presented with the outputs of C, but not the command that were used to achieve the outputs.
Instead,I'd like the user to be presented with a console like this:
>>> [info,results] = my_package.load(<tag>)
>>> my_package.plot(results)
>>> print(info)
<output>
>>> my_package.analyze(results)
<output>
>>>
Save this in a file called demo.tcl
#!/usr/local/bin/expect -f
# Spawn Python and await prompt
spawn /usr/local/bin/python3
expect ">>>"
# Send Python statement and await prompt
send "print('Hello world!')\n"
expect ">>>"
# Pass control to user so he can interact with Python
interact
Then make it executable with:
chmod +x demo.tcl
And run with:
xterm -e ./demo.tcl
In the picture, you can see I went on after the "Hello world" to print the system version info.
Your paths for Python and expect may be different, so check and alter to suit.
For anyone who happens to be using macOS (a.k.a. OSX), you can install expect with homebrew as follows:
brew install expect
And, since Macs don't ship with X11 any more, rather than install XQuartz and run xterm, you can start a new Terminal and run the Python in there quite simply with:
open -a Terminal.app demo.tcl
As suggested by user #pask I simply printed the commands before executing them.
Furthermore, I added a -c switch to be able to put the Python commands directly in B.sh instead of having to write a file C.py
Here is the B.sh I am using now:
#!/bin/sh
xterm -e python -i -c "print('>>> import my_package');import my_package;print('>>> [info,results] = my_package.load()');[info,results] = my_package.load()"

most of terminal commands do not work

I am new to python and OSX terminal commands. I wanted to permanently add a directory to PYTHONPATH, thus I looked here and used these instructions:
1-Open up Terminal
2-Type open .bash_profile
3-In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar
4-Save the file, restart the Terminal, and you're done
I also followed this instruction:
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
After these changes I cannot use any command like python, which, whereis, pip, etc. in Terminal :(
The response is something like below:
-bash: which: command not found
I do not know what I have done.
are you sure you have appended the path because what i can see is you have replaced old path definition and assigned a new one
remove bash profile use one of these commands /bin/rm ~/.bash_profile Or if you want to save your current bash configs then use a mv instead /bin/mv ~/.bash_profile ~/bash_profile Then close your terminal and start again you will be able to use all commands and apps

Categories