ImportError: cannot import name 'DllTest' from 'myDll' (unknown location) (pythonnet)

54 Views Asked by At

I installed python 3.8 and 3.12 in my computer, and I installed pythonnet 3.0.3 in 3.8 interpreter and 3.12 (venv) respectively, including clr-loader 0.2.6. Then I repeated the procedures in the following link: installing pythonnet

However, no matter which interpreter I use, the import error pops up: ImportError: cannot import name 'DllTest' from 'myDll' (unknown location). Both my main.py and myDll.dll are in the same location.

main.py

import os
import clr
# myfile = open('C:/Users/k0330/OneDrive/Desktop/Kurse/WS 23 -24/Bachelorarbeit/Python/Project/callMyDll/myDll.dll')
# clr.AddReference(myfile)
#clr.AddReference("C:/Users/k0330/OneDrive/Desktop/Kurse/WS 23 -24/Bachelorarbeit/Python/Project/callMyDll/myDll.dll")
clr.AddReference(r"C:\Users\k0330\OneDrive\Desktop\Kurse\WS 23 -24\Bachelorarbeit\Python\VS\callMyDll\myDll.dll")
from myDll import DllTest

dll = DllTest()
dll.msgboxText()
print(FindMax(7,9))

.NET

Public Class DllTest
    Public Sub msgboxText()
        MsgBox("Hello World!")
    End Sub

    Public Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
        ' local variable declaration */
        Dim result As Integer

        If (num1 > num2) Then
            result = num1
        Else
            result = num2
        End If
        FindMax = result
    End Function

End Class

Then I consulted the resources to try several address types: link 1 file address type

Still the same error.

I noticed that there is no pythonnet package in the base interpreter 3.12, so I followed the source below by SET Path="C:\Users\k0330\AppData\Local\Programs\Python\Python312" in the terminal, and then pip install pythonnet. link 2 However, it said that: Requirement already satisfied: pythonnet in c:\users\k0330\onedrive\desktop\kurse\ws 23 -24\bachelorarbeit\python\project\venv\lib\site-packages (3.0.3) Requirement already satisfied: clr-loader<0.3.0,>=0.2.6 in c:\users\k0330\onedrive\desktop\kurse\ws 23 -24\bachelorarbeit\python\project\venv\lib\site-packages (from pythonnet) (0.2.6) Requirement already satisfied: cffi>=1.13 in c:\users\k0330\onedrive\desktop\kurse\ws 23 -24\bachelorarbeit\python\project\venv\lib\site-packages (from clr-loader<0.3.0,>=0.2.6->pythonnet) (1.16.0) Requirement already satisfied: pycparser in c:\users\k0330\onedrive\desktop\kurse\ws 23 -24\bachelorarbeit\python\project\venv\lib\site-packages (from cffi>=1.13->clr-loader<0.3.0,>=0.2.6->pythonnet) (2.21)

I tried the step above in cmd, also the similar result, but there is still no pythonnet package in base 3.12. Requirement already satisfied: pythonnet in c:\users\k0330\appdata\local\programs\python\python38\lib\site-packages (3.0.3) Requirement already satisfied: clr-loader<0.3.0,>=0.2.6 in c:\users\k0330\appdata\local\programs\python\python38\lib\site-packages (from pythonnet) (0.2.6) Requirement already satisfied: cffi>=1.13 in c:\users\k0330\appdata\local\programs\python\python38\lib\site-packages (from clr-loader<0.3.0,>=0.2.6->pythonnet) (1.16.0) Requirement already satisfied: pycparser in c:\users\k0330\appdata\local\programs\python\python38\lib\site-packages (from cffi>=1.13->clr-loader<0.3.0,>=0.2.6->pythonnet) (2.21)

Thus, I tried all the techniques in the video: Installation by pip and pip3 I have 3.8, 3.8\Script, 3.12 and 3.12\Script in the paths of user variable and system variable. link 3

The results are still the same as above both in Pycharm and VS Code: With 3.8 interpreter: ImportError: cannot import name 'DllTest' from 'myDll' (unknown location) With 3.12 interpreter: ModuleNotFoundError: No module named 'clr'

How can I solve the problems above?

0

There are 0 best solutions below