2

I am a python beginner (python 2.7 and ironpython 2.7) and not a programmer. From time to time, I find a code which starts like so:

import clr
clr.AddReferenceToFile("nameOfmodule")
from nameOfmodule import someMethod

What is the point of this? Can't we just use:

from nameOfmodule import someMethod

?

I tried googling but have not really understand the explanation.

Thank you for the reply.

EDIT: the confusion is not between "from nameOfmodule import someMethod" and "import nameOfmodule.someMethod". I edited the code, so that now it makes more sense.

denfromufa
  • 5,610
  • 13
  • 81
  • 138
marco
  • 899
  • 5
  • 13
  • 21
  • possible duplicate of ['import module' or 'from module import'](http://stackoverflow.com/questions/710551/import-module-or-from-module-import) – Selcuk Feb 26 '15 at 13:32
  • Which bit don't you understand, the `clr` stuff or just the `import` syntax? – jonrsharpe Feb 26 '15 at 13:34

3 Answers3

5

You don't need to import clr and AddReferenceToFile with recent versions of Python for .NET, but it still works for backwards compatibility so the examples you're looking at might still use it so they work for a larger number of people.

With recent versions, you can treat CLR assemblies as normal Python modules:

from nameOfModule import someMethod

clr is the Common Language Runtime, provided by Microsoft's .NET framework. So, in your example, the script uses clr so that it can refer to a component written in C# or Visual Basic and made into a library for use with something else.

http://pythonnet.github.io/ has some more information.

denfromufa
  • 5,610
  • 13
  • 81
  • 138
Simon Fraser
  • 2,758
  • 18
  • 25
1

Are you using IronPython and .NET? If so the code in the first example is required to add a .NET assembly as reference in IronPython, so that Python can import it.

Otherwise you don't need to do it, and from nameOfmodule import someMethod will work.

Note, the code in your second example is incorrect for importing a function in a standard Python module. You can either:

>>> import nameOfmodule
>>> result = nameOfmodule.someMethod()

or

>>> from nameOfmodlue import someMethod
>>> result = someMethod()

but the following will give you an error:

>>> import nameOfmodule.someMethod
ImportError: No module named someMethod
mhawke
  • 84,695
  • 9
  • 117
  • 138
0

In the second example you still have to use nameOfmodule.someMethod(...) when you want to call method, in the first you simply need someMethod(..). If you used import nameOfmodule.someMethod as sm that would make more sense.

If you like typing or you have another someMethod defined somewhere in your code use your second example.

An example using os.path:

In [3]: import os.path     
In [4]: os.path.abspath(".")
Out[4]: '/home/foo'    
In [5]: from os import path
In [6]: path.abspath(".")
Out[6]: '/home/foo'    
In [7]: import os.path as op   
In [8]: op.abspath(".")
Out[8]: '/home/foo'

The clr is only relevant if you are using Ironpython:

The import statement in Python is more analogous to a using statement in C#. you still need to load the relevant .dll assembly. C# does this at compile-time by using references; IronPython by default includes standard CLR references, which is why it is immediately possible to import System.Xml

However, if you want to load a .dll that is not included by default in IronPython, you must use clr.AddReference("myAssembly.dll") and then use the import statement to import the module.

There a a blog post here that goes into more detail

Community
  • 1
  • 1
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321