30

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.

5 Answers 5

53

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does pythonnet work on Linux systems? I get an error when installing: error: option --single-version-externally-managed not recognized
I had this problem to and can confirm that this answer works.
8

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

pip uninstall clr

And then install pythonnet

pip install pythonnet

Comments

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

1 Comment

So whats the correct version to download in that case? Any idea
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.

Comments

0

The safest way is to first initialize Pythonnet using pythonnet.load before importing clr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.