-
Notifications
You must be signed in to change notification settings - Fork 750
Running Embedded Python.NET Application on Environment without Python Installed? #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That is exactly what I'm doing with pythonnet, but I install CPython as part of larger installation. @rionmonster one option would be to use the nuget packages for CPython and pythonnet: https://www.nuget.org/packages/python2/ When embedding this nuget package in .NET app, you will need to set PYTHONHOME environment variable or this property |
Thanks for the insights @denfromufa I eventually elected to go another route for the time being, but I was able to play around with the use of I appreciate it. |
@denfromufa @rionmonster I'm attempting to do the same thing. I installed pythonnet_py27_dotnet & python2 via nuget and then I'm setting If I drop the DLL in the appropriate Windows\System location everything works as expected but that's what I'm trying to avoid. Any ideas on how to get pythonnet to find the python27 DLL? |
@willdrob you may need to append this python path to Also there is outstanding issue with explicitly picking the 64-bit binary if you have Python 64-bit: |
@denfromufa you are correct, I needed to append the python path to %path%. |
@rionmonster @denfromufa I am also implementing a similar process for packaging python code to be used within a Windows application without requiring the user to install Python. I found a way to include the python runtime as well as dependent libraries in the project itself. It turns out if you include the PythonRuntime.dll in the project and include the dependent libraries in a Lib folder in the same directory, the python script is able to be loaded and used correctly. I am running into an issue though being that if the PYTHONHOME environment variables are set (for some system python installation), then the pythonruntime.dll will no longer find the python files in the project. It seems to prioritize searching in the paths set by environment variables first. Then if environment variables are set, the pythonruntime.dll will no longer look in the project directory. The problem is I want to prefer using the python dependencies in my project as opposed to the user's system install since the user may not have the necessary dependencies and versions installed. Do you all know of a fix for this? Is it possible to change the search prioritization in the Pythonnet runtime DLL? |
@ryan-rogowski just hide %PYTHONHOME% in your app before loading Python runtime. |
Thanks @denfromufa ! Do you mean to set PYTHONHOME to an empty string prior to loading Python runtime? or how would I go about hiding the environment variable from my app?
|
@wdrobinson HI I'm new so bare with me,My code looks like this But your's is I need explanation of how you're setting path without using |
Wanted to add some notes about embedding the latest version of python (3+) while using pythonnet. As mentioned previously, there is a python nuget package that contains all the python runtime. It supports current python version Add <ItemGroup>
<PackageReference Include="pythonnet" Version="3.0.3" />
<PackageReference Include="python" Version="3.9.0" />
</ItemGroup> Add an item in csproj to include the python nuget package's <ItemGroup>
<None Include="$(Pkgpython)\tools\**\*" LinkBase="python" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup> Update using Python.Runtime;
var pythonDir = Path.Combine(Environment.CurrentDirectory, @"python");
// hardcoded for now, but can probably be made to a bit more dynamic if we search through the `python` output directory
Runtime.PythonDLL = Path.Combine(pythonDir, "python39.dll");
PythonEngine.PythonHome = pythonDir;
PythonEngine.Initialize(); I suspect when copying the files from the python nuget package's Credit to above posters for paving the way, as well as this SO post which shed some light on how to copy the python nuget package's contents to the build output folder. Btw, if you want to browse the full contents of the python nuget package's directory, you can view it under this folder Hope this helps get someone on the ground a bit quicker. Now I'm trying to figure out how to install python modules at build time to the output directory... |
As for installing python modules, here's a really simple example target after build/publish that accomplishes this: <Target Name="InstallPyModulesAfterBuild" AfterTargets="Build">
<PropertyGroup>
<PyBaseDir>$(OutputPath)</PyBaseDir>
<PyModules>cryptography pyasyncore boto3 prompt_toolkit google-auth requests</PyModules>
</PropertyGroup>
<Message Text="Installing python modules (PyBaseDir = `$(PyBaseDir)`): $(PyModules)" Importance="High" />
<Exec Command="$(PyBaseDir)python\python.exe -m pip install $(PyModules) --quiet --disable-pip-version-check" />
</Target>
<Target Name="InstallPyModulesAfterPublish" AfterTargets="Publish">
<PropertyGroup>
<PyBaseDir>$(PublishDir)</PyBaseDir>
<PyModules>cryptography pyasyncore boto3 prompt_toolkit google-auth requests</PyModules>
</PropertyGroup>
<Message Text="Installing python modules (PyBaseDir = `$(PyBaseDir)`): $(PyModules)" Importance="High" />
<Exec Command="$(PyBaseDir)python\python.exe -m pip install $(PyModules) --quiet --disable-pip-version-check" />
</Target> Note: I'm sure there's an easy way to simplify using the same logic for both build & publish, but this worked well enough for me now, albeit it is essentially installing/downloading the python modules twice. |
Environment
Details
I haven't worked too extensively with Python previously, but I've been experimenting with the idea of writing some utilities using it and embedding them within a .NET application (e.g. Windows Forms, Console, WPF, etc.). Is it possible to embed Python itself within the assembly using Python.NET or is it required that Python be installed on the executing machine?
Any attempts that I made to run outside of my development environment (with Python 2.7 installed), resulted in an error about a missing
python27.dll
, which I couldn't seem to figure out how to include, and I may just be missing something.Ideally, I'd like to be able to package this up as an executable, distribute it to users, who will likely not have Python installed on their machine. I know that the documentation mentions the need for an interpreter and I was wondering if the one available with IronPython would suffice?
Any advice would be appreciated. I understand that this may not be plausible, but I'd love to take advantage of using Python.NET for this if possible.
The text was updated successfully, but these errors were encountered: