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.
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:
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
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.
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.
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).
来源:https://stackoverflow.com/questions/14633695/how-to-install-python-for-net-on-windows