Python: AttributeError: 'module' object has no attribute 'AddReference'?
Asked Answered
S

4

27

Am trying to use clr.AddReference and clr.AddReferenceToFile to import an assembly, but python(2.7) keeps making this error:

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    clr.AddReference("UnityEngine")
AttributeError: 'module' object has no attribute 'AddReference'

Can anyone tell me how to fix this.

Shirker answered 20/12, 2017 at 19:26 Comment(0)
S
50

Once I met this issue. There is a package named clr while the pythonnet package's alias is also clr. So I removed clr by "pip uninstall clr" and then installed pythonnet by 'pip install pythonnet'. Finally, everything works well.

Stationmaster answered 3/4, 2018 at 5:25 Comment(2)
Does pythonnet work on Linux systems? I get an error when installing: error: option --single-version-externally-managed not recognizedAgnusago
I had this problem to and can confirm that this answer works.Flotsam
E
8

You must be using the wrong 'clr' module. remove it

pip uninstall clr

And then install pythonnet

pip install pythonnet
Esra answered 29/11, 2020 at 2:44 Comment(0)
W
4

run the code

import clr
dir(clr)

if the reasult is as ['StyleBuilder', 'builtins', 'cached',....] then the problem is your IDE is installing the WRONG Clr module

Wingate answered 18/3, 2018 at 20:10 Comment(1)
So whats the correct version to download in that case? Any ideaHendecahedron
B
1

In addition to having normal clr installed, the mistake I made was the little script I was using to test python.net was named pythonnet.py. So when python.net was trying to load itself, it found my script instead and problems ensued. If you get this specific error:

partially initialized module 'clr' has no attribute 'AddReference'

That could be why.

Bijection answered 1/3, 2023 at 1:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.