Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adamdriscoll/snek
PowerShell wrapper around Python for .NET to invoke Python from PowerShell
https://github.com/adamdriscoll/snek
hacktoberfest powershell python
Last synced: about 2 months ago
JSON representation
PowerShell wrapper around Python for .NET to invoke Python from PowerShell
- Host: GitHub
- URL: https://github.com/adamdriscoll/snek
- Owner: adamdriscoll
- License: mit
- Created: 2018-01-03T21:31:06.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T16:04:59.000Z (about 1 year ago)
- Last Synced: 2024-11-09T06:11:33.981Z (2 months ago)
- Topics: hacktoberfest, powershell, python
- Language: PowerShell
- Homepage:
- Size: 423 KB
- Stars: 137
- Watchers: 12
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [Snek](https://www.reddit.com/r/Snek/)
## PowerShell wrapper around [Python for .NET ](https://github.com/pythonnet/pythonnet) to invoke Python from PowerShell
![](./snek.jpg)
## Install snek
```
Install-Module snek
```## Requirements
* Python v3.7, v3.8, v3.9, v3.10, or v3.11 (defaults to python 3.11)
## Functions
* Use-Python
* Invoke-Python
* Import-PythonRuntime
* Import-PythonPackage
* Install-PythonPackage
* Uninstall-PythonPackage
* Use-PythonScope
* Set-PythonVariable### Invoke Python Code (v3.11)
```
Use-Python {
Invoke-Python -Code "print('hi!')"
}
hi!
```### Invoke Python Code (v3.7)
```
PS > Use-Python {
Invoke-Python -Code "print('hi!')"
} -Version v3.7
hi!
```### Returning A Value from Python to PowerShell
Due to the use of `dynamic` the type must be cast to the expected type so you need to specify the `-ReturnType` parameter to do so.
```
Use-Python {
Invoke-Python "'Hello'" -ReturnType ([String])
}
```### Imports the `numpy` Python module and does some math
Access methods of modules directly!
```
Use-Python {
$np = Import-PythonPackage "numpy"
[float]$np.cos($np.pi * 2)[float]$np.sin(5)
[float]($np.cos(5) + $np.sin(5))
} -Version v3.7
```Output
```
1
-0.9589243
-0.6752621
```### Manage pip
Format is `Install-PythonPackage `
```
Install-PythonPackage requests
```Or similarly:
```
Uninstall-PythonPackage requests
```### Using Scopes
You can use Python scopes to string together multiple `Invoke-Python` calls or to pass in variables from PowerShell.
```
Use-Python {
Use-PythonScope {
Invoke-Python -Code "import sys"
Invoke-Python -Code "sys.version" -ReturnType ([string])
}
}
```### Passing a .NET Object to Python
```
class Person {
[string]$FirstName
[string]$LastName
}Use-Python {
Use-PythonScope {
$Person = [Person]::new()
$Person.FirstName = "Adam"
$Person.LastName = "Driscoll"
Set-PythonVariable -Name "person" -Value $PersonInvoke-Python -Code "person.FirstName + ' ' + person.LastName" -ReturnType ([string])
}
}
```