23

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.

denfromufa
  • 5,610
  • 13
  • 81
  • 138
Ran
  • 635
  • 2
  • 10
  • 22

4 Answers4

45

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.

ASLAN
  • 629
  • 1
  • 7
  • 20
Guancheng Li
  • 459
  • 4
  • 3
  • 2
    Does pythonnet work on Linux systems? I get an error when installing: `error: option --single-version-externally-managed not recognized` – Soerendip Jan 19 '21 at 22:23
  • I had this problem to and can confirm that this answer works. – Ant Jan 14 '22 at 16:28
8

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

pip uninstall clr

And then install pythonnet

pip install pythonnet
Abhijeet sinha
  • 403
  • 4
  • 4
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

Afshin Amiri
  • 3,438
  • 1
  • 20
  • 21
0

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.

Sam
  • 26
  • 3