17

I downloaded Python for .NET.
Inside the zip is clr.pyd, nPython.exe, Python.Runtime.dll and 2 debug database files.
I put the clr.pyd and Python.Runtime.dll in my python DLLs dir C:\Python27\DLLs thinking this is all that's needed for installation. I then open up the Python GUI and type import clr and I get:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import clr
SystemError: dynamic module not initialized properly

New to python but not .NET and want to use the CPython and not IronPython. What am I missing in this installation? The readme of Python for .NET says there is an installation for Windows package but all I found was the zip file.

denfromufa
  • 5,610
  • 13
  • 81
  • 138
user441521
  • 6,942
  • 23
  • 88
  • 160
  • "python.net" don't you mean ironpython? – Dude Bro Jan 31 '13 at 19:51
  • 6
    I do not. IronPython is it's own executable made by MS. I want to use the "normal" Python but have the .NET library exposed to it. It was my understanding Python.NET does this. – user441521 Jan 31 '13 at 20:08
  • 2
    Your understanding is correct. I like to explain it as IronPython is a .Net env that contains Python. Python .Net allows Python to contain .Net – bc3tech Jan 16 '14 at 16:35
  • Have the same issue. The version for CLR 2.0 works fine for me but not the 4.0 version. – Jonno Mar 13 '14 at 14:56
  • @bc3tech ... and allows .NET to embed CPython (not limited IronPython) – denfromufa Jan 11 '15 at 01:57

5 Answers5

16

The proper way to load CLR in Python is like this:

  1. Make sure no old stuff is left from Python.NET in Python installation folder (e.g. C:\Python27). In my case I had legacy clr.pyd in one of folders. Note that pip for some old versions did not remove all parts of Python.NET.
  2. Append the directory with Python.NET files (clr.pyd and Python.Runtime.dll) to sys.path

Now you can load CLR the most flexible way without even installing to Python directories!

You can compile from source on github:

pip install git+https://github.com/pythonnet/pythonnet

or use Windows wheels/installers from Christoph Gohlke:

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pythonnet

PyPI package is available for installation from pip:

https://pypi.python.org/pypi/pythonnet

More installation options such docker, WinPython, conda, nuget, choco are listed here:

https://github.com/pythonnet/pythonnet/wiki/Installation

denfromufa
  • 5,610
  • 13
  • 81
  • 138
5

The correct way to install Python for .NET is to copy ALL the files from the .zip into the directory that contains your .py file.

Then if you execute

python yourfilename.py

you should find that your "import clr" statement works. I have tested this using python 2.7 x86 and pythonnet-2.0-Beta0-clr4.0_140_py27_UCS2_x86.zip

I was able to replicate your error by only copying the clr.pyd file into my working directory.

Note that I was unable to make this work in Python 3.3 x86 or Python 3.3 x64

Devin
  • 1,262
  • 15
  • 20
  • I believe the issue is not related to copying over any of the other files. – Jonno Mar 13 '14 at 14:55
  • 2
    This works because sys.path includes current directory, but carrying pythonnet with each .py script is not very flexible and maintenance nightmare. – denfromufa Sep 28 '15 at 15:22
4

I actually took matters in to my own hands here and created a Python.Net Chocolatey package. to install, simply run

cinst pythonnet

at the command line. Maybe this will help users having issues getting this to run.

bc3tech
  • 1,228
  • 14
  • 27
2

I don't know why yet but the only way I could get it to work is to copy those 3 files into the C:\Python27\ directory directly.

Jonno
  • 767
  • 2
  • 7
  • 13
2

If you are planning to freeze with py2exe or pyinstall be sure to install the dev version. There is something wrong with the 2.0.0 release when packaged with py2exe, pyinstaller and maybe other freezers. But 2.1.0.dev1 works well. So...

pip install --pre pythonnet

And you'll have to add the pythone.runtime.dll to the bundle (see docs for your preferred bundler). There is also a problem with how py2exe loads the dll when using bundle_files: 1 (single exe).

MSlimmer
  • 160
  • 1
  • 10