Shai Efrati
Shai Efrati

Reputation: 524

Using a Python virtualenv on a MS-Windows machine

I took over a project created by a colleague of mine on a shared MS-Windows laptop. The project was written in PyCharm and my colleague created a virtualenv for it.

When running the code in the Command Prompt the virtualenv's site-packages, even though the virtualenv was activated, aren't accessible. I played around with it and discovered that if i'm in venv\Scripts then the virtualenv works as expected.

So, the question is - how does one sets the active working directory/path for a virtualenv on a MS-Windows machine?

(venv) C:\Users\George\Documents\Simulation>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin']

vs

(venv) C:\Users\George\Documents\Simulation\venv\Scripts>python
Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:30:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print(sys.path)
['', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Users\\George\\Documents\\Simulation\\venv\\DLLs', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib\\plat-win', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib\\lib-tk', 'C:\\Users\\George\\Documents\\Simulation\\venv\\Scripts', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs', 'C:\\Python27\\Lib\\lib-tk', 'C:\\Users\\George\\Documents\\Simulation\\venv', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib\\site-packages', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib\\site-packages\\win32', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib\\site-packages\\win32\\lib', 'C:\\Users\\George\\Documents\\Simulation\\venv\\lib\\site-packages\\Pythonwin']

And also:

(venv) C:\Users\George\Documents\Simulation\venv\Scripts>where python
C:\Users\George\Documents\Simulation\venv\Scripts\python.exe
C:\Python27\python.exe

(venv) C:\Users\George\Documents\Simulation>where python
C:\Python27\python.exe

And path:

(venv) C:\Users\George\Documents\Simulation>path
PATH=C:\Users\general\Desktop\shared\Simulation\venv\Scripts;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Users\George\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\bin;

(venv) C:\Users\George\Documents\Simulation\venv\Scripts>path
PATH=C:\Users\general\Desktop\shared\Simulation\venv\Scripts;C:\Python27\;C:\Python27\Scripts;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\WINDOWS\System32\OpenSSH\;C:\Users\George\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\bin;

Upvotes: 3

Views: 99

Answers (1)

Udi
Udi

Reputation: 30472

Your venv/scripts/activate.cmd is probably incorrect, adding

C:\Users\general\Desktop\shared\Simulation\venv\Scripts\

To your path environment variable instead of

C:\Users\George\Documents\Simulation\venv\Scripts\

Edit activate.cmd and fix the line starting with set path=... accordingly.

(This was probably the result of creating the virtualenv while this directory was "mounted" in a different location than it currently resides.)

Upvotes: 1

Related Questions