Git Product home page Git Product logo

virtualenvwrapper-powershell's Introduction

VirtualEnvWrapper for Windows Powershell

This is a mimic of the powerfull virtualenvwrapper but for Windows Powershell.

Unless the previous version of my esteemed colleague Guillermo Lòpez equivalent but obsolete it's compatible with Python 2+ and entierly based on a PowerShell script.

Installation

Just use the Install.ps1 script:

./Install.ps1

and the script will create required path if needed and install the profile.ps1 file directly to automaticly activate VirtualEnvWrapper when the shell is opened

Manual Installation

Put the file VirtualEnvWrapper.psm1 into the directory ~\Documents\WindowsPowerShell\Modules. Edit or create the file ~\Documents\WindowsPowerShell\Profile.ps1 (see ) and add into the lines below :

$MyDocuments = [Environment]::GetFolderPath("mydocuments")
Import-Module $MyDocuments\WindowsPowerShell\Modules\VirtualEnvWrapper.psm1

Location

The virtual environments directory is set into your personnal directory : ~/Envs

Where ~ is your personnal directory.

If you want to set your environment. Just add and variable environment called :

WORKON_HOME (as in Unix/Linux system).

Usage

The module add few commands in Powershell :

  • lsvirtualenv (alias: Get-VirtualEnvs) : List all Virtual environments
  • mkvirtualenv (alias: New-VirtualEnv) : Ceate a new virtual environment
  • rmvirtualenv (alias: Remove-VirtualEnv) : Remove an existing virtual environment
  • workon: Activate an existing virtual environment
  • Get-VirtualEnvsVersion: to display the current version.

Create a virtual environment

To create a virtual environment just type:

MkVirtualEnv -Name MyEnv -Python ThePythonDistDir

where MyEnv is your environment and ThePythonDistDir is where the python.exe live. For example:

MkVirtualEnv -Name MyProject -Python c:\Python36 

will create and virtual environment named MyProject located at ~\Envs with the Python 3.6 distribution located at C:\Python36

If the -Python option is not set, the python command set in your path is used by default.

Options are:

  • -Name : The new environment name
  • -Packages or -i : Install packages separated by a coma (Note: this differs from original virtualenvwrapper )
  • -Associate or -a: Still todo
  • -Requirement or -r: The requirement file to load.

If both options Packages and Requirement are set, the script will install first the packages then the requirements as in original Bash script.

List virtual environments

Type

LsVirtualEnv

in a Powershelll to display the entiere list with the Python version.

For Example:

Python Virtual Environments available

Name                          Python version
====                          ==============
TheProjectIHave               3.6.3

Activate a virtual environment

Type

workon TheEnvironment

in a console. The PS command line starts now with:

(TheEnvironment) C:\Somewhere>

to show you what is the default

To ensure that the Python environment is the good one type:

Get-Command python

The path should be:

~\Envs\TheEnvironment\Scripts\python.exe

Leave from a virtual environment

Just type deactivate as usual (Python default).

Todo

  • Activate the autocompletion
  • Set the virtualenvwrapper options into system environment variables (see the main project)

Development

A script InstallDev.ps1 exists to simplify the development. Invoke it with:

$ .\InstallDev.ps1 

will unload VirtualEnvWrapper.ps1 from memory and reload it.

virtualenvwrapper-powershell's People

Contributors

atien-panopto avatar dbellandi avatar lepsalex avatar marcinbarczynski avatar mmorys avatar rbierbasz avatar regisf avatar tsgagnon avatar vmiheer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

virtualenvwrapper-powershell's Issues

Support paths with spaces

I launched lsvirtualenv and got this:

image

It happens because my username contains space. Please, add support for paths with spaces

Create new release with Issue #30 fix?

The wrapper works well with the Issue #30 (-Force) fix, but it appears the current release does not incorporate the fix? A new release that includes the fix would be helpful for anyone downloading the latest release since it doesn't reflect the current code and the Issue #30 fix needs to be made manually if using the current release.

Python path contains (x86)

Attempting to run mkvirtualenv __name_of_env__ when the python path contains "Program Files (x86)" results in an error: The term 'x86' is not recognized as the name of a cmdlet ....

I have a branch with a fix and can open a PR if you'd like, diff output below:

diff --git a/VirtualEnvWrapper.psm1 b/VirtualEnvWrapper.psm1
index f3b43dd..76d210d 100644
--- a/VirtualEnvWrapper.psm1
+++ b/VirtualEnvWrapper.psm1
@@ -73,7 +73,7 @@ function Get-PythonVersion($Python) {
         return
     }

-    $python_version = Invoke-Expression "$Python --version 2>&1"
+    $python_version = Invoke-Expression "& '$Python' --version 2>&1"
     if (!$Python -and !$python_version) {
         Write-Host "I don't find any Python version into your path" -ForegroundColor Red
         return
@@ -130,7 +130,7 @@ function New-Python3Env($Python, $Name) {
         $PythonExe = Join-Path (Split-Path $Python -Parent) "python.exe"
     }

-    $Command = "$PythonExe -m venv"
+    $Command = "& '$PythonExe' -m venv"

     Invoke-CreatePyEnv $Command $Name
 }

ERROR: The path to access to python doesn't exist. Python directory = C:\Python27amd64\python.exe

Hey man sorry to bother you but i get another error while making use of mkvirtual env

