3

Based on this link I created and activated a virtual environment in a Python script but what I want is after activating the virtualenv the rest of the code should run in the env, install few items and then process.

Code:

#!/usr/bin/python
import commands
import os
import time
import datetime
import sys
import json
import requests

out = commands.getoutput("wget <url>/s.sh")
new = commands.getoutput("chmod 755 s.sh")


env = "virtualenv " + "test"    

#checking if env present if not create and activate it
try:
    l=commands.getoutput('/bin/bash --rcfile s.sh')
except:
    print env + " not present" 
    m = commands.getoutput(env)
    print env + " not present so creating" 
    os.system('/bin/bash --rcfile s.sh')


v = commands.getoutput("which python")
print v + " python to be used"
v = commands.getoutput("pip install wget")

s.sh file code:-

#!/bin/sh
. test/bin/activate

Basically instead of a shell script to create virtualenv, activate it, and run a few steps I want to use python script.

What am I missing? Is it right use case?

Community
  • 1
  • 1
pkm
  • 2,683
  • 2
  • 29
  • 44
  • Wouldn't it be easier to write a shell script that creates and activates the virtualenv then runs the script within it for you? – jonrsharpe Aug 10 '15 at 13:28

1 Answers1

1

I've found the virtual env activation stuff to all be a bit too much like hard work in scripts. In your case you're doing it from python instead of the shell, but I guess it's the same basic principal (haven't seen commands before - are they all running in the same subprocess)?

Instead, I normally just use the full path to the executables in the venv. It's all more explicit, but then again, that is the python way :)

One thing you have to watch out for is permissions.

sudo -u whatever_user /path_to_myvenv/bin/pip install -r /some_path/requirements.txt

Not sure if that's helpful at all, but I'd be looking to call the binary in the venv directly.

EDIT just had a play with commands - each of them is independent (so that's probably the root cause of your issue)

commands.getoutput('pwd')  # /somepath
commands.getoutput('cd /tmp')
commands.getoutput('pwd')  # still /somepath

I'd be looking to do something like:

commands.getoutput('virtualenv test')
commands.getoutput('test/bin/pip install wget')
Aidan Kane
  • 3,856
  • 2
  • 25
  • 28
  • so when os.system('/bin/bash --rcfile s.sh') command is run virtualenv is activated but still print statement after that shows /usr/bin/python as python not /test/bin/python ... any idea – pkm Aug 10 '15 at 13:26
  • Just read in the other question that the `os.system` call will leave the venv activated. I don't know enough about what's going on there, but it's a bit much like shell magic for me. I'd follow the advice of the answer that has more votes instead. – Aidan Kane Aug 10 '15 at 13:32
  • ok i tried subprocess also, there also cd doesn't change dir >>> subprocess.call(['pwd']) /home/test 0 >>> subprocess.call(['cd ..'],shell=True) 0 >>> subprocess.call(['pwd']) /home/test 0 – pkm Aug 10 '15 at 13:38
  • You need to use `subprocess.call(['cd /tmp'], shell=True)` - but you'll see that the directory hasn't actually changed in the next call. – Aidan Kane Aug 10 '15 at 13:42
  • right ? so i have a shell script to create virtualenv, activate it, and run a few steps inside that virtualenv but is it like that we can't do same thing in python? – pkm Aug 10 '15 at 13:45
  • That's not really what you're doing though. You're almost doing step 1, then restarting your computer, then doing step 2 etc. Each time you call `commands.getoutput` things start fresh. I don't really understand the details, but you can get around it by doing what I've specified above. Is there a problem doing it that way? Your entire code can be reduced to 2 calls. – Aidan Kane Aug 10 '15 at 14:59