How to install Python for .NET on Windows
Solution 1:
The proper way to load CLR in Python is like this:
- 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. - Append the directory with Python.NET files (
clr.pyd
andPython.Runtime.dll
) tosys.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
Solution 2:
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
Solution 3:
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.
Solution 4:
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.
Solution 5:
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).