1

I want to dynamically create, destroy & use Python virtual environments that contain code loaded by pip.

The virtualenvapi Python package looks promising because it provides an install() method that uses pip to install packages. It supports both package name and URL arguments, both of which I need.

However, I would prefer to use venv as I don't care about Python earlier than 3.6, virtual environments are complex, venv is in the standard library but virtualenvapi isn't, and there are good arguments to prefer venv.

venv provides API support with venv.EnvBuilder(). But how does one accomplish this with it:

env = VirtualEnvironment('/path/to/environment/name')
env.install('git+https://github.com/KarrLab/log.git#egg=log')

Thanks

Arthur
  • 525
  • 7
  • 18

1 Answers1

1

Below .py may help your demand.

import virtualenv
import pip
import os

# create and activate the virtual environment
venv_dir = os.path.join(os.path.expanduser("~"), ".venv")
virtualenv.create_environment(venv_dir)
execfile(os.path.join(venv_dir, "bin", "activate_this.py"))

# pip install a package using the venv as a prefix
pip.main(["install", "--prefix", venv_dir, "xmltodict"])

Referance

Edit 1:

Also its including how to use pip with script.

Omrum Cetin
  • 1,320
  • 13
  • 17