Issue
This is pretty straightforward to activate a virtualenv
from powershell of Windows, by ./venv/Scripts/activate
command, or with an absolute path like below:
href="https://i.stack.imgur.com/zpRMH.png" rel="nofollow noreferrer">
But when I want to execute the same command from a Python script that executes commands in powershell
, virtualenv doesn't activate and I can't run pip install something
commands inside virtualenv. It means that I can't add packages or even upgrade pip inside virtualenv (Surely because it's not activated correctly).
Note
I'm confident about the implementation of the code because it works clearly for other commands. The only problem might be with C:/temp/venv/Scripts/activate
command sent to powershell. Looking for some command like source
in Linux to activate that virtualenv.
Here is my code:
installer.py
script: runs different commands inside powershell with subprocess
, and returns the result.
# installer.py
class Installer:
def run(command):
# Some code here
proc = subprocess.Popen(
[ 'powershell.exe', command ],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Some code here
install.py
script: sends commands to the Installer class
# install.py
from installer import Installer
installer = Installer()
installer.run('C:/temp/venv/Scripts/activate')
Solution
SOLUTION
Turned out I didn't need to activate virtualenv. I could easily run pip install
commands with following command sent to subprocess:
installer.run('C:/temp/venv/Scripts/python.exe -m pip install somepackage')
Answered By - Tabaraei Answer Checked By - David Marino (WPSolving Volunteer)