Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conda Environments - Wrong Path Captured #463

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

Closed
ghost opened this issue May 11, 2017 · 15 comments
Closed

Conda Environments - Wrong Path Captured #463

ghost opened this issue May 11, 2017 · 15 comments

Comments

@ghost
Copy link

ghost commented May 11, 2017

Environment

  • Pythonnet version: 2.3
  • Python version: Python 3.5 (Conda environment)
  • Operating System: Windows 10

Details

I have many Conda Python environments, and I have installed PythonNet in only one of them.
I installed PythonNet using "conda install -c pythonnet pythonnet", and the Python.Runtime.dll is located in a conda environment named anaconda3: "C:\Users\username\Miniconda3\envs\anaconda3\Lib\site-packages"

In Visual Studio, I added a reference to the DLL and tried to follow the Embedding Python in .NET" example. My root Conda environment with no external libraries installed is running Python 3.6, so it is no surprise I was getting "Unable to load DLL 'python35'.

I went ahead and downgraded the base Conda environment to 3.5 and captured the PythonEngine.PythonPath variable and see it is pointing to the following paths:
"C:\Users\username\Miniconda3\python35.zip;C:\Users\username\Miniconda3\Lib;C:\Users\username\Miniconda3\DLLs;C:\Users\username\Documents\Visual Studio 2015\Projects\WindowsFormsApplication4\WindowsFormsApplication4\bin\x64\Debug"

So it appears that installing PythonNet in a conda environment and simply pointing to the DLL is insufficient. Is there any way to add the correct paths to the PythonEngine? I was unable to find any documentation on this.

@den-run-ai
Copy link
Contributor

@gemiko thanks for trying pythonnet! Search this issue tracker for %pythonhome% examples.

@den-run-ai
Copy link
Contributor

@ghost
Copy link
Author

ghost commented May 12, 2017

I attempted to follow this #259 (comment), under my VS2015 environment to set the paths, but didn't work. Setting PYTHONHOME in my system environmental variable to C:\Users\username\Miniconda3\envs\anaconda3 also did not work.

The only way PythonNet works for me is by using the root Anaconda environment, which isn't a solution when when I have to manage multiple projects that have their own package requirements which is why I'm using Conda environments.

Any other potential solutions?

@den-run-ai
Copy link
Contributor

@gemiko did you activate your env. before calling it for embedding in .NET at runtime? what errors and tracebacks did you get? what does conda env list show?

Finally before PythonEngine.Initialize() call you can set PythonHome property:

public static string PythonHome

@ghost
Copy link
Author

ghost commented May 19, 2017

Inside the commandline, activating the conda environment and then starting visual studio works.

However, when deploying the software, I found I absolutely needed to set the PYTHONHOME and PYTHONPATH environment variables. The good thing is that Conda Python environments are standalone and can be deployed with your application. It's just a matter of how to package it all together.

Suppose the Python 3.5 environment should be stored in a subdirectory named "Python" one level from the exe, this worked for me:

string exeDir = AppDomain.CurrentDomain.BaseDirectory;
string envPythonHome = exeDir + @"Python";
Environment.SetEnvironmentVariable("PYTHONHOME", envPythonHome, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PATH", envPythonHome, EnvironmentVariableTarget.Process);

Adding the following to the Post-Build event command line will mirror the Python environment to the build output directory and complete the build process (Robocopy error codes < 8 are not errors and need to be set to 0 for Visual Studio builds to be successful):
(robocopy "Path to Python Environment Directory" "$(TargetDir)Python" /MIR) ^& IF %ERRORLEVEL% LEQ 7 SET ERRORLEVEL=0

Hope this helps others!

@den-run-ai
Copy link
Contributor

@gemiko would you mind summarizing your tutorial in wiki? this is great resource asked many times here!

@ghost
Copy link
Author

ghost commented May 20, 2017

Unfortunately, it stopped working for me. I'll need to investigate it some more.

@ghost
Copy link
Author

ghost commented May 20, 2017

Looks like I got it working now. Setting both the PATH and PYTHONHOME to the directory is needed. I'll also edit the above post so there's no confusion for others.

string exeDir = AppDomain.CurrentDomain.BaseDirectory;
string envPythonHome = exeDir + @"Python";
Environment.SetEnvironmentVariable("PYTHONHOME", envPythonHome, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("PATH", envPythonHome, EnvironmentVariableTarget.Process);

@filmor
Copy link
Member

filmor commented Jul 11, 2017

This looks solved, feel free to reopen if that isn't the case.

@filmor filmor closed this as completed Jul 11, 2017
@shrishar
Copy link

shrishar commented Aug 7, 2018

Hi Everyone,

I tried every possible thing written on this page but nothing worked. Still getting this issue
System.DllNotFoundException: 'Unable to load DLL 'python35': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'
I have download pythonnet and python3.5 nuget package and trying to embed python in >net environment.

Please help

Thanks,
Divya

@den-run-ai
Copy link
Contributor

@shrishar i don't think anyone tested the python3.5 nuget package with pythonnet, but there should not be anything special about it. just make sure to match the architecture x86/x64.

@shrishar
Copy link

shrishar commented Aug 8, 2018 via email

@GafferMan
Copy link

GafferMan commented Dec 31, 2018

Is there any additional documentation on how to call python FROM C#? I have struggled with everything listed here and still cannot get this working. I have installed pythonnet into a python 3.6 environment running in Conda. I can access the clr FROM python, but really need to call python FROM C#.

I finally found that I needed to include "Python.Runtime.dll", but it was unclear from WHERE (I found it here: "C:\Anaconda\envs\cvpy36\Lib\site-packages" and assume that is what I need. (It would be helpful to include this information on the main pythonnet documentation page).

I was able to get through the "Unable to load DLL" errors by using the code that gemiko supplied above, but now as soon as I call PythonEngine.Initialize() the application quits (no errors, no messages, nothing).

Any suggestions, or link to more detailed step-by-step instructions on how to make this work in C# (something that includes adding the correct references, "using" statements, etc.)?

Thanks!

---- EDIT ----

I found the issue and want to leave this here for others.

It seems you have to set the environment variable, but you ALSO need to set PythonHome for the PythonEngine (I assumed this would get set from the environment variable, as PythonEngine cannot be called without it).

Adding the line:

PythonEngine.PythonHome = "C:\\Anaconda\\envs\\cvpy36";

fixed it.

@den-run-ai
Copy link
Contributor

@GafferMan yes, and anaconda can mess up few other things when embedding its Python via pythonnet. I prefer to use vanilla CPython for embedding use cases. There are now wheels for almost any packages, albeit not highly optimized.

@AK47dev1
Copy link

@shrishar i don't think anyone tested the python3.5 nuget package with pythonnet, but there should not be anything special about it. just make sure to match the architecture x86/x64.

Oh, Jesus Christ.
The only point for me was the architecture type!

My working solutions:
HERE_01
HERE_02

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants