Skip to content
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

[Enhancement Request] Allow importing Python package with C-extension e.g. numpy #4846

Closed
wta12 opened this issue Mar 30, 2023 · 4 comments · Fixed by #4868
Closed

[Enhancement Request] Allow importing Python package with C-extension e.g. numpy #4846

wta12 opened this issue Mar 30, 2023 · 4 comments · Fixed by #4868

Comments

@wta12
Copy link

wta12 commented Mar 30, 2023

Enhancement Request

Allow importing package with C-extension e.g. numpy

Detailed Description

OS SDK 3.5.1 returns error if execute python script with numpy as dependencies
"OpenStudio/OpenStudio_3_5_1/bin/openstudio labs --verbose --python_home /usr/local/anaconda3/envs/pythonMeasure --python_path /usr/local/anaconda3/envs/pythonMeasure/lib/python3.8/site-packages execute_python_script python_script_test.py"
Return error:

Error message: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

Possible Implementation:

EP instance in same OS SDK installation allows Plugin:Instance (pythons script) with numpy imported

@wta12 wta12 added Enhancement Request Triage Issue needs to be assessed and labeled, further information on reported might be needed labels Mar 30, 2023
@tijcolem tijcolem added component - CLI and removed Triage Issue needs to be assessed and labeled, further information on reported might be needed labels Mar 31, 2023
@jmarrec jmarrec changed the title [Enhancement Request] Allow importing package with C-extension e.g. numpy [Enhancement Request] Allow importing Python package with C-extension e.g. numpy Apr 4, 2023
@tijcolem
Copy link
Collaborator

@Myoldmopar I think this one is related to how E+ manages python and python packages?

@tijcolem
Copy link
Collaborator

@wta12 Also, have you tried setting the PYTHONPATH env to point to the pip package repo and then run it?

e.g. export PYTHONPATH=/path/to/my/modules/

@Myoldmopar
Copy link
Member

I'm happy to help, but I'd need to dive into the way OpenStudio packages and uses EnergyPlus. There is certainly a method for this to work with raw EnergyPlus, although it's admittedly not the most elegant solution. I suspect @jmarrec would have more to offer in the near term, if available.

@jmarrec jmarrec self-assigned this Apr 25, 2023
jmarrec added a commit that referenced this issue Apr 25, 2023
* Explicitly insert to sys.path the site-packages and lib-dynload folders from the system python
* Pass --python_path twice (site-packages and lib-dynload)
* Pass --python_home
@jmarrec
Copy link
Collaborator

jmarrec commented Apr 25, 2023

I have it fixed locally. But I'd like to discuss with @kbenne about it, because I had to change the Unix (well, Ubuntu to be exact, I think it works on mac...) dlopen flags from RTLD_LAZY | RTLD_LOCAL to RTLD_GLOBAL | RTLD_NOW when using python.

On unix, the C native ext are NOT linked to libpython.so. Our libpythonengine.so IS linked to it.
If we use RTLD_LOCAL, we do not export these libpython.so symbols and as such we get unresolved symbols at runtime, such as undefined symbol: PyFloat_Type. The alternative is to find a clever way also dlopen the libpython.so, this one with the RTLD_GLOBAL visibility.

https://stackoverflow.com/questions/67891197/ctypes-cpython-39-x86-64-linux-gnu-so-undefined-symbol-pyfloat-type-in-embedd

Currently

Mac

$ openstudio labs --python_path /Users/julien/.pyenv/versions/3.8.12/lib/python3.8/site-packages --python_path /Users/julien/.pyenv/versions/3.8.12/lib/python3.8/lib-dynload --pyexecute "import numpy as np; print(np.array([1, 2, 3]))"
┌────────────────────────────────────────────────────────────────────────────────┐
│         The `labs` command is experimental - Do not use in production          │
└────────────────────────────────────────────────────────────────────────────────┘
[1 2 3]

Ubuntu 20.04

$ openstudio labs --python_path /home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages --python_path /home/julien/.pyenv/versions/3.8.13/lib/python3.8/lib-dynload --pyexecute "import numpy as np; print(np.array([1, 2, 3]))"
┌────────────────────────────────────────────────────────────────────────────────┐
│         The `labs` command is experimental - Do not use in production          │
└────────────────────────────────────────────────────────────────────────────────┘
Traceback (most recent call last):
  File "/home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/core/__init__.py", line 23, in <module>
    from . import multiarray
  File "/home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/core/multiarray.py", line 10, in <module>
    from . import overrides
  File "/home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/core/overrides.py", line 6, in <module>
    from numpy.core._multiarray_umath import (
ImportError: /home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-x86_64-linux-gnu.so: undefined symbol: PyObject_SelfIter

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/__init__.py", line 141, in <module>
    from . import core
  File "/home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/core/__init__.py", line 49, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/usr/bin/python3"
  * The NumPy version is: "1.24.3"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: /home/julien/.pyenv/versions/3.8.13/lib/python3.8/site-packages/numpy/core/_multiarray_umath.cpython-38-x86_64-linux-gnu.so: undefined symbol: PyObject_SelfIter

terminate called after throwing an instance of 'std::runtime_error'
  what():  Error executing Python code
Aborted (core dumped)

jmarrec added a commit that referenced this issue Jun 30, 2023
* Explicitly insert to sys.path the site-packages and lib-dynload folders from the system python
* Pass --python_path twice (site-packages and lib-dynload)
* Pass --python_home
jmarrec added a commit that referenced this issue Jul 6, 2023
#4846 - Allow importing Python package with C-extension e.g. numpy in the labs CLI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants