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?

1
  • Wouldn't it be easier to write a shell script that creates and activates the virtualenv then runs the script within it for you? Commented Aug 10, 2015 at 13:28

1 Answer 1

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')
Sign up to request clarification or add additional context in comments.

6 Comments

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
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.
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
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.
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?
|

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.