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

MWE for path error when using libpython310 on macOS #28

Draft
wants to merge 59 commits into
base: main
Choose a base branch
from

Conversation

speth
Copy link
Contributor

@speth speth commented Aug 20, 2023

This is a MWE for an issue building a package that uses an embedded Python interpreter by linking to libpython3.10.dylib and starting the interpreter from the Python C API. The problem is that in the embedded interpreter, sys.path refers to paths in the base conda environment, rather than the active conda environment corresponding to the libpython3.10.dylib that is linked. This then makes it impossible to import packages that are installed (only) in the active conda environment.

Examining sys.path

Relevant files:

This example builds the following C++ program:

#include "Python.h"

int main(int argc, char** argv)
{
    Py_Initialize();
    PyRun_SimpleString(
        "import sys\n"
        "print('Hello, world')\n"
        "print('embedded sys.path:', sys.path)\n"
        "sys.stdout.flush()\n");
    return 0;
}

with the compiler command:

clang++ -I${CONDA_PREFIX}/include/python3.10 -L${CONDA_PREFIX}/lib mwe.cpp -o mwe -lpython3.10 -Wl,-rpath,${CONDA_PREFIX}/lib

This shows that the embedded interpreter's sys.path is (incorrectly) defined as:

['/Users/runner/miniforge3/lib/python310.zip',
 '/Users/runner/miniforge3/lib/python3.10',
 '/Users/runner/miniforge3/lib/python3.10/lib-dynload',
 '/Users/runner/miniforge3/lib/python3.10/site-packages']

While the python binary in this environment reports sys.path as:

['',
 '$PREFIX/lib/python310.zip',
 '$PREFIX/lib/python3.10',
 '$PREFIX/lib/python3.10/lib-dynload',
 '$PREFIX/lib/python3.10/site-packages']

(where the instances of $PREFIX are output substitutions introduced by Azure Pipelines).

Specific conditions required to trigger behavior

This error occurs when:

  • Using a conda-forge feedstock
  • With macOS x64 (Intel)
  • With Python 3.10

Other versions of Python (3.8, 3.9, and 3.11) all work fine, as do all builds for Linux and Windows. I have also successfully built a minimal embedded Python test using the conda-forge build of Python 3.10 for macOS x64 outside of the conda-forge feedstock infrastructure (see repository and CI results here).

speth and others added 30 commits August 15, 2023 16:44
Minimum version for C++17 support
CPPFLAGS is just the preprocessor flags anyway
Otherwise, we have a conflict between 'cantera.lib' as the static
library versus as the "import library" for 'cantera.dll'.
@conda-forge-webservices
Copy link

Hi! This is the friendly automated conda-forge-linting service.

I just wanted to let you know that I linted all conda-recipes in your PR (recipe) and found it was in an excellent condition.

speth added a commit to speth/conda-forge-cantera-feedstock that referenced this pull request Aug 20, 2023
speth added a commit to speth/conda-forge-cantera-feedstock that referenced this pull request Aug 20, 2023
speth added a commit to speth/conda-forge-cantera-feedstock that referenced this pull request Aug 22, 2023
speth added a commit to speth/conda-forge-cantera-feedstock that referenced this pull request Aug 22, 2023
@bryanwweber
Copy link
Member

@speth was this resolved upstream?

@speth
Copy link
Contributor Author

speth commented Jun 19, 2024

Not to my knowledge. The issue I created (conda-forge/conda-forge.github.io#1996) is still open and got no response. I'm not sure how to update/re-run this to test it again.

@bryanwweber
Copy link
Member

I asked around if there was someone who could help out with that issue. Let's see if it gets a response

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

Successfully merging this pull request may close these issues.

2 participants