Tuesday, February 1, 2022

[SOLVED] working with virtualenv without Python in Windows path

Issue

I have to do some work with a company with security restrictions on their computers.

I need to set Python2.7 virtualenv on their Windows10 machine but can't add python to Windows path. I installed Python through the Windows Software Centre. The interpreter is in usual C:\Python27\python.exe but it is not added to Windows path. When I run python in CMD it is not recognizable although C:\Python27\python opens the interpreter.

The problem is that to add it to Windows path I need admin privileges. It is simply not possible. I know the obvious answer is to contact admin but again it is not an option.

So the problem is, having this setup I need to install virtualenv, inside create all my environment and work on it. I can't find the way to do it without Python in the path.


Solution

I usually install several system-wide pythons versions on my windows boxes with the only purpose of creating virtualenvs and that's more than ok. In fact, i strongly recommend to avoid polluting PATH with system-wide python paths at all. Using just virtualenvs is really a good way to go.

That said, if you're creating virtualenvs manually (ie: not using any IDE/plugin to manage this process for you) you can just do it on the command prompt like this:

> cd c:\python27\Scripts
> pip install virtualenv

and then just using c:\python27\Scripts\virtualenv.exe to create your new virtualenvs, for instance, something like this would do it:

> c:\python27\Scripts\virtualenv --python=c:\python27\python.exe foo_venv

That said, if it's more convenient for you to have virtualenv executable reachable from the console (ie: where virtualenv finds its path) you can just add it temporarily to your console session with something like:

SET PATH=%PATH%;C:\Python27\Scripts

Or even better, just create a shortcut of one bat file which spawns your favourite console (cmd prompt, conemu, ...) setting PATH on that particular session, that way you don't need to persist the variable PATH with admin privileges.



Answered By - BPL
Answer Checked By - Katrina (WPSolving Volunteer)