bbopt / nomad

NOMAD - A blackbox optimization software
https://nomad-4-user-guide.readthedocs.io/
GNU Lesser General Public License v3.0
110 stars 24 forks source link

PyNomad Import Error in Python>3.6 #151

Open juavid opened 10 months ago

juavid commented 10 months ago

I have tested PyNomad successfully with Python 3.6 but when I try to install it in Python version >3.6 I get the following error after installing it when trying to import the PyNomad library.

import PyNomad ImportError: DLL load failed while importing PyNomad: The specified module could not be found.

Is there is any workaround to make it work in Python version >3.6?

Thanks in advanced.

ctribes commented 10 months ago

It should work with Python version > 3.6. Nothing special is required.

Something that can go wrong is the path where Nomad libraries are located. Here are some checks that I did on my computer. Please note, the last element in the %PATH% variable, this is where the Nomad libraries have been put when building with mingw tools (it will be different for Visual Studio).

image

ctribes commented 9 months ago

There are several tricky things when building/import PyNomad. One is the compatibility between the compiler and the Python version. For example, in my case (OSX), I have

_(base) christophe@Fol-de-Dol nomad4devCopy2 % python Python 3.9.7 (default, Sep 16 2021, 08:50:36) [Clang 10.0.0 ] :: Anaconda, Inc. on darwin Type "help", "copyright", "credits" or "license" for more information.

So I need to build nomad with Clang. But the right architecture must be selected ('arm64' or 'x86_64'). I can force x86_64 by adding a build flag like -DCMAKE_OSX_ARCHITECTURES=x86_64 when configuring with cmake.

ctribes commented 9 months ago

When importing PyNomad in Python is not working another option exists.

Nomad can optimize on a Python blackbox without using the PyNomad interface in batch mode (see user guide). A simple example is provided.

param.txt

bb.py

import sys
import os
import re
input_file_name=sys.argv[1]
with open(input_file_name,'r') as openfile:
    line = openfile.read().strip()
    X = re.sub('\s+',' ',line).strip().split()
    openfile.close()

dim = 5
f = float(X[4])
g1 = sum([(float(X[i])-1)**2 for i in range(dim)])-25
g2 = 25-sum([(float(X[i])+1)**2 for i in range(dim)])
print(f,g1,g2)
juavid commented 9 months ago

That is a good solution, thanks for the help, I have tried with VS17 and VS16 and two different PCs. I identified at least one potential problem related to setuptools>60.0.0. but even then the problem remains. There are some warning messages as well during the compilation but they seem to also appear when I compile it for python 3.6. I was wondering what would be the recommended compiler for Windows?

ctribes commented 9 months ago

Visual Studio 15 works for me when building PyNomad. I tried with MinGW and it is not working as is.

nic-ian commented 7 months ago

Has someone found the causes of this problem? I am using Visual Studio 2022 as a complier, and Python 3.10.12. It produces this error message when importing PyNomad from Python. I used DependenciesGUI to find the missing DLL but no one of them seems missing. The path should be correct since i can correctly call nomad from my CMD.

Furthermore, I checked the architecture and it is correct AMD64, both in the .pyd and in my computer

Thank you in advance!

ctribes commented 6 months ago

The building procedure has changed for release v.4.4.0. Maybe it can help solving the problem. Also, PyNomad binaries are now available at https://pypi.org/project/PyNomadBBO/