C:\Users\frank> mkvirtualenv -Python "C:\Python27amd64\python.exe"  -Name hola

  ERROR: The path to access to python doesn't exist. Python directory = C:\Python27amd64\python.exe

By the way, if would be awesome if the wraper searched for the virtualenvs in the path so one could just give python.exe to the -Python keyword.

MacOS Compatibility

I'm working on modifying this to work with MacOS/cross-compatible pwsh 7. Just wondering if anyone else has started this yet in case I'm spinning my wheels.

[Bug] [Windows] After activating the virtual environment, it doesn't change where pip points at.

More information:

python(this works)

> where.exe python*.exe
C:\Users\lasersPew\Envs\web\Scripts\python.exe
C:\Users\lasersPew\Envs\web\Scripts\pythonw.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\python.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\python3.11.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\python3.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\pythonw.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\pythonw3.11.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\pythonw3.exe

pip(this doesn't work)

C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\pip.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\pip3.11.exe
C:\Users\lasersPew\AppData\Local\Microsoft\WindowsApps\pip3.exe

Software Information:
Windows 11
tried both in VSCode and Windows Terminal

Unable to create process --

[Update: the problem I describe here was caused by something corrupt in the directory where I made envorenments. I chaged WORKON_HOME to point to a new, empty, directory and the problem went away]

I was having problems with environments created by "python -m venv" not giving me a working pip, and found virtualenvwrapper-powershell and it worked for me. Sometime last week I had this update from microsoft to Windows 10:

https://support.microsoft.com/en-us/help/4551762/windows-10-update-kb4551762

Could be a coincence, but now old woes are back and show in virtualenvwrapper also:

PS C:\Users\pa2> Get-VirtualEnvVersion
Version 0.2.0
PS C:\Users\pa2> New-VirtualEnv uvula
Creating virtual env...
. : The term 'C:\py\venvs\uvula\Scripts\activate.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At C:\Users\pa2\Documents\WindowsPowerShell\Modules\VirtualEnvWrapper.psm1:105 char:7
+     . $ActivatepPath
+       ~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\py\venvs\uvula\Scripts\activate.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


  SUCCESS: uvula virtual environment was created and your're in.

PS C:\Users\pa2> C:\py\venvs\uvula\Scripts\Activate.ps1
(uvula) PS C:\Users\pa2> pip install requests
Fatal error in launcher: Unable to create process using '"c:\py\venvs\uvula\scripts\python.exe"  "C:\py\venvs\uvula\Scripts\pip.exe" install requests'

I believe problem is that "activate.ps1" and not "Activate.ps1" -- I will try creating symlinks but have been down similar road before with '-m venv' and some other manager. There seems to be something very sensitive about my home pc, have had similar problems with 3.6, 3.7 and 3.8, started whole new user accounts to try to fix.

Suggestion: Use py instead of probing python.exe manually

Python for Windows usually comes with the py wrapper executable. Launching py will start the highest available version of Python, py -2 the highest available 2.x version, py -3.6 Python 3.6.x if installed and so on. This can simplify your venv creation functions.

"workon" command has no effect after second call

my system: windows 10:

> [System.Environment]::OSVersion.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
10     0      19041  0

powershell version: Version : 5.1.19041.1023

I use pyenv-win to install: 3.8.10

And put the "VirtualEnvWrapper.psm1" at: "$mydocuments\WindowsPowerShell\Modules".
Added these to Profile.ps1:

$PowerShellPath = Split-Path $PROFILE.CurrentUserAllHosts
Import-Module $PowerShellPath\Modules\VirtualEnvWrapper.psm1

Restarted powershell, execute mkvirtualenv t1, I can enter (t1) virtualenv.
then execute deactivate, virtualenv is deactivated, all is good.

Then I type workon t1 again, prompt does not have (t1) prefix, and the python command is still the global one, at this point, if I execute deactivate, this is reported as not recognized.

I have to edit the VirtualEnvWrapper.psm1:
In the function function Workon, change this line:

    Import-Module $activate_path

To this:

    & $activate_path

Now everything works, I can workon t1, deactivate, workon t1, all is good.

Just don't know the root cause, I'm completely noob with powershell.

Load-Module is not recognized

Activating a virtual environment using:

workon venv_name

leads to the following error:

 The term 'Load-Module' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Here is the output of "host" command:

Name             : ConsoleHost
Version          : 5.1.19041.610
InstanceId       : f2763b19-e8bc-49db-8528-ad52b93904d5
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-IN
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

I cannot see any online doc for "Load-Module". What is this command and why is this happening?

Temporary venv is not automatically removed

Hi,
first of all, thanks a lot for creating this neat module.

I am trying to get the mktmpenv command to work, however, I think that the venv hook does not call the generated postdeactivate.ps1 script.

Asides, I changed the postdeactivate-script to

if (Test-Path -Path `"$dest_dir\README.tmpenv`") {
    Write-Host `"Removing temporary environment $uuid`"
    # Change the location else MS Windows will refuse to remove the directory
    Set-Location `"$WORKON_HOME`" 
    Remove-VirtualEnv `"$uuid`"
}

s.t. it correctly removes the tmp env when executed manually in a shell. Yet, the script is not executed when running deactivate within the tmp env.

Maybe you can say where you found documentation or source for the use of postdeactivate scripts with venv?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.