Skip to content

Commit f953750

Browse files
authored
First Release (#1)
Got CI working, hopefully. Basic functionality is in-place for initial use and tests are passing.
1 parent 22b547a commit f953750

6 files changed

Lines changed: 38 additions & 15 deletions

Functions/Enter-PythonVirtualEnvironment.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Enter-PythonVirtualEnvironment([string]$Name){
88
foreach ($environmentPath in $virtualEnvs){
99
$environmentName = $environmentPath.Name
1010
if ($environmentName.StartsWith($Name)){
11-
Write-Host "To exit the virtual environment execute the command 'deactivate'."
11+
Write-Host "To exit the virtual environment execute the command, 'deactivate'."
1212
Invoke-Expression -Command "$virtualenvRoot\$environmentName\Scripts\Activate.ps1"
1313
return
1414
}

PythonPowershellUtilities.psd1

8.67 KB
Binary file not shown.

PythonPowershellUtilities.psm1

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,3 @@ function Import-Function{
1818

1919
# Have to dot source the function call, otherwise the function are only loaded in the function scope (and not visible in to the module)
2020
. Import-Function -folderPath $script:ScriptDir
21-
22-
Export-ModuleMember -Function New-PythonInstallation
23-
Export-ModuleMember -Function Get-InstalledPythonVersions
24-
Export-ModuleMember -Function New-PythonVirtualEnvironment
25-
Export-ModuleMember -Function Enter-PythonVirtualEnvironment
26-
Export-ModuleMember -Function Remove-PythonVirtualEnvironment
27-
Export-ModuleMember -Function Get-PythonUtilitiesConfigValue
28-
Export-ModuleMember -Function Set-PythonUtilitiesConfigValue
29-
Export-ModuleMember -Function Restore-PythonUtilitiesConfigDefaults

README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
1+
[![Build Status](https://healthcatalyst.visualstudio.com/Clinical%20Decision/_apis/build/status/Text/Python%20Powershell%20Utilities?branchName=development)](https://healthcatalyst.visualstudio.com/Clinical%20Decision/_build/latest?definitionId=1055&branchName=development)
2+
13
# Python Powershell Utilities
24

35
A powershell module for installing python versions and managing python virtual environments.
46

57
## Overview
6-
This module makes it easy to download multiple versions of python into single configurable location. It also makes it easy to create and manage virtual environments that point to any of those installations. For example, to install Python version 3.7.2, create a virtual environment, and then activate that environment you would run
8+
This module makes it easy to download multiple versions of python into a single configurable location. It also makes it easy to create and manage virtual environments that point to any of those installations. For example, to install Python version 3.7.2, create a virtual environment, and then activate that environment you would run
79

810
```powershell
911
New-PythonInstallation 3.7.2
1012
New-PythonVirtualEnvironment -Version 3.7.2 -Name MyEnvironment
1113
Enter-PythonVirtualEnvironment MyEnvironment
1214
```
1315

14-
By default this tool appends the python version to every environment name. So the code above would create a virtual environment named `MyEnvironment-3.7.2`. However, when activating a virtual environment it is only necessary to include as much of the name as you need to uniquely identify your environment since behind the scenes the appropriate virtual environment is found using `EnvironmentName.StartsWith()`.
16+
By default this tool appends the python version to every environment name. So the code above would create a virtual environment named `MyEnvironment-3.7.2`. However, when activating a virtual environment it is only necessary to include as much of the name as you need to uniquely identify your environment since behind the scenes the appropriate virtual environment is found using `EnvironmentName.StartsWith()`.
17+
18+
**See the Wiki tab for an API reference.**
19+
20+
21+
## Installation Locations
22+
By default `New-PythonInstallation` will add a new sub-directiry inside `C:\PythonInstallations` called `python<version>` where version is the major.minor.patch python version. For example, calling `New-PythonInstallation 3.6.8` would create the new directory `C:\PythonInstallations\python3.6.8`. Similarly, `New-PythonVirtualEnvironment` will create new environments in `C:\PythonVirtualEnvironments` by default. For example, calling `New-PythonVirtualEnvironment -Name MyEnvironment -Version 3.7.1` will create a new virtual environment named `MyEnvironment-3.7.1` in `C:\PythonVirtualEnvironments` (which can then be activated by calling `Enter-PythonVirtualEnvironment MyEnvironment`).
23+
24+
### Changing the Defaults
25+
Both of these default locations can be changed by calling another function in this module `Set-PythonUtillitiesConfigValue` which takes two arguments, `-Key` and `-Value`. Here is a list of valid configuration keys which can be set using this function.
26+
27+
|Key |Default Value | Description |
28+
|----------------------|------------------------------|-------------|
29+
|PythonInstallRoot |C:\PythonInstallations\ | The directory into which new python versions will be installed.|
30+
|VirtualEnvironmentRoot|C:\PythonVirtualEnvironments\ | The directory into which new virtual environments will be placed, and where the utility will look for environments when `Enter-PythonVirtualEnvironment` is called.|
31+
32+
33+
So to set the directory where new virtual environments are created to `C:\Users\my.user\Developer`, use
34+
```powershell
35+
Set-PythonUtilitiesConfigValue -Key VirtualEnvironmentRoot -Value "C:\Users\my.user\Developer"
36+
```
37+
Similarly, to obtain the current value for a configuration key, use `Get-PythonUtilitiesConfigValue` which takes one argument, `-Key`. The defaults are stored in a JSON file in the module directory. `Get-PythonUtilitiesConfigValue` and `Set-PythonUtilitiesConfigValue` simply read and write values to and from this file.
38+
39+
40+
It is recommended that if you wish to change the defaults, you do so before using the utilities to install python versions or create virtual environments. If you install python versions into one directory and then change the default installation directory, the utility will forget that those versions were installed, since they are not present in the new directory, and you will need to re-install those versions again in the new directory in order to create new virtual environments using those installations. Similarly, if you change the default virtual environment directory after creating virtual environments in the old directory, the old virtual environments will still exist, but cannot be accessed by calling `Enter-PythonVirtualEnvironment` since the utilities are now pointed at the new directory.
41+
42+
## Why Can't I Use this Tool to Install Version 2.x?
43+
For python version 3.x the python development team provides several installers for windows, one of which is an executable installer that can be run from the command line / powershell. This is the installer that these utilities use. However, for version 2.x the development team only offers a gui-based MSI installer which cannot be run from Powershell, as far as I am aware. So in order to keep things simple, and in light of the fact that most people consider python version 2.x to be deprecated anyway, these utilities only support version 3.x.
44+
45+
## Uninstalling Python Versions
46+
There is currently no function in these utilties for uninstalling python versions. This is due to the fact that while the executable installers make it easy to uninstall from the GUI, there is no obvious way to use the executable installer to uninstall python from a powershell script. If you wish to uninstall a previously-installed version of python there are two paths. First, if you chose not to delete the installer executable after running `New-PythonInstallation` you can open the installer for the version of python that you wish to uninstall and following the uninstall options in the UI. If the installer executables where not deleted during installation they can be found in the directory where the `PythonPowershellUtilities` module was installed, which by default is `C:\Program Files\WindowsPowerShell\Modules\`. The executables will be in the `Functions` directory inside the module.

Tests/ConfigHelpers.tests.ps1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
Describe "Getting and Setting Config Values" -Tag "Unit" {
3-
. "$PSScriptRoot\..\Functions\Get-PythonUtilitiesConfigValue.ps1"
4-
. "$PSScriptRoot\..\Functions\Set-PythonUtilitiesConfigValue.ps1"
3+
Import-Module "$PSScriptRoot\..\PythonPowershellUtilities.psm1" -Force
54

65

76
$testValue = "42"

Tests/IntallationAndVirtualenv.tests.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Describe "Installing new python versions and creating virtual environments" -Tag "Unit" {
22
Import-Module "$PSScriptRoot\..\PythonPowershellUtilities.psm1" -Force
33

4+
45
New-PythonInstallation -Version 3.7.2 -YesToAll | Out-Null
56
$installRoot = Get-PythonUtilitiesConfigValue "PythonInstallRoot"
67
It 'Should create a new python 3.7.2 installation' {
@@ -15,7 +16,7 @@ Describe "Installing new python versions and creating virtual environments" -Tag
1516
}
1617

1718
Enter-PythonVirtualEnvironment -Name $venvName | Out-Null
18-
pip install toolz
19+
pip --disable-pip-version-check install toolz
1920
It 'Should install the test dependency into the new venv' {
2021
Test-Path -Path "$venvRoot\$venvName-3.7.2\Lib\site-packages\toolz" | Should -BeTrue
2122
}

0 commit comments

Comments
 (0)