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

16
  • The ~ won't be expanded unless you pass shell=True to subprocess.run. Commented Jun 18, 2020 at 18:09
  • @textux Thanks - you're right of course, but I don't think that's the problem Commented Jun 18, 2020 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? Commented Jun 19, 2020 at 0:16
  • @SeanBreckenridge I've edited the question Commented Jun 19, 2020 at 6:49
  • You normally need gnome-terminal -e bash ... I think you are missing the -e Commented Jun 19, 2020 at 7:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.