Forums

Cannot access 'workon' command from within a python script

I am trying to activate a virtualenv and run a script from within another python script. It would look something like this:

import os
os.system('workon myvirtualenv')
os.system('python myscript.py')

However, when I try to run this, I get the error: sh: 1: workon: not found

Is there a way to access the 'workon' command from within another python script or a way to specify that I want a file to be run within a virtualenv and not the home directory?

You need to have a full shell set up in order to do that, and the workon and the call to python would need to be in the same command so that the former can affect the latter. The subprocess module is a good way to get both of those effects. So the code would be:

import subprocess
subprocess.check_call("workon myvirtualenv; python myscript.py", shell=True)

This did not solve the issue. This code yields the error: /bin/sh: 1: workon: not found

Make sure that you still have the following lines in your .bashrc. If you have removed them, then virtualenvwrapper would not be initialised.

export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

This is the current state of my .bashrc file:

# Load up standard site-wide settings.
source /etc/bashrc

#remove duplicate entries from history
export HISTCONTROL=ignoreboth

# Show current git branch in prompt.
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_GRAY="\[\033[0;37m\]"

PS1="$LIGHT_GRAY\$(date +%H:%M) \w$YELLOW \$(parse_git_branch)$LIGHT_GREEN\$ $LIGHT_GRAY"

# Load virtualenvwrapper
source virtualenvwrapper.sh &> /dev/null

export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Adding the lines you suggested still did not resolve the issue. This is full error I'm getting:

/bin/sh: 1: workon: not found
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    subprocess.check_call("workon myvirtualenv", shell=True)
  File "/usr/lib/python3.8/subprocess.py", line 364, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'workon myvirtualenv' returned non-zero exit status 127.

You don't need to source virtualenvwrapper.sh twice. Did you try in a new console after making changes in .bashrc?

I removed the second source virtualenvwrapper command and tested it in a new console and still getting the same issue. I found a workaround by just having my python script execute a separate bash script that runs the workon command:

1
2
3
4
5
#!/bin/bash
source virtualenvwrapper.sh

workon myvirtualenv
echo "Activated myvirtualenv"

Ideally I would be able to do this without having to create the separte bash file though.

You could also try, instead of activating the venv, running the "subscript" with the python executable from the venv (if you run which python when you have the venv activated, it will show you the full path to it). Than you could probably use os.system('/path/to/venv/python myscript.py'). I understand that there are reasons you can't run the "upper" script in the venv on the first place, right?

As per your suggestion I tried:

import os
os.system('/home/autsauce/.virtualenvs/myvirtualenv/bin/python myscript.py')

This code isn't working either as packages I import in my python script that are installed in myvirtualenv are not being found which means it is not actually running the script in the virtual env for some reason. I am wondering if I have messed up my PA configuration somewhere along the way as so many recommendations which seem like they should work are not resolving the issue.

What if you run it normally in the bash console?