0

I'm attempting to automate some of the steps I routinely go through in setting up venv environments in Python. I'm able to get to where I create the folder and a requirements.txt folder inside, containing any modules I desire. But it seems like the session doesn't remember the last command, preventing me from activating the venv and installing the requirements.txt file afterwards.

# Navigate to new venv directory
subprocess.run("cd " + destination, shell=True)
# Activate venv environment.
subprocess.run("Scripts\\activate.bat", shell=True)
# Install requirements with pip.
subprocess.run("pip install -r requirements.txt", shell=True)

Output:

The system cannot find the path specified.

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

David Metcalfe
  • 2,237
  • 1
  • 31
  • 44

2 Answers2

0

You should navigate to the folder with requirements.txt after activating env, this file definitely not in the environment folder.

vZ10
  • 2,468
  • 2
  • 23
  • 33
  • I don't quite follow what you mean, but `requirements.txt` absolutely is in the venv folder. Inside, there is `Include`, `Lib`, `Scripts` and `requirements.txt`. That much of the script works without fail. – David Metcalfe Jun 13 '17 at 19:53
  • So you created virtualenv, after that went into it put requirements.txt inside? – vZ10 Jun 14 '17 at 04:40
  • Once the `venv` folder is created, the requirements.txt is created and populated, yes. The parts after this are where I'm having issues. – David Metcalfe Jun 14 '17 at 05:13
0

Instead of navigating to the folder and attempting to install with pip, I called pip inside the venv Scripts folder and installed the packages which were already appended to a list when I was writing to requirements.txt.

# Install requirements to venv.
subprocess.run([os.path.join(destination, 'Scripts', 'pip.exe')] + 'install {}'.format(' '.join(requirements)).split())
David Metcalfe
  • 2,237
  • 1
  • 31
  • 44