2019-03-09 21:20:15

Hi all, i am trying to use a assembly in python using pythonnet in python 3.
how ever i get the following error:
AttributeError: module 'clr' has no attribute 'AddReference'                                                           
and the code is from one o the demos in pythonnet downloadible from github

best regards
never give up on what ever you are doing.

2019-03-09 21:52:13

would you mind sharing the code?

2019-03-09 22:08:44

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import clr

import sys
sys.path.append("C:\Windows\assembly")

SWF = clr.AddReference("System.Windows.Forms")
print (SWF.Location)
import System.Windows.Forms as WinForms
from System.Drawing import Size, Point


class HelloApp(WinForms.Form):
    """A simple hello world app that demonstrates the essentials of
       winforms programming and event-based programming in Python."""

    def __init__(self):
        self.Text = "Hello World From Python"
        self.AutoScaleBaseSize = Size(5, 13)
        self.ClientSize = Size(392, 117)
        h = WinForms.SystemInformation.CaptionHeight
        self.MinimumSize = Size(392, (117 + h))

        # Create the button
        self.button = WinForms.Button()
        self.button.Location = Point(160, 64)
        self.button.Size = Size(820, 20)
        self.button.TabIndex = 2
        self.button.Text = "Click Me!"

        # Register the event handler
        self.button.Click += self.button_Click

        # Create the text box
        self.textbox = WinForms.TextBox()
        self.textbox.Text = "Hello World"
        self.textbox.TabIndex = 1
        self.textbox.Size = Size(1260, 40)
        self.textbox.Location = Point(160, 24)

        # Add the controls to the form
        self.AcceptButton = self.button
        self.Controls.Add(self.button)
        self.Controls.Add(self.textbox)

    def button_Click(self, sender, args):
        """Button click event handler"""
        print ("Click")
        WinForms.MessageBox.Show("Please do not press this button again.")

    def run(self):
        WinForms.Application.Run(self)


def main():
    form = HelloApp()
    print ("form created")
    app = WinForms.Application
    print ("app referenced")
    app.Run(form)


if __name__ == '__main__':
    main()

In this demo, all i did was adding the assembly paths.

best regards
never give up on what ever you are doing.

2019-03-09 23:08:36

The issue could be a naming conflict with pythonnet clr, resulting in it loading a different library or file that may also be called clr. This could be because of another installed package that has the same clr import alias, or there could be files named clr in your working directory, which you'll either need to rename or remove. You can also check out the library your importing by adding this code after you import it:

import clr
print(dir(clr))
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-03-09 23:20:45

Hi, it apears that the clr i was using is some thing else. the code is not related to pythonnet. how should i make now?

best regards
never give up on what ever you are doing.

2019-03-09 23:34:23 (edited by magurp244 2019-03-09 23:38:24)

If the clr is in your working directory, either rename it or remove it. If its an installed package you'll need to remove it, something similar was brought up [here]. You can do it with pip:

pip uninstall clr
pip install pythonnet
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-03-09 23:38:06

oh my sole, it works. thanks a lot for your help. and sorry for me being so stupid

best regards
never give up on what ever you are doing.

2019-03-09 23:41:59

No problem, and don't beat yourself up too much over it, programmings all about problem solving. Just another learning experience, heh.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2019-03-09 23:44:43

Yeh, true. i am quite new to python... just these errors i come accross. i learn some thing new every day

best regards
never give up on what ever you are doing.

2019-03-10 06:29:37

hi,
we have a confusing package on pip called clr which you shouldn't install.
you should install only the pythonnet module

2019-03-10 07:46:41

Yes, i saw when installing pythonnet last time. i saw that a clr package was installed, well i left it, as i thought it was a needed package.

best regards
never give up on what ever you are doing.