Reputation: 337
I am creating a single script for setup and running whole Django project.
venv_parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir))
venv_dir = os.path.abspath(os.path.join(venv_parent_dir, 'fvenv'))
subprocess.run(args=['virtualenv', '-p', 'python3', venv_dir])
os.popen('/bin/bash --rcfile %s'%(venv_dir+'/bin/activate'))
With the above code I created a virtual environment then activate this. Now I want to install the requirements.txt
file in the activated virtual environment
subprocess.run(args=['pip3', 'install', '-r', 'requirements.txt'])
I tried with subprocess
, but it's not installing in the virtual environment, it is installing in the operating system Python.
Upvotes: 3
Views: 438
Reputation: 309029
A the moment, the os.popen
command does not affect the environment that subprocess.run
runs in. That means that your subprocess.run
call is using the system pip3
instead of the pip
from the virtualenv. You can use the pip
from the virtualenv by using the full path:
import os
pip = os.path.join(venv_dir, 'bin', 'pip')
subprocess.run(args=[pip, 'install', '-r', 'requirements.txt'])
By using /path/to/venv/bin/pip
, you don't have to activate the virtual environment first.
Upvotes: 2