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.

5
  • "python.net" don't you mean ironpython? Commented Jan 31, 2013 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. Commented Jan 31, 2013 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 Commented Jan 16, 2014 at 16:35
  • Have the same issue. The version for CLR 2.0 works fine for me but not the 4.0 version. Commented Mar 13, 2014 at 14:56
  • @bc3tech ... and allows .NET to embed CPython (not limited IronPython) Commented Jan 11, 2015 at 1:57

6 Answers 6

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

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

Comments

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

2 Comments

I believe the issue is not related to copying over any of the other files.
This works because sys.path includes current directory, but carrying pythonnet with each .py script is not very flexible and maintenance nightmare.
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.

Comments

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.

Comments

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).

Comments

0

I managed to get python net working by downloading python latest version importing modules into it with pip and adding the dll python311.dll to the nuget version of pythonnet and setting runtime.pythonsdll = path to the dll.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.