Videos
I just started learning Python 3 recently and the course I'm learning it from "Python for Everybody by Dr. Charles Severace". In the lessons, he uses Command Prompt to run his code. I've also heard that Powershell is basically the same thing as CP, just that it can do more. The question is, should I switch over to PS now, or does it not matter when just as long as I get to know how to use both eventually?
I'm a .Net stack developer and know PS very well but I've barely used Python and it seems like Python has been constantly moving towards being the mainstream language for a myriad of things.
I see Microsoft adding it to Excel, more Azure functionality, it's #1 for AI/machine learning, data analysis, more dominate in web apps, and seemingly other cross platform uses.
I've been hesitant to jump into the Python world, but am I wrong for thinking more of my time should be invested learning Python over PowerShell for non-Windows specific uses?
Or how do people familiar with both PS & Python feel about learning the languages and their place in the ecosystem?
Since, you are able to run Python in PowerShell. You can just do python <scriptName>.py to run the script. So, for a script named test.py containing
Copyname = raw_input("Enter your name: ")
print "Hello, " + name
The PowerShell session would be:
Start:
Copycd C:\Python27
python test.py
Session transcript:
CopyEnter your name: Monty Python
Hello, Monty Python
As far as I have understood your question, you have listed two issues.
Problem 1
You are not able to execute the Python scripts by double clicking the Python file in Windows.
Reason
The script runs too fast to be seen by the human eye.
Solution
Add input() in the bottom of your script and then try executing it with double click. Now the cmd will be open until you close it.
Example
Copyprint("Hello World")
input()
Problem 2
./ issue
Solution
Use Tab to autocomplete the filenames rather than manually typing the filename with ./ autocomplete automatically fills all this for you.
Usage
CD into the directory in which .py files are present and then assume the filename is test.py then type python te and then press Tab, it will be automatically converted to python ./test.py.
Try setting the path this way:
Copy $env:path="$env:Path;C:\Python27"
For what's worth, this command did it for me (Python 3.3):
Copy[System.Environment]::SetEnvironmentVariable("PATH", $Env:Path + ";C:\Python33", "Machine")
I just had to restart the PowerShell window after that.
» pip install setuptools
Last semester, I started work on the Import-Package module. It is still in the prerelease stages as it needs some polishing before going to v1, but I started putting it to use.
Preface: my Import-Package module
PowerShell's Import-Module command (as well as Add-Type) can be used to import C# dlls. However, both commands lack good dependency management.
If a .dll is dependent on another, those dependencies must be prepared and loaded manually. C# .nupkgs are made for automatic dependency management, but Import-Module can only load PowerShell .nupkgs.
There is the PowerShell PackageManagement module that provides functions for installing, updating and removing them, but it doesn't provide methods for loading them.
So, I wrote a module of my own.
Microsoft makes nuget.exe's and dotnet.exe's internals available as C# libraries. Examples are:
-
NuGet.Packaging - used for parsing .nupkgs and .nuspecs
-
Microsoft.NETCore.Platforms - used for identifying OS's as used by nuget.exe and dotnet.exe
All of these libraries are used in Import-Package to parse and load entire .nupkgs from NuGet.
Python.NET
The main reason I set out to write the Import-Package module last semester was to explore ways to automate Edge using webdriver.
NuGet.org offers good Selenium libraries, but doesn't offer great ones for webdriver installation. Python's webdriver-manager library is more robust and better maintained than similar libraries in C#. On top of that, I was also curious to know if cpython's binding API was available in C#.
It is: nuget.org - pythonnet (Python.NET, formerly Python.Runtime)
-
IronPython is also an option. When picking an embedded engine use these considerations:
-
IronPython can be run multithreaded. CPython (Python.NET) can not.
-
CPython (Python.NET) supports the ctypes module. IronPython does not.
-
CPython is the official python engine from Python.org and has a better release schedule than IronPython
-
Currently CPython supports python 3.12, while IronPython is still on python 3.7
-
-
Use Cases
The biggest use case for doing this (over just using python.exe) is to make libraries written for Python available for PowerShell.
Here is an example of how I currently use the library:
Python Selenium:
Prepare Python.NET:
using namespace Python.Runtime
Import-Module Import-Package
Import-Package pythonnet
# cpython has a GIL, so in order to use the python API, you need to lock it:
# - Unlocking the GIL does not destroy any python variables or data. It just prevents you from using it.
New-Module -Name "CPython-GIL" -ScriptBlock {
$state = @{ "lock" = $null }
function global:Lock-Python {
Write-Host "Python GIL is now locked. Unlock it ANYTIME with Unlock-Python." -ForegroundColor Yellow
$state.lock = [Python.Runtime.Py]::GIL()
}
function global:Unlock-Python {
$state.lock.Dispose()
}
Export-ModuleMember
} | Import-Module```
Lock-Python # GIL is now locked. Python API is now usable.
$python = @{} # hashtable for my python variablesLoad the Python libraries
# Get the webdriver-manager and selenium package objects
$python.webdriver = [Py]::Import( "webdriver_manager" )
$python.selenium = [Py]::Import( "selenium" )
# Import the subpackages. These will be available as a property on the parent package
& {
[Py]::Import( "webdriver_manager.microsoft" )
[Py]::Import("selenium.webdriver.edge.options")
[Py]::Import("selenium.webdriver.common.keys")
[Py]::Import("selenium.webdriver.edge.service")
}Prepare Edge and Edge WebDriver
Update/Install msedgedriver.exe and create the Selenium 4 service
$msedge = @{}
# Update and get path to msedgedriver.exe
$msedge.webdriver = $python.webdriver.EdgeChromiumDriverManager().install()Python.NET objects are designed to be strictly dynamic in nature
-
They don't automatically cast themselves to C#/PowerShell-friendly types.
-
They do support a lot of standard type operands like concatenation and property accessors...
-
...but I find it best to just cast to a C# type when possible.
-
Prepare the EdgeOptions object
# Create the EdgeOptions object $msedge.options = $python.selenium.webdriver.EdgeOptions()
!!!CAREFUL!!!
Chrome-based browsers do not allow you to use a User Data directory via webdriver at the same time as the user.
You can either close all user browsers or clone the default user data instead.
You can obtain the User Data directory directory path from edge://version or chrome://version > Profile Path. The User Data directory is the parent folder to the profile folder
# Paste your Profile Path here:
# - This is the default path for Edge:
$msedge.profile_path = "C:\Users\Administrator\AppData\Local\Microsoft\Edge\User Data\Default"
$msedge.profile_folder = $msedge.profile_path | Split-Path -Leaf
$msedge.user_data = $msedge.profile_path | Split-Path -Parent
$msedge.options.add_argument("--user-data-dir=$( $msedge.user_data )")
$msedge.options.add_argument("--profile-directory=$( $msedge.profile_folder )")
$msedge.options.add_argument("--log-level=3") # Chrome.exe and Edge.exe can be extremely noisy
$msedge.options.page_load_strategy="none" # Allows controlling the browser before page loadAutomate away!
# Start the automated browser
$Window = & {
# Internally, python keyword arguments are actually a kw object:
$service = [Py]::kw( "service", $msedge.service )
$options = [Py]::kw( "options", $msedge.options )
$python.selenium.webdriver.Edge( $service, $options )
}
# go to url:
$Window.get( "edge://version" )
# run javascript:
$Window.execute_script( "window.open('https://google.com','_blank')" )FUTURE PLANS:
I've unfortunately remembered that V8 is also embeddable. There's also already a C# bindings library for it: https://github.com/Microsoft/ClearScript
If I can get it working, I'll share my results.
EDIT: done - Turning PowerShell into a JavaScript Engine