Skip to content

Import does not work from Cython #1890

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

Closed
josephernest opened this issue Jul 27, 2022 · 7 comments
Closed

Import does not work from Cython #1890

josephernest opened this issue Jul 27, 2022 · 7 comments

Comments

@josephernest
Copy link

josephernest commented Jul 27, 2022

This code works from Python:

import clr
clr.AddReference("abc.dll")
from ABC import abc

But when compiled with Cython --embed, it fails:

cython test.py --embed
call vcvarsallbat x64
cl test.c /I C:\Python38\include /link C:\Python38\libs\python38.lib
test.exe

with:

Traceback (most recent call last):
File "test.py", line 3, in init test
ModuleNotFoundError: No module named 'ABC'

Is this related to #941?

Is there a temporary or permanent fix for this?
Thanks!

A good idea would be to have an import function that can be called on clr namespace:

import clr
clr.AddReference("abc.dll")
ABC = clr.ImportModule('ABC')

that would not need to use the overridden import built-in function.

@filmor
Copy link
Member

filmor commented Jul 27, 2022

  1. Never delete the issue template, it's there for a reason
  2. Using Python.NET from Cython is not supported

@filmor filmor closed this as not planned Won't fix, can't repro, duplicate, stale Jul 27, 2022
@josephernest
Copy link
Author

josephernest commented Jul 27, 2022

Thank you for your answer @filmor.

More generally, is there a way to import a Python.NET module without overriding the Python built-in import function and without adding the imported module to globals() - which can have other side effects than the one I'm describing in Cython?

In order to understand how it works, is there a specific reason for which

import clr
clr.AddReference("abc.dll")

makes the new module ABC ready to be imported as a global variable?

As a comparison with ctypes, we can assign the loaded library into a custom variable name:

mylib_custom_variable_name = cdll.LoadLibrary("mylib.dll")
mylib_custom_variable_name.myfunction()

or to a class-member

self.mylib = cdll.LoadLibrary("mylib.dll")  # not visible in the global variables

Is it a design choice or is there a reason for which it is different in clr? Thanks!

@filmor
Copy link
Member

filmor commented Jul 27, 2022

Well, we have landed some improvements a while ago so that the global import is not actually overridden anymore. Instead we now use a PEP302 finder+loader combination since #1369. What you are suggesting wouldn't work, though, because .NET assemblies don't (usually) have a flat namespace (in contrast to C libraries). We could, however, add a function get_namespace to clr or pythonnet that would allow you to take a RuntimeAssembly object as returned from clr.AddReference and get a facade module as you would get from an import:

import clr
assembly = clr.AddReference('abc')
namespace = clr.get_namespace(assembly, 'ABC')

@josephernest
Copy link
Author

Thank you for your answer @filmor. Is there a way I can test something like this by setting parameters into clr? import clr; clr.options = ... or would this require a new compilation of clr ? (I think I don't have all the .NET compiler requirements to build it)

@filmor
Copy link
Member

filmor commented Jul 27, 2022

This was just a suggestion, it would require implementation :)

We only require a .NET6 SDK for compilation nowadays.

@josephernest
Copy link
Author

Ok @filmor :)
Maybe this small enhancement would allow compilation with Cython? It's probably worth trying!

@filmor
Copy link
Member

filmor commented Jul 29, 2022

It is worth trying, but I have neither time to do it nor a need for this. At the very least, you'll have to wait until 3.0 is released.

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