Pythonnet Import Error Visbrain in Anaconda 3

1

Environment :

  • Pythonnet version: 2.3.0, installed with pip in Amaconda3
  • Python version: 3.6.6 using with Anaconda
  • Visual Studio 2017 Community
  • Operating System: Windows 7, 64 bit

I am running Visbrain module by Pythonnet. I had create a virtual environment named py36 in Anaconda 3. Anaconda 3 is in D drive. Trying to run below code in my c# program.

using (Py.GIL())
{
 dynamic np = Py.Import("visbrain");
}

But it pops out an error which is

Python.Runtime.PythonException: 'ModuleNotFoundError : No module named 'visbrain''

stack trace :

Python.Runtime.PythonException
  HResult=0x80131500
  Message=ModuleNotFoundError : No module named 'visbrain'
  Source=Python.Runtime
  StackTrace:

i had visbrain under Lib\site-packages.

Tried Solution but did not success:

1) add PATH, PYTHONHOME in environment variable. PATH : D:\Anaconda3\envs\py36; PYTHONHOME : D:\Anaconda3\envs\py36;

2) pythonnet Embedding Python in .net example failing to load module added PYTHONPATH : D:\Anaconda3\envs\py36\Lib\site-packages;

===

i am strange that "numpy" module is also inside of the Lib\site-packages.

when i tried below example

dynamic np = Py.Import("numpy");

It is fine.

Would like to know what is the issue here and how to solve this, thank you.

c#
importerror
python.net
asked on Stack Overflow Aug 15, 2018 by STREETKILLER007 • edited Jan 1, 2019 by Cœur

2 Answers

2

according to https://github.com/pythonnet/pythonnet/wiki/Troubleshooting-on-Windows,-Linux,-and-OSX

it mentioned

"Unable to load DLL pythonXX": CPython is not installed, or not registered in %PATH% and %PYTHONHOME% environment variables. Alternatively set PythonEngine.PythonHome property before initializing Python runtime.

Instead of setting up PythonHome and Path manually, you set them up by coding.

before PythonEngine.Initialize(); just remember to set the PATH and PYTHONHOME, just as example

// Modify Path
string path = @"d:\Anaconda3\envs\py36;" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine); 

// Set Path
Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process);

// Set PythonHome
Environment.SetEnvironmentVariable("PYTHONHOME", @"d:\Anaconda3\envs\py36", EnvironmentVariableTarget.Process);

// Set PythonPath
// ONLY SET THIS IF YOU ARE SURE WHAT YOU ARE DOING
Environment.SetEnvironmentVariable("PYTHONPATH", @"d:\Anaconda3\envs\py36\Lib", EnvironmentVariableTarget.Process);
answered on Stack Overflow Aug 17, 2018 by STREETKILLER007 • edited Aug 21, 2018 by STREETKILLER007
0

I had a similar issue with Miniconda, apparently Python.NET does not work well with conda. See this answer: best is do reinstall python (without conda).

answered on Stack Overflow Aug 26, 2020 by bricx

User contributions licensed under CC BY-SA 3.0