Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating virtualenv in a Python script and running the rest in that env

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?

like image 902
pkm Avatar asked Feb 15 '26 07:02

pkm


1 Answers

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')
like image 156
Aidan Kane Avatar answered Feb 16 '26 19:02

Aidan Kane