How to install Python for .NET on Windows
Asked Answered
T

6

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.

Turkmen answered 31/1, 2013 at 19:33 Comment(5)
"python.net" don't you mean ironpython?Tena
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.Turkmen
Your understanding is correct. I like to explain it as IronPython is a .Net env that contains Python. Python .Net allows Python to contain .NetHypsometry
Have the same issue. The version for CLR 2.0 works fine for me but not the 4.0 version.Shilashilha
@Hypsometry ... and allows .NET to embed CPython (not limited IronPython)Lassiter
L
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

Lassiter answered 2/8, 2014 at 4:44 Comment(0)
B
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

Balky answered 20/3, 2013 at 13:21 Comment(2)
I believe the issue is not related to copying over any of the other files.Shilashilha
This works because sys.path includes current directory, but carrying pythonnet with each .py script is not very flexible and maintenance nightmare.Lassiter
H
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.

Hypsometry answered 13/10, 2014 at 17:44 Comment(0)
S
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.

Shilashilha answered 13/3, 2014 at 15:15 Comment(0)
M
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).

Mylor answered 6/11, 2015 at 0:34 Comment(0)
S
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.

Selfdeception answered 10/9, 2023 at 13:43 Comment(1)
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.Binary

© 2022 - 2024 — McMap. All rights reserved.