Issue
Before I get downvotes, this is not a duplicate. I have exhausted both google and SO. In SO, there are questions regarding how Python modules can be installed. But my question is creating a venv
and installing modules to this created venv
programatically. I have tried a few things;
My sample code
def create_venv_install_requirements(venv_folder, filename):
print(f'Creating a new virtual environment')
virtualenv.create_environment(venv_folder)
activate_file = os.path.join(venv_folder, 'Scripts', 'activate_this.py')
print('Installing requirements')
with open(filename) as f:
requirements = f.readlines()
for item in requirements:
exec(open(activate_file).read(), globals())
subprocess.call(f'pip install {item}')
# pip.main('install', item) this does not work as well
The issue I am facing is I can successfully create a venv
but the modules cannot be installed within the created venv
, instead they are being installed system wide. How can install packages within the activated venv
?
Solution
It sounds like your script is calling the wrong pip
despite the fact that you're activating it. You can explicitly call pip
in the virtual environment (even without activating it like) this:
subprocess.call('{venv_folder}/bin/pip install {item}')
But don't actually do that, because if you have a list of requirements in a file you should just call:
subprocess.call('{venv_folder}/bin/pip install -r {filename}')
...and of course, in this case you don't need to open the requirements file and iterate over it yourself.
Note: For Windows systems, replace bin
with Scripts
Answered By - larsks