ylikx / forpy

Forpy - use Python from Fortran

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Configuration on Windows with Intel visual fortran complier

wuxiaoxiong1990 opened this issue · comments

Hello, I’m trying to call some python modules of anaconda from FORTRAN program on Windows OS and intel visual fortran complier integrated in VS IDE. I tested the examples of forpy on linux with ifort which worked well but raised errors on Windows. The complier cannot recongnize the function used in forpy.f90 like '_Py_Initialize'. I didn't find the manual about configuration on Windows. Can you provide some detailed sugguestions about this situation? Many thanks!

Hi, this looks like that you did not link against the Python library. I did not have the chance to try it myself, but according to [1] (point 7.), you can set the path to the Python library in the project properties in the tab "Linker > General > Additional Library Directories".
Make sure to use the path of anaconda Python. You might also have to set the PYTHONHOME environment variable (see #6).

[1] https://docs.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2019#create-the-core-c-projects

Hi, this looks like that you did not link against the Python library. I did not have the chance to try it myself, but according to [1] (point 7.), you can set the path to the Python library in the project properties in the tab "Linker > General > Additional Library Directories".
Make sure to use the path of anaconda Python. You might also have to set the PYTHONHOME environment variable (see #6).

[1] https://docs.microsoft.com/en-us/visualstudio/python/working-with-c-cpp-python-in-visual-studio?view=vs-2019#create-the-core-c-projects

Thanks for your reply and your contribution to forpy. I have modified some configurations depending on your suggestion and make the test case work on VS. I summarize it as follow for others who may encounter the same problem:

  1. switch debug to x64 mode if anaconda is x64
  2. in the tab "tools-settings-Intel Composer XE-Visual Fortran-Compilers-x64"
    add "C:\ProgramData\Anaconda3\include" to Includes
    add "C:\ProgramData\Anaconda3\libs" to Libraries
    where "C:\ProgramData\Anaconda3" is root path of python.exe
  3. "project properties-Fortran-Preprocessor-Preprocess Source File" Yes(/fpp)
    "Linker-Input-Additional Dependencies" python36.lib (depends on libs)
  4. If step 2 is skipped, "include" path to "Fortran-General-Additional Include Directories" and "libs" path to "Linker-General-Additional Library Directories"

Notice:
If I set PYTHONHOME variable on Windows (my anaconda version 3.6.3) an error like "Fatal Python error: Py_Initialize: unable to load the file system codec ModuleNotFoundError: No module named 'encodings' " will be induced like this. "intro_to_forpy" works well without PYTHONHOME.

I'm trying to use forpy with Intel Fortran on Windows 10 with Visual studio 2019. I have installed Python 3.7 and have followed the steps given by @wuxiaoxiong1990, but I cannot get it to work.

It throws the following error:

forpy_mod.obj
forpy_mod.obj : error LNK2019: unresolved external symbol Py_DecRef referenced in function FORPY_MOD_mp_FORPY_FINALIZE
forpy_mod.obj : error LNK2019: unresolved external symbol Py_Finalize referenced in function FORPY_MOD_mp_FORPY_FINALIZE
forpy_mod.obj : error LNK2019: unresolved external symbol PyType_IsSubtype referenced in function FORPY_MOD_mp_IS_FLOAT
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_IsInstance referenced in function FORPY_MOD_mp_IS_NDARRAY
forpy_mod.obj : error LNK2019: unresolved external symbol PyErr_Occurred referenced in function FORPY_MOD_mp_IS_NDARRAY
forpy_mod.obj : error LNK2019: unresolved external symbol PyErr_Clear referenced in function FORPY_MOD_mp_IS_NDARRAY
forpy_mod.obj : error LNK2019: unresolved external symbol PyList_New referenced in function FORPY_MOD_mp_LIST_CREATE_EMPTY
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_List referenced in function FORPY_MOD_mp_LIST_CREATE_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyTuple_New referenced in function FORPY_MOD_mp_TUPLE_CREATE_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_Tuple referenced in function FORPY_MOD_mp_TUPLE_CREATE_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol Py_IncRef referenced in function FORPY_MOD_mp_NONETYPE_CREATE
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_New referenced in function FORPY_MOD_mp_DICT_CREATE
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_Clear referenced in function FORPY_MOD_mp_DICT_CLEAR
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_Copy referenced in function FORPY_MOD_mp_DICT_COPY
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_Keys referenced in function FORPY_MOD_mp_DICT_KEYS
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_Items referenced in function FORPY_MOD_mp_DICT_ITEMS
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_Values referenced in function FORPY_MOD_mp_DICT_VALUES
forpy_mod.obj : error LNK2019: unresolved external symbol PyList_Append referenced in function FORPY_MOD_mp_LIST_APPEND_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyLong_FromLongLong referenced in function FORPY_MOD_mp_LIST_APPEND_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PyFloat_FromDouble referenced in function FORPY_MOD_mp_LIST_APPEND_REAL32
forpy_mod.obj : error LNK2019: unresolved external symbol PyComplex_FromDoubles referenced in function FORPY_MOD_mp_LIST_APPEND_COMPLEX_REAL32
forpy_mod.obj : error LNK2019: unresolved external symbol PyUnicode_DecodeUTF8 referenced in function FORPY_MOD_mp_LIST_APPEND_CHAR_1D
forpy_mod.obj : error LNK2019: unresolved external symbol PyList_Sort referenced in function FORPY_MOD_mp_LIST_SORT
forpy_mod.obj : error LNK2019: unresolved external symbol PyList_Reverse referenced in function FORPY_MOD_mp_LIST_REVERSE
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_Concat referenced in function FORPY_MOD_mp_LIST_ADD
forpy_mod.obj : error LNK2019: unresolved external symbol PyList_Insert referenced in function FORPY_MOD_mp_LIST_INSERT_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_DelItem referenced in function FORPY_MOD_mp_LIST_DELITEM_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_GetItem referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyLong_AsLongLongAndOverflow referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PyErr_SetString referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PyFloat_AsDouble referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_REAL32
forpy_mod.obj : error LNK2019: unresolved external symbol PyComplex_AsCComplex referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_COMPLEX_REAL32
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_IsTrue referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_LOGICAL
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_Length referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_CHAR_1D
forpy_mod.obj : error LNK2019: unresolved external symbol PyUnicode_AsUTF8AndSize referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_CHAR_1D
forpy_mod.obj : error LNK2019: unresolved external symbol PyBytes_AsString referenced in function FORPY_MOD_mp_SEQUENCE_GETITEM_INT32_CHAR_1D
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_Index referenced in function FORPY_MOD_mp_SEQUENCE_INDEX_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_Count referenced in function FORPY_MOD_mp_SEQUENCE_COUNT_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_Contains referenced in function FORPY_MOD_mp_SEQUENCE_CONTAINS
forpy_mod.obj : error LNK2019: unresolved external symbol PySequence_SetItem referenced in function FORPY_MOD_mp_MUTABLESEQUENCE_SETITEM_INT32_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyTuple_SetItem referenced in function FORPY_MOD_mp_TUPLE_SETITEM_INT32_INT32
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_GetAttr referenced in function FORPY_MOD_mp_OBJECT_GETATTRIBUTE
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_SetAttr referenced in function FORPY_MOD_mp_OBJECT_SETATTR
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_GetItem referenced in function FORPY_MOD_mp_MAPPING_GETITEM_OBJECT_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_SetItem referenced in function FORPY_MOD_mp_MAPPING_SETITEM_OBJECT_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_DelItem referenced in function FORPY_MOD_mp_MAPPING_DELITEM_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyMapping_HasKey referenced in function FORPY_MOD_mp_MAPPING_CONTAINS
forpy_mod.obj : error LNK2019: unresolved external symbol PyErr_GivenExceptionMatches referenced in function FORPY_MOD_mp_DICT_GET_OBJECT_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyBytes_FromStringAndSize referenced in function FORPY_MOD_mp_BYTES_CREATE_CHARS
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_Str referenced in function FORPY_MOD_mp_STR_CREATE_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyImport_ImportModule referenced in function FORPY_MOD_mp_IMPORT_PY
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_Call referenced in function FORPY_MOD_mp_CALL_PY_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyBuffer_FillContiguousStrides referenced in function FORPY_MOD_mp_NDARRAY_CREATE_NOCOPY_INT32_1D
forpy_mod.obj : error LNK2019: unresolved external symbol PyMemoryView_FromBuffer referenced in function FORPY_MOD_mp_NDARRAY_CREATE_NOCOPY_INT32_1D
forpy_mod.obj : error LNK2019: unresolved external symbol PyObject_GetBuffer referenced in function FORPY_MOD_mp_GET_DATA_HELPER
forpy_mod.obj : error LNK2019: unresolved external symbol PyBuffer_IsContiguous referenced in function FORPY_MOD_mp_GET_DATA_HELPER
forpy_mod.obj : error LNK2019: unresolved external symbol PyBuffer_Release referenced in function FORPY_MOD_mp_GET_DATA_HELPER
forpy_mod.obj : error LNK2019: unresolved external symbol PyModule_AddObject referenced in function FORPY_MOD_mp_PYTHONMODULE_ADD_OBJECT
forpy_mod.obj : error LNK2019: unresolved external symbol PyEval_GetBuiltins referenced in function FORPY_MOD_mp_PRINT_PY
forpy_mod.obj : error LNK2019: unresolved external symbol PyDict_GetItemString referenced in function FORPY_MOD_mp_PRINT_PY
forpy_mod.obj : error LNK2019: unresolved external symbol PySys_GetObject referenced in function FORPY_MOD_mp_GET_SYS_PATH
forpy_mod.obj : error LNK2019: unresolved external symbol PyRun_SimpleString referenced in function FORPY_MOD_mp_RUN_STRING
forpy_mod.obj : error LNK2019: unresolved external symbol PyModule_Create2 referenced in function FORPY_MOD_mp_PYTHONMODULE_INIT
forpy_mod.obj : error LNK2019: unresolved external symbol PyErr_Print referenced in function FORPY_MOD_mp_ERR_PRINT
forpy_mod.obj : error LNK2019: unresolved external symbol PyBool_FromLong referenced in function FORPY_MOD_mp_FORPY_INITIALIZE_BOOL
forpy_mod.obj : error LNK2019: unresolved external symbol Py_IsInitialized referenced in function FORPY_MOD_mp_FORPY_INITIALIZE
forpy_mod.obj : error LNK2019: unresolved external symbol Py_Initialize referenced in function FORPY_MOD_mp_FORPY_INITIALIZE
libifcoremt.lib(for_main.obj) : error LNK2019: unresolved external symbol MAIN__ referenced in function main
forpy_mod.exe : fatal error LNK1120: 68 unresolved externals

Does anyone have a workaround? @ylikx Could you please help me on this?

Hi, according to the error message linking against the Python runtime library failed. Try to locate the python37.lib file and make sure you have added the correct library path as described above.
Also you can not create a .exe from forpy_mod.F90 alone, you'll need some example code that uses the forpy module.