Using Python Script in .NET Applications
If you had learnt programming, high chance is you would have heard of Python script. Designed as a simplistic language that emphasize readability, it is no wonder that Python is one of the most popular and favorite among programmers.
In recent years, Python usage is popular in data science and machine learning. In engineering related fields or research works, python is also one of the frequently used language to achieve desired results. As a scripting language, it is free for everyone to download and use. There are many useful open source libraries written in Python and can be referenced and used in any Python program.
However, it is not without its drawbacks. When it comes to publishing an app, they are a little bit tricky and confusing for Windows users. Kickstarting the program requires some basic programming language, especially the usage of command prompts. For Unix and Linux users, they are more familiar in using terminal console to execute commands but for Windows and Macintosh, it might be confusing and inconvenient to ask a user to execute a certain command in order to let the Python program to run successfully.
Besides, there is also version compatibility issue for Windows users. Some Python scripts only support version 2 while the latest script requires v3.8 and above. Thus when it comes to deployment. We need to make sure the client computer needs to install the right version in order to ensure the program run successfully.
Launching Program with Python
Python is scripting programming so it has a minimum requirement for users to launch the program. Users need to know some knowledge on how to use command prompt or terminal console in their operating system. Below are the general steps to launch a Python program.
- Install the corresponding Python version.
- Install dependencies based on program requirements.
- Start command prompt > pip install PackageName.
- $ python program.py.
There is a version compatibility issue introduced in step 1 and the Python command may not register globally sometimes. Therefore, users would need to execute with a different command, ie. python3 or call the full path of the command. If the client computer only contains one version of Python, then the user would most likely not face this problem. This problem exists only if the client-side contains more than one Python version installed.
Thus, a better method is introduced to overcome this issue and make deployment seamless when it comes to releasing programs to Windows users. We can achieve that through Nuget packages.
Go to the Nuget manager and install the libraries below to your project. Your project can be any dotnet framework version. It doesn’t matter.
- NET Standard Library v2.0 (You need to install dotnet standard v2.0 in order to install Python.Included)
- Pythonnet_netstandard_py37_win (depends on the Python version you want to use, i.e. 3.5, 3.8 etc).
In your application startup script, call the code below to start the Python assembly and install dependencies if any (line 12).
If you found out Installer.TryInstallPip() not working on your computer, it is probably you may need to add the below snippet as a kind of “hack”.
How to Execute Python Script
After running the script, you’ll find out that the executable folder will contain a new folder python-3.7.3-embed-amd64 and all the dependencies will be downloaded under Lib\site-packages folder as well. In this case, you don’t need to worry if the client computer has installed Python Windows, or different versions. All your script now will be redirected to use this independent Python location.
Point of Interests
The downside of using this approach is there is a start-up time we cannot skip. Regardless whether we’ve downloaded the core Python compiler and framework, every time the program still has to go through the same workflow and spend time loading into the program memory.
You probably already knew that Python.Included only handles the dependencies deployment and a simple execute command. If you want to call a Python script directly from your dotnet application, you can look for IronPython or Python.NET.
Please leave your comment below if you have any ideas or thoughts. Thanks.
Originally published at http://filpal.wordpress.com on April 17, 2022.