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

[BUG] ModuleNotFoundError when cimporting from embedded cython program #6533

Open
Source61 opened this issue Dec 4, 2024 · 2 comments
Open

Comments

@Source61
Copy link

Source61 commented Dec 4, 2024

Describe the bug

#6514 (comment)

Take this example:
https://cython.readthedocs.io/en/latest/src/userguide/sharing_declarations.html#sharing-c-functions

When generating a spammery.cpp file using cython --embed -3 spammery.pyx and compile it using gcc/g++ depending on whether I'm using C++ or C, running the executable I get ModuleNotFoundError at runtime even if I've added sys.path.append('.') or sys.path.append('') to the spammery.pyx file.

In addition, when I add import sys etc, the errorline cython cites is "import sys" (or I recon any statement before cimport) as the cause of ModuleNotFoundError, given that there any statements prior, in either case there's a runtime error.

Code to reproduce the behaviour:

volume.pyx

cdef float cube(float x):
  return x * x * x

volume.pxd

cdef float cube(float x)

spammery.pyx

import sys
sys.path.append('.')
sys.path.append('')

from volume cimport cube

def menu(description, size):
    print(description, ":", cube(size),
          "cubic metres of spam")

menu("Entree", 1)
menu("Main course", 3)
menu("Dessert", 2)

Expected behaviour

No compile or runtime errors.

OS

Debian 12 (bookworm)

Python version

3.11.2

Cython version

3.1.0a1

Additional context

No response

@da-woods
Copy link
Contributor

da-woods commented Dec 4, 2024

I think that all cimports happen before anything else in the module, so will happen before your changes to sys.path. I don't think that's easy to change.

We should probably point to the right lines though when showing the error message.

@Source61
Copy link
Author

Source61 commented Dec 6, 2024

I think that all cimports happen before anything else in the module, so will happen before your changes to sys.path. I don't think that's easy to change.

We should probably point to the right lines though when showing the error message.

Thanks for the response.
So is the behavior laid out in the design of Cython that it will by default allow import and cimport modules in sys.path default locations, but not in the current location?
Why is that, when Python by default will import not only anything in the current work directory but subdirectories etc if they have an __init__.py file and such?

Is it out of a security concern that Python doesn't seem to share, and could be easily bypassed by an evil module/program copying another evil module into a sys.path directory at runtime before importing?

It seems like such a strange design choice to me and not in compliance with how Python operates?

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

No branches or pull requests

2 participants