0

I am trying to optimise my workflow and so I want to issue a command from a python program that will open a terminal, switch to a specific directory and then run a command, in this case activate a virtual environment.

This question is similar to this one. However, I am not sure how to handle the call to the virtualenv.

import subprocess

subprocess.run(['gnome-terminal', '--working-directory', '/home/jeff/projects', 'bash \"workon wagtail\" '])

What happens is that the new window get created in the correct directory but the workon does not get called

Working in the terminal, if I use

gnome-terminal --working-directory='/home/jeff/projects'  -- bash -ci "source /usr/bin/virtualenvwrapper.sh && workon ; exec bash;"

The command works, but the workon is performed before the terminal prompt appears

If I run

gnome-terminal --working-directory='/home/jeff/projects'  -- bash -ci "source /usr/bin/virtualenvwrapper.sh && workon wagtail; exec bash;"

the terminal does not enter the venv (wagtail). However the command

workon wagtail

is in the terminal history

Psionman
  • 3,084
  • 1
  • 32
  • 65
  • The `~` won't be expanded unless you pass `shell=True` to `subprocess.run`. – tectux Jun 18 '20 at 18:09
  • @textux Thanks - you're right of course, but I don't think that's the problem – Psionman Jun 18 '20 at 19:21
  • whats currently happening, do you get an error; does a window pop up; does it open but not in the right directory? – Sean Breckenridge Jun 19 '20 at 00:16
  • @SeanBreckenridge I've edited the question – Psionman Jun 19 '20 at 06:49
  • You normally need `gnome-terminal -e bash ...` I think you are missing the `-e` – Mark Setchell Jun 19 '20 at 07:54
  • @MarkSetchell I'm making some progress the command gnome-terminal --working-directory /home/jeff/projects -- bash -c "workon; exec bash" Is almost there, but gives error bash: workon command not found – Psionman Jun 19 '20 at 08:50
  • Cool - have a read here... https://stackoverflow.com/a/34611480/2836621 – Mark Setchell Jun 19 '20 at 09:04
  • @MarkSetchell Thanks. I don't think that's the problem. workon is fine if I call it from the command line, but not in a script. This works: gnome-terminal --working-directory='/home/jeff/projects' -- bash -ci "echo hello; exec bash;" but this doesn't: gnome-terminal --working-directory='/home/jeff/projects' -- bash -ci "workon ; exec bash;" – Psionman Jun 19 '20 at 09:19
  • When working at the command-line, `bash` is run as a login shell and sets up your environment using its startup files. When `bash` is run by `gnome-terminal` it doesn't do that. – Mark Setchell Jun 19 '20 at 09:27
  • @MarkSetchell Thank you. This works: gnome-terminal --working-directory='/home/jeff/projects' -- bash -ci "source /usr/bin/virtualenvwrapper.sh && workon ; exec bash;", but this does not: gnome-terminal --working-directory='/home/jeff/projects' -- bash -ci "source /usr/bin/virtualenvwrapper.sh && workon wagtail; exec bash;". The command is run BEFORE the terminal prompt appears – Psionman Jun 19 '20 at 10:16
  • Cool - you can add it as the answer and accept it and bag the points. Then everyone can see, in a nicely formatted way, how to do it. – Mark Setchell Jun 19 '20 at 10:30
  • @MarkSetchell But it doesn't work! This is the command I want. It runs the 'workon wagtail' before the terminal prompt appears and the venv is not invoked: gnome-terminal --working-directory='/home/jeff/projects' -- bash -ci "source /usr/bin/virtualenvwrapper.sh && workon wagtail; exec bash;" – Psionman Jun 19 '20 at 11:11
  • Oh sorry, I misunderstood your comment. Maybe you could click `edit` under your question and add your latest, greatest code in there where it is nicely formatted and easy for everyone to read rather than wading through the comments, please? – Mark Setchell Jun 19 '20 at 11:14
  • @MarkSetchell Question updated! – Psionman Jun 19 '20 at 11:42
  • I don't understand why you `exec bash`. That will **replace** the current `bash` process in which you have run `workon` with a new `bash` process in which you haven't. Why do you do that? You are already running a perfectly happy `bash` process, surely? – Mark Setchell Jun 21 '20 at 11:29
  • @MarkSetchell If I do not use exec bash then the new terminal disappears. I want a new terminal,wthere I want to run the workon command – Psionman Jun 22 '20 at 07:36

0 Answers0