-2

I want to activate virtual environment with python, then run some commands such as, (pip install requirements.txt). I want to run following commands with python. How can I do that. Thanks...

C:\directory\myfolder\venv\Scripts\activate
(venv) C:\directory\myfolder> pip install requirements.txt

For example, I tried following codes;

import subprocess
commands = ['C:\directory\myfolder\venv\Scripts\activate', 'pip install requirements.txt']
procs = [subprocess.Popen(i, shell=True) for i in commands]
for p in procs:
    p.wait()

Output: ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'

Halim
  • 61
  • 8
  • does this solve https://stackoverflow.com/questions/6943208/activate-a-virtualenv-with-a-python-script – ArunJose Dec 07 '19 at 09:20
  • In your shell example, the working directory is `C:\directory\myfolder`; that doesn't appear to be the case for when you run your Python script. – chepner Dec 07 '19 at 13:55
  • There's also the issue that you are *executing*, not sourcing, `activate` in a shell that exits immediately after you execute it. – chepner Dec 07 '19 at 13:56

1 Answers1

-1

This works for me:

$ pip install -r requirements.txt --no-index `--find-links` file:///tmp/packages

--no-index - Ignore package index (only looking at --find-links URLs instead).

-f, --find-links <URL> - If a URL or path to an html file, then parse for links to archives. If a local path or file:// URL that's a directory, then look for archives in the directory listing.

or try this

first of all, create a virtual environment

in python 3.6

virtualenv --python=/usr/bin/python3.6 <path/to/new/virtualenv/>

in python 2.7

virtualenv --python=/usr/bin/python2.7 <path/to/new/virtualenv/>

then install all the packages available in the requirement.txt file.

pip install -r <path/to/the/> requirement.txt
Shb8086
  • 5
  • 